Go to file
Martin Rademacher e8c3bb316e
Update for PHP 8.2 (#1377)
2023-01-04 10:17:10 +13:00
.github/workflows Update for PHP 8.2 (#1377) 2023-01-04 10:17:10 +13:00
Examples Attachables (#995) 2021-11-08 13:45:29 +13:00
bin Switch all logging to using a PSR logger (#968) 2021-08-13 11:48:58 +12:00
docs Add Generator class for OO use (#928) 2021-05-20 12:45:34 +12:00
src Update for PHP 8.2 (#1377) 2023-01-04 10:17:10 +13:00
tests add psr/log 2.0 and 3.0 to 3.x branch (#1006) 2021-12-05 11:35:19 +13:00
.gitignore Streamline processors dealing with inheritance (parents, interfaces, traits) (#974) 2021-08-30 15:36:58 +12:00
.php-cs-fixer.dist.php Upgrade to PHP cs-fixer 3.0 (#956) 2021-06-24 10:35:01 +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-2.0.txt Started the rewrite which will be compatible with Swagger v2.0 Spec 2015-01-16 18:10:11 +01:00
README.md Add Generator class for OO use (#928) 2021-05-20 12:45:34 +12:00
composer.json Add PHP 8.1 to build matrix (#1041) 2021-12-24 08:45:20 +13:00
phpunit.xml.dist Github action CI migration (#886) 2021-01-06 08:46:26 +13:00
psalm.xml Typehint and phpdoc improvements based on phpstan 2020-09-07 15:18:08 +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 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)

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 Migration guide.

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

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

Running linting only:

composer lint

To make php-cs-fixer fix linting errors:

composer cs