yiisoft/yii-swagger

Open Api Swagger 3 for Yii Framework

dev-master / 3.0.x-dev 2021-09-18 19:57 UTC

This package is auto-updated.

Last update: 2021-09-18 19:57:08 UTC


README

yiisoft.png

Yii Swagger


Latest Stable Version Total Downloads Build status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis type-coverage

Installation

The package could be installed with composer:

composer require yiisoft/yii-swagger

Configuration

1. Add route configuration to config/routes.php

use Yiisoft\DataResponse\Middleware\FormatDataResponseAsHtml;
use Yiisoft\DataResponse\Middleware\FormatDataResponseAsJson;
use Yiisoft\Router\Group;
use Yiisoft\Router\Route;
use Yiisoft\Swagger\Middleware\SwaggerUi;
use Yiisoft\Swagger\Middleware\SwaggerJson;

// Swagger routes
Group::create('/swagger', [
    Route::get('')
        ->addMiddleware(fn (SwaggerUi $swaggerUi) => $swaggerUi->withJsonUrl('/swagger/json-url'))
        ->addMiddleware(FormatDataResponseAsHtml::class),
    Route::get('/json-url')
        ->addMiddleware(static function (SwaggerJson $swaggerJson) {
            return $swaggerJson
                // Uncomment cache for production environment
                // ->withCache(3600)
                ->withAnnotationPaths([
                    '@src/Controller' // Path to API controllers
                ]);
        })
        ->addMiddleware(FormatDataResponseAsJson::class),
]),

2. Add annotations to default API controller

use OpenApi\Annotations as OA;

/**
 * @OA\Info(title="My first API", version="1.0")
 */
class DefaultController {
    // ...
}

and before actions

/**
 * @OA\Get(
 *     path="/api/endpoint",
 *     @OA\Response(response="200", description="Get default action")
 * )
 */
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
    // ...
}

See Swagger-PHP documentation for details on how to annotate your code.

3. (Optional) Add config for aliases and asset manager

use Yiisoft\Definitions\Reference;
use Yiisoft\Assets\AssetManager;

return [
    //...
    'yiisoft/aliases' => [
        'aliases' => [
            //...
            '@views' => '@root/views',
            '@assets' => '@public/assets',
            '@assetsUrl' => '@baseUrl/assets',
        ],
    ],
    'yiisoft/view' => [
        'basePath' => '@views',
        'defaultParameters' => [
            'assetManager' => Reference::to(AssetManager::class),
        ]
    ],
    //...

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework. To run it:

./vendor/bin/infection

Static analysis

The code is statically analyzed with Psalm. To run static analysis:

./vendor/bin/psalm

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

License

The Yii Swagger is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.