Go to file
Martin Rademacher 46c3e4c493
Dev tools (#1243)
* Add .gitattributes and .editorconfig
* Add basic fixer to ensure we have a @license docblock
* Add missing dev autoload config
2022-05-20 16:17:19 +12:00
.github/workflows Docs codeblocks (#1232) 2022-05-19 12:13:01 +12:00
Examples Components cleanup (#1199) 2022-04-22 11:03:38 +12:00
bin OpenApi version override (#1179) 2022-04-01 08:32:16 +13:00
docs Dev tools (#1243) 2022-05-20 16:17:19 +12:00
src Dev tools (#1243) 2022-05-20 16:17:19 +12:00
tests Allow original backing value in Enum (#1239) 2022-05-19 08:42:31 +12:00
tools Dev tools (#1243) 2022-05-20 16:17:19 +12:00
.editorconfig Dev tools (#1243) 2022-05-20 16:17:19 +12:00
.gitattributes Dev tools (#1243) 2022-05-20 16:17:19 +12:00
.gitignore Add test to ensure attribute parameters are matching annotations (#1217) 2022-04-30 11:31:20 +12:00
.php-cs-fixer.dist.php Dev tools (#1243) 2022-05-20 16:17:19 +12:00
.spectral.yaml Improve example specs (#990) 2021-10-08 14:11:42 +13:00
Changelog.md Document the changelog on the github releases page 2018-05-22 11:00:17 +02:00
LICENSE Rename license file to be in line with what GH expects (#1188) 2022-04-06 15:38:04 +12:00
README.md Docs codeblocks (#1232) 2022-05-19 12:13:01 +12:00
composer.json Dev tools (#1243) 2022-05-20 16:17:19 +12:00
phpstan-baseline.neon Add PHPStan workflow and baseline (#1222) 2022-05-04 09:57:50 +12:00
phpstan.neon Add PHPStan workflow and baseline (#1222) 2022-05-04 09:57:50 +12:00
phpunit.xml.dist Github action CI migration (#886) 2021-01-06 08:46:26 +13:00
psalm-baseline.xml Add Psalm to analysis chain (#1223) 2022-05-04 11:32:59 +12:00
psalm.xml Add Psalm to analysis chain (#1223) 2022-05-04 11:32:59 +12:00

README.md

Build Status Total Downloads License

swagger-php

Generate interactive OpenAPI documentation for your RESTful API using doctrine annotations.

For a full list of supported annotations, please have look at the OpenApi\Annotations namespace or the documentation website.

Features

  • Compatible with the OpenAPI 3.0 and 3.1 specification.
  • Extracts information from code & existing phpdoc annotations.
  • Command-line interface available.
  • Documentation site with a getting started guide.
  • Exceptional error reporting (with hints, context)
  • As of PHP 8.1 all annotations are also available as PHP attributes

OpenAPI version support

swagger-php allows to generate specs either for OpenAPI 3.0.0 or OpenAPI 3.1.0. By default the spec will be in version 3.0.0. The command line option --version may be used to change this to 3.1.0.

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

Requirements

swagger-php requires at least PHP 7.2 for annotations and PHP 8.1 for using attributes.

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

Usage

Add annotations to your php files.

/**
 * @OA\Info(title="My First API", version="0.1")
 */

/**
 * @OA\Get(
 *     path="/api/resource.json",
 *     @OA\Response(response="200", description="An example resource")
 * )
 */

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 = \OpenApi\Generator::scan(['/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

Starting with version 4 the default analyser used on the command line is the new ReflectionAnalyser.

Using the --legacy flag (-l) the legacy TokenAnalyser can still be used.

Usage from the Deserializer

Generate the OpenApi annotation object from a json string, which makes it easier to manipulate objects programmatically.

<?php

use OpenApi\Serializer;

$serializer = new Serializer();
$openapi = $serializer->deserialize($jsonString, 'OpenApi\Annotations\OpenApi');
echo $openapi->toJson();

Usage from docker

Generate the swagger documentation to a static json file.

docker run -v "$PWD":/app -it tico/swagger-php --help

More on OpenApi & Swagger

Contributing

Feel free to submit Github Issues or pull requests.

The documentation website is build from the docs folder with vuepress.

Make sure pull requests pass PHPUnit and PHP-CS-Fixer (PSR-2) tests.

To run both unit tests and linting execute:

composer test

Running unit tests only:

./bin/phpunit

Regenerate annotation/attribute reference markup docs

composer docs:refgen

Running linting only:

composer lint

To make php-cs-fixer fix linting errors:

composer cs