zircote/swagger-php
swagger-php generates OpenAPI 3.0/3.1/3.2 documentation from your PHP 8.2+ code using attributes (preferred) or optional Doctrine annotations. Use it via CLI or programmatically, with helpful error reporting and a full documentation site.
GeneratorThe Generator class is the main entry point into swagger-php.
\OpenApi\Generator classThe Generator class provides an object-oriented (and fluent) API to generate OpenApi specs
and allows for easy customization where needed.
Generator class to generate OpenApi specsrequire('vendor/autoload.php');
$validate = true;
$logger = new \Psr\Log\NullLogger();
$processors = [/* my processors */];
$finder = \Symfony\Component\Finder\Finder::create()->files()->name('*.php')->in(__DIR__);
$context = new OpenApi\Context();
$openapi = (new \OpenApi\Generator($logger))
->setProcessorPipeline(new \OpenApi\Pipeline($processors))
->setAliases(['MY' => 'My\Annotations'])
->setNamespaces(['My\\Annotations\\'])
->setAnalyser(new \OpenApi\Analysers\ReflectionAnalyser([new OpenApi\Analysers\DocBlockAnnotationFactory(), new OpenApi\Analysers\AttributeAnnotationFactory()]))
->setVersion(\OpenApi\Annotations\OpenApi::VERSION_3_0_0)
->generate(['/path1/to/project', $finder], new \OpenApi\Analysis([], $context), $validate);
Aliases and namespaces setting are optional and only required when using annotations.
They allow to customize the parsing of docblocks and are passed through to the underlying doctrine/annotations library.
Defaults:
aliases: ['oa' => 'OpenApi\\Annotations']
Aliases help to parse annotations. Effectively, they avoid having to write use OpenApi\Annotations as OA; in your code
and make [@OA](https://github.com/OA)\property(..) annotations still work.
namespaces: ['OpenApi\\Annotations\\']
Namespaces control which annotation namespaces can be autoloaded automatically. Under the hood this
is handled by registering a custom loader with the doctrine annotation library.
require('"vendor/autoload.php');
$openapi = (new \OpenApi\Generator())->generate(['/path1/to/project']);
The generate() method does not support the CLI exclude and pattern options directly.
Instead, a custom Symfony Finder class (SourceFinder) can be used that understands these options.
$exclude = ['tests'];
$pattern = '*.php';
$openapi = (new \OpenApi\Generator())->generate(new \OpenApi\SourceFinder(__DIR__, $exclude, $pattern));
How can I help you explore Laravel packages today?