Debian Codegraph @

vendor/zircote/swagger-php/README.md

raw
1 [![Build Status](https://img.shields.io/github/actions/workflow/status/zircote/swagger-php/build.yml?branch=master)](https://github.com/zircote/swagger-php/actions?query=workflow:build)
2 [![Total Downloads](https://img.shields.io/packagist/dt/zircote/swagger-php.svg)](https://packagist.org/packages/zircote/swagger-php)
3 [![License](https://img.shields.io/badge/license-Apache2.0-blue.svg)](LICENSE)
4
5 # swagger-php
6
7 Generate interactive [OpenAPI](https://www.openapis.org) documentation for your RESTful API
8 using [PHP attributes](https://www.php.net/manual/en/language.attributes.overview.php) (preferred) or
9 [doctrine annotations](https://www.doctrine-project.org/projects/annotations.html) (requires additional
10 `doctrine/annotations` library).
11
12 See the [documentation website](https://zircote.github.io/swagger-php/guide/using-attributes.html) for supported
13 attributes and annotations.
14
15 **Annotations are deprecated and may be removed in a future release of swagger-php.**
16
17 ## Features
18
19 - Compatible with the OpenAPI **3.0**, **3.1** and **3.2** specification.
20 - Extracts information from code and existing phpdoc comments.
21 - Can be used programmatically or via command-line tool.
22 - [Documentation site](https://zircote.github.io/swagger-php/) with a getting started guide.
23 - Error reporting (with hints, context).
24 - All metadata is configured via PHP attributes.
25
26 ## OpenAPI version support
27
28 `swagger-php` allows to generate specs either for **OpenAPI 3.0.0**, **OpenAPI 3.1.0** and **OpenAPI 3.2.0**.
29 By default, the spec will be in version `3.0.0`. The command line option `--version` may be used to change to
30 any other supported version.
31
32 Programmatically, the method `Generator::setVersion()` can be used to change the version.
33
34 ## Requirements
35
36 `swagger-php` requires at least **PHP 8.2**.
37
38 ## Installation (with [Composer](https://getcomposer.org))
39
40 ```shell
41 composer require zircote/swagger-php
42 ```
43
44 For cli usage from anywhere, install swagger-php globally and make sure to place the `~/.composer/vendor/bin` directory
45 in your PATH so the `openapi` executable can be located by your system.
46
47 ```shell
48 composer global require zircote/swagger-php
49 ```
50
51 ### doctrine/annotations
52
53 As of version `4.8` the [doctrine annotations](https://www.doctrine-project.org/projects/annotations.html) library **is
54 optional** and **no longer installed by default**.
55
56 If your code uses doctrine annotations you will need to install that library manually:
57
58 ```shell
59 composer require doctrine/annotations
60 ```
61
62 ## Usage
63
64 Use OpenAPI attributes to add metadata to your classes, methods and other structural PHP elements.
65
66 ```php
67
68 use OpenApi\Attributes as OAT;
69
70 #[OAT\Info(title: 'My First API', version: '0.1')]
71 class MyApi
72 {
73 #[OAT\Get(path: '/api/resource.json')]
74 #[OAT\Response(response: '200', description: 'An example resource')]
75 public function getResource()
76 {
77 // ...
78 }
79 }
80 ```
81
82 Visit the [Documentation website](https://zircote.github.io/swagger-php/) for
83 the [Getting started guide](https://zircote.github.io/swagger-php/guide) or look at
84 the [examples directory](docs/examples) for more examples.
85
86 ### Usage from PHP
87
88 Generate always-up-to-date documentation.
89
90 ```php
91 <?php
92 require("vendor/autoload.php");
93 $openapi = (new \OpenApi\Generator())->generate(['/path/to/project']);
94 header('Content-Type: application/x-yaml');
95 echo $openapi->toYaml();
96 ```
97
98 Documentation of how to use the `Generator` class can be found in
99 the [Generator reference](https://zircote.github.io/swagger-php/reference/generator).
100
101 ### Usage from the Command Line Interface
102
103 The `openapi` command line interface can be used to generate the documentation to a static yaml/json file.
104
105 ```shell
106 ./vendor/bin/openapi --help
107 ```
108
109 ## Automatic type resolution
110
111 As of version 6, resolving of types is done using the `TypeInfoTypeResolver` class. It uses the `symfony/type-info`
112 library under the hood which allows handling of complext types.
113
114 With this change, `swagger-php` supports all available native type-hints and also complext generic type-hints via phpdoc
115 blocks.
116 This simplifies the definition of schemas.
117
118 For example, the following two examples are now equivalent:
119
120 ```php
121 class MyClass
122 {
123 #[OAT\Property(items: new OAT\Items(oneOf: [
124 new OAT\Schema(type: SchemaOne::class),
125 new OAT\Schema(type: SchemaTwo::class),
126 ]))]
127 public array $values;
128 }
129 ```
130
131 ```php
132 class MyClass
133 {
134 /**
135 * @var list<SchemaOne|SchemaTwo>
136 */
137 public array $values;
138 }
139 ```
140
141 If this is not desired, the `LegacyTypeResolver` can be used to preserve the old behaviour of version 5.
142 The `LegacyTypeResolver` is deprecated and will be removed in a future release.
143
144 ## [Contributing](CONTRIBUTING.md)
145
146 ## More on OpenApi & Swagger
147
148 - https://swagger.io
149 - https://www.openapis.org
150 - [OpenApi Documentation](https://swagger.io/docs/)
151 - [OpenApi Specification](http://swagger.io/specification/)
152 - [Related projects](docs/related-projects.md)