Skip to content

emvicy/OpenApi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenApi

a module for Emvicy2 (2.x) PHP Framework: https://github.com/emvicy/Emvicy/tree/2.x


Installation

cd into the modules folder of your Emvicy copy; e.g.:

cd /var/www/html/modules/;

git clone

git clone --branch 3.x https://github.com/emvicy/OpenApi.git OpenApi;

Usage

Validating

validate against an openapi file

use OpenApi\Model\Validate;

$oDTValidateRequestResponse = Validate::request(
    $oDTRequestCurrent,
    Config::get_MVC_PUBLIC_PATH() . '/openapi/api.yaml'
);

header('Content-Type: application/json');
echo json_encode(Convert::objectToArray($oDTValidateRequestResponse));

validate against an openapi URL

use OpenApi\Model\Validate;

// validate against openapi URL
$oDTValidateRequestResponse = Validate::request(
    $oDTRequestCurrent,
    'https://example.com/api/openapi.yaml'
);

header('Content-Type: application/json');
echo json_encode(Convert::objectToArray($oDTValidateRequestResponse));

Auto-creating Emvicy Routes from openapi file

All Routes lead to their given operationId, set in openapi

\OpenApi\Model\Route::autoCreateFromOpenApiFile(
    Config::get_MVC_PUBLIC_PATH() . '/openapi/api.yaml',
    // default Controller
    '\Foo\Controller\Api'
);
  • if operationId in yaml file declaration contains a Class::method that will be used as target

All Routes lead explicitely to Api::delegate()

\OpenApi\Model\Route::autoCreateFromOpenApiFile(
    Config::get_MVC_PUBLIC_PATH() . '/openapi/api.yaml',
    '\Foo\Controller\Api',
    'delegate'
);

Auto-creating local DataType Classes from within Yaml Files or URLs

Create a configuration according to the following scheme; it contains the Yaml files or URLs to be processed.

Example Configuration for several YAML File locations (Foo here is the primary Module)

$aConfig['MODULE']['Foo']['service'] = [
    'Bar' => [ // Service Name
        'aOpenApi' => [
            'aLocation' => [
                'https://bar.example.com/path/to/openapi.yaml', // a remote yaml address
            ],
        ],
    ],
    'Baz' => [ // Service Name
        'aOpenApi' => [
            'aLocation' => [
                '/absolute/path/to/openapi.yaml', // a local yaml address
            ],
        ],
    ],
];

You can then run the cli command to generate local DataType Classes according to the yaml files.

php emvicy openapi:datatype

DTClassesOnOpenapi3yaml

otherwise you can call the Generator explicitely.

// generate from any accessible yaml location
\OpenApi\Model\Generate::DTClassesOnOpenapi3yaml(
    sOpenApiFile: '/absolute/path/to/openapi.yaml', # openapi yaml file|URL
    sSubDirName: 'DTOpenapi',                       # Storing Classes in `/modules/{primary}/DataType/DTOpenapi`
    bUnlinkDir: false,                              # remove and create Folder for new; true|false
    bValueFromExample: false,                       # take values from "example" as default values
    bDebug: true                                    # print debug infos
);

Get Logs

Logs are fired to Events.

Available events are:

  • Emvicy_module_OpenApi::sYamlSource

listen to event and write its content to a logfile

\MVC\Event::bind('Emvicy_module_OpenApi::sYamlSource', function($sContent){
    \MVC\Log::write($sContent, 'openapi.log');
});

About

::module:: - OpenApi module for Emvicy

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages