PHP code example of wedo / openapi-generator
1. Go to this page and download the library: Download wedo/openapi-generator library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wedo / openapi-generator example snippets
//minimal config
$config = new Config();
$config->baseRequest = BaseRequest::class;
$config->serverUrl = 'https://www.mydomain.com/api/v1';
$config->baseEnum = BaseEnum::class; // use with @see annotation if U want to provide user enum options
$config->fProcessor()->generateRef(ClassType::from(ErrorResponse::class));
};
$this->generator->getClassProcessor()->getMethodProcessor()->onProcess[] = function() {
// set some standard error responses for each endpoint
$methodProcessor = $this->generator->getClassProcessor()->getMethodProcessor();
$path->responses[400] = $methodProcessor->createResponse('Bad request error response', 'ErrorResponse');
$annotations = $method->getAnnotations();
if (!empty($annotations['throws'])) {
// add your own error responses classes for some specific exceptions
}
}
//set some title
$this->generator->getJson()->info->title = 'My api';
//add your security schemes if needed
$this->generator->getJson()->components->securitySchemes = [
'APIKeyHeader' => [
'type' => 'apiKey',
'in' => 'header',
'name' => 'api-key',
]
];
$json = $this->generator->generate();
file_put_contents('open-api.json', $json);