kaz29/cakephp-open-api-theme-plugin

OpenApiTheme plugin for CakePHP

Installs: 14 357

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 2

Forks: 1

Open Issues: 1

Language:Twig

Type:cakephp-plugin

1.1.6 2024-05-15 20:51 UTC

This package is auto-updated.

Last update: 2024-11-15 21:49:18 UTC


README

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require kaz29/cakephp-open-api-theme-plugin --dev

Dependency

Setup

  • Copy swagger-settings.php to config directory.

  • Describe the settings of your application in swagger-settings.php.

  • Add "build:swagger" : "build-swagger-json" into your app's composer.json scripts section.

    "scripts": {
        "post-install-cmd": "App\\Console\\Installer::postInstall",
        "post-create-project-cmd": "App\\Console\\Installer::postInstall",
        "check": [
            "@test",
            "@cs-check"
        ],
        "cs-check": "phpcs --colors -p  src/ tests/",
        "cs-fix": "phpcbf --colors -p src/ tests/",
        "stan": "phpstan analyse",
        "test": "phpunit --colors=always",
                "build:openapi": "openapi -b ./vendor/autoload.php --output /srv/docs/openapi.yml ./src"  // <-- add this line
    },
    
  • Add the following configuration to the end of config/bootstrap_cli.php.

Configure::write('Bake.theme', 'OpenApiTheme');
  • Change Application.php as follows.
#[OA\Info(title: '**Your Application name**', version: '**Your application version**')]
#[OA\Server(description: '**Environment name**', url: '**URL**')]
#[OA\Schema(
    schema: 'ValidationErrorResponse',
    title: 'Validation error response',
    required: ['message', 'errors'],
    properties: [
        new OA\Property(
            property: 'message',
            type: 'string',
            description: 'Error message',
        ),
        new OA\Property(
            property: 'errors',
            type: 'object',
            description: 'Error details',
        ),
    ]
)]
class Application extends BaseApplication
...
    protected function bootstrapCli(): void
    {
        try {
            $this->addPlugin('Bake');
        } catch (MissingPluginException $e) {
            // Do not halt if the plugin is missing
        }

        $this->addPlugin('Migrations');

        // Load more plugins here
        $this->addPlugin('OpenApiTheme');  // Add this line
    }

Usage example

bake controller

$ bin/cake bake open_api_controller Articles --prefix Api

bake model

$ bin/cake bake open_api_model Articles

create swagger.json

$ composer build:swagger

Baked example

A baked example of the CakePHP CMS Tutorial can be found in the example directory.

Author

Kazuhiro Watanabe - cyo [at] mac.com - https://twitter.com/kaz_29

License

OpenApiTheme plugin for CakePHP is licensed under the MIT License - see the LICENSE file for details