Skip to content

zircote/swagger-php

Build Status Total Downloads License

swagger-php

Generate interactive OpenAPI documentation for your RESTful API using PHP attributes (preferred) or doctrine annotations (requires additional doctrine/annotations library).

See the documentation website for supported attributes and annotations.

Annotations are deprecated and may be removed in a future release of swagger-php.

Features

  • Compatible with the OpenAPI 3.0, 3.1 and 3.2 specification.
  • Extracts information from code and existing phpdoc comments.
  • Can be used programmatically or via command-line tool.
  • Documentation site with a getting started guide.
  • Error reporting (with hints, context).
  • All metadata is configured via PHP attributes.

OpenAPI version support

swagger-php allows to generate specs either for OpenAPI 3.0.0, OpenAPI 3.1.0 and OpenAPI 3.2.0. By default, the spec will be in version 3.0.0. The command line option --version may be used to change to any other supported version.

Programmatically, the method Generator::setVersion() can be used to change the version.

Requirements

swagger-php requires at least PHP 8.2.

Installation (with Composer)

composer require zircote/swagger-php

For cli usage from anywhere, install swagger-php globally and make sure to place the ~/.composer/vendor/bin directory in your PATH so the openapi executable can be located by your system.

composer global require zircote/swagger-php

doctrine/annotations

As of version 4.8 the doctrine annotations library is optional and no longer installed by default.

If your code uses doctrine annotations you will need to install that library manually:

composer require doctrine/annotations

Usage

Use OpenAPI attributes to add metadata to your classes, methods and other structural PHP elements.

use OpenApi\Attributes as OAT;

#[OAT\Info(title: 'My First API', version: '0.1')]
class MyApi
{
    #[OAT\Get(path: '/api/resource.json')]
    #[OAT\Response(response: '200', description: 'An example resource')]
    public function getResource()
    {
        // ...
    }
}

Visit the Documentation website for the Getting started guide or look at the examples directory for more examples.

Usage from PHP

Generate always-up-to-date documentation.

<?php
require("vendor/autoload.php");
$openapi = (new \OpenApi\Generator->generate(['/path/to/project']);
header('Content-Type: application/x-yaml');
echo $openapi->toYaml();

Documentation of how to use the Generator class can be found in the Generator reference.

Usage from the Command Line Interface

The openapi command line interface can be used to generate the documentation to a static yaml/json file.

./vendor/bin/openapi --help

Automatic type resolution

As of version 6, resolving of types is done using the TypeInfoTypeResolver class. It uses the symfony/type-info library under the hood which allows handling of complext types.

With this change, swagger-php supports all available native type-hints and also complext generic type-hints via phpdoc blocks. This simplifies the definition of schemas.

For example, the following two examples are now equivalent:

class MyClass
{
    #[OAT\Property(items: new OAT\Items(oneOf: [
        new OAT\Schema(type: SchemaOne::class),
        new OAT\Schema(type: SchemaTwo::class),
    ]))]
    public array $values;
}
class MyClass
{
    /**
     * @var list<SchemaOne|SchemaTwo>
     */
    public array $values;
}

If this is not desired, the LegacyTypeResolver can be used to preserve the old behaviour of version 5. The LegacyTypeResolver is deprecated and will be removed in a future release.

More on OpenApi & Swagger

About

A php swagger annotation and parsing library

Resources

License

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Contributors 176

Languages