/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
api/controllers/data/SwaggerController.php
149 строк
4 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace api\controllers\data; use Swagger\Annotations\Swagger; use Symfony\Component\Finder\Finder; use Yii; use yii\base\ExitException; use yii\base\InvalidConfigException; use yii\caching\Cache; use yii\filters\ContentNegotiator; use yii\helpers\ArrayHelper; use yii\web\BadRequestHttpException; use yii\web\Controller; use yii\web\Response; use function Swagger\scan; /** * Генерация документации для Swagger * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2019 */ class SwaggerController extends Controller { /** * @var string|array|Finder The directory(s) or filename(s). * If you configured the directory must be full path of the directory. */ public $scanDir; /** * @var array The options passed to `Swagger`, Please refer the `Swagger\scan` function for more information. */ public $scanOptions = []; /** * @var Cache|string|null the cache object or the ID of the cache application component that is used to store * Cache the \Swagger\Scan */ public $cache = 'swagger'; /** * @var string Cache key * [[cache]] must not be null */ public $cacheKey = 'api-swagger-cache'; /** * @inheritdoc */ public function behaviors() { return ArrayHelper::merge(parent::behaviors(), [ 'contentNegotiator' => [ 'class' => ContentNegotiator::class, 'formats' => [ 'application/json' => Response::FORMAT_JSON, 'application/xml' => Response::FORMAT_XML, ], ], ]); } /** * Получить данные для документации * * @return Swagger * @throws ExitException * @throws InvalidConfigException */ public function actionData() { Yii::$app->getResponse()->format = Response::FORMAT_JSON; $headers = Yii::$app->getResponse()->getHeaders(); $headers->set('Access-Control-Allow-Headers', 'Content-Type, apiKey, Authorization'); $headers->set('Access-Control-Allow-Methods', 'GET, POST, DELETE, PUT'); $headers->set('Access-Control-Allow-Origin', '*'); $headers->set('Allow', 'OPTIONS,HEAD,GET'); $this->clearCache(); // if ($this->cache !== null) { // $cache = $this->getCache(); // if (($swagger = $cache->get($this->cacheKey)) === false) { // $swagger = $this->getSwagger(); // $cache->set($this->cacheKey, $swagger); // } // } else { // $swagger = $this->getSwagger(); // } $swagger = $this->getSwagger(); return $swagger; } /** * Удаление кэша * @throws ExitException * @throws InvalidConfigException */ protected function clearCache() { $clearCache = Yii::$app->getRequest()->get('clear-cache', false); if ($clearCache !== false) { $this->getCache()->delete($this->cacheKey); Yii::$app->response->content = 'Succeed clear swagger api cache.'; Yii::$app->end(); } } /** * Получение кэша * @return Cache * @throws InvalidConfigException */ protected function getCache() { return is_string($this->cache) ? Yii::$app->get($this->cache, false) : $this->cache; } /** * Get swagger object * @return Swagger */ protected function getSwagger() { return scan($this->scanDir, $this->scanOptions); } /** * @inheritdoc * @throws BadRequestHttpException */ public function beforeAction($action) { $parent = parent::beforeAction($action); $this->scanDir = [ Yii::getAlias('@api/controllers'), Yii::getAlias('@api/swagger'), Yii::getAlias('@common/forms/api'), ]; return $parent; } }