Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make doctrine/annotations optional #1508

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

# swagger-php

Generate interactive [OpenAPI](https://www.openapis.org) documentation for your RESTful API using [doctrine annotations](https://www.doctrine-project.org/projects/doctrine-annotations/en/latest/index.html).
Generate interactive [OpenAPI](https://www.openapis.org) documentation for your RESTful API using
[doctrine annotations](https://www.doctrine-project.org/projects/annotations.html) (optional as of version 4.8)
or [PHP attributes](https://www.php.net/manual/en/language.attributes.overview.php).

For a full list of supported annotations, please have look at the [`OpenApi\Annotations` namespace](src/Annotations) or the [documentation website](https://zircote.github.io/swagger-php/guide/annotations.html).

Expand Down Expand Up @@ -41,6 +43,16 @@ For cli usage from anywhere install swagger-php globally and make sure to place
composer global require zircote/swagger-php
```

### doctrine/annotations
As of version `4.8` the [doctrine annotations](https://www.doctrine-project.org/projects/annotations.html) library **is optional** and **no longer installed by default**.

If your code uses PHPDoc annotations you will need to install this as well:

```shell
composer require doctrine/annotations
```


## Usage

Add annotations to your php files.
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"require": {
"php": ">=7.2",
"ext-json": "*",
"doctrine/annotations": "^1.7 || ^2.0",
"psr/log": "^1.1 || ^2.0 || ^3.0",
"symfony/deprecation-contracts": "^2 || ^3",
"symfony/finder": ">=2.2",
Expand All @@ -59,11 +58,15 @@
},
"require-dev": {
"composer/package-versions-deprecated": "^1.11",
"doctrine/annotations": "^1.7 || ^2.0",
"friendsofphp/php-cs-fixer": "^2.17 || ^3.0",
"phpstan/phpstan": "^1.6",
"phpunit/phpunit": ">=8",
"vimeo/psalm": "^4.23"
},
"suggest": {
"doctrine/annotations": "^1.7 || ^2.0"
},
"autoload-dev": {
"exclude-from-classmap": [
"/tests/Fixtures"
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ However, doctrine will be quite specific about whether an alias is valid or not.
any additional work.

## Doctrine
Annotations are PHP comments (docblocks) containing [doctrine style annotations](https://www.doctrine-project.org/projects/doctrine-annotations/en/latest/index.html).
Annotations are PHP comments (docblocks) containing [doctrine style annotations](https://www.doctrine-project.org/projects/annotations.html).

::: info
All documentation related to doctrine applies to annotations only.
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ An array instance is valid against this property if its number of items is great
<dd><p>A boolean value indicating whether all items in an array property must be unique.<br />
<br />
If this attribute is set to true, then all items in the array must be unique.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor49">JSON schema validation</a></p></dd>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|\UnitEnum[]|class-string</span></dt>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string</span></dt>
<dd><p>A collection of allowable values for a property.<br />
<br />
A property instance is valid against this attribute if its value is one of the values specified in this collection.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor76">JSON schema validation</a></p></dd>
Expand Down Expand Up @@ -1163,7 +1163,7 @@ An object representing a server variable for server URL template substitution.
<dl>
<dt><strong>serverVariable</strong> : <span style="font-family: monospace;">string</span></dt>
<dd><p>The key into Server->variables array.</p></dd>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|\UnitEnum[]|class-string</span></dt>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string</span></dt>
<dd><p>An enumeration of values to be used if the substitution options are from a limited set.</p></dd>
<dt><strong>default</strong> : <span style="font-family: monospace;">string</span></dt>
<dd><p>The default value to use for substitution, and to send, if an alternate value is not supplied.<br />
Expand Down
14 changes: 7 additions & 7 deletions docs/reference/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ An array instance is valid against this property if its number of items is great
If this attribute is set to true, then all items in the array must be unique.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor49">JSON schema validation</a></p></dd>
<dt><strong>pattern</strong> : <span style="font-family: monospace;">string|null</span></dt>
<dd><p>A string instance is considered valid if the regular expression matches the instance successfully.</p></dd>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|\UnitEnum[]|class-string</span></dt>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string</span></dt>
<dd><p>A collection of allowable values for a property.<br />
<br />
A property instance is valid against this attribute if its value is one of the values specified in this collection.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor76">JSON schema validation</a></p></dd>
Expand Down Expand Up @@ -1057,7 +1057,7 @@ An array instance is valid against this property if its number of items is great
If this attribute is set to true, then all items in the array must be unique.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor49">JSON schema validation</a></p></dd>
<dt><strong>pattern</strong> : <span style="font-family: monospace;">string|null</span></dt>
<dd><p>A string instance is considered valid if the regular expression matches the instance successfully.</p></dd>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|\UnitEnum[]|class-string</span></dt>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string</span></dt>
<dd><p>A collection of allowable values for a property.<br />
<br />
A property instance is valid against this attribute if its value is one of the values specified in this collection.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor76">JSON schema validation</a></p></dd>
Expand Down Expand Up @@ -1215,7 +1215,7 @@ An array instance is valid against this property if its number of items is great
If this attribute is set to true, then all items in the array must be unique.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor49">JSON schema validation</a></p></dd>
<dt><strong>pattern</strong> : <span style="font-family: monospace;">string|null</span></dt>
<dd><p>A string instance is considered valid if the regular expression matches the instance successfully.</p></dd>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|\UnitEnum[]|class-string</span></dt>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string</span></dt>
<dd><p>A collection of allowable values for a property.<br />
<br />
A property instance is valid against this attribute if its value is one of the values specified in this collection.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor76">JSON schema validation</a></p></dd>
Expand Down Expand Up @@ -2079,7 +2079,7 @@ An array instance is valid against this property if its number of items is great
If this attribute is set to true, then all items in the array must be unique.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor49">JSON schema validation</a></p></dd>
<dt><strong>pattern</strong> : <span style="font-family: monospace;">string|null</span></dt>
<dd><p>A string instance is considered valid if the regular expression matches the instance successfully.</p></dd>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|\UnitEnum[]|class-string</span></dt>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string</span></dt>
<dd><p>A collection of allowable values for a property.<br />
<br />
A property instance is valid against this attribute if its value is one of the values specified in this collection.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor76">JSON schema validation</a></p></dd>
Expand Down Expand Up @@ -2525,7 +2525,7 @@ An array instance is valid against this property if its number of items is great
If this attribute is set to true, then all items in the array must be unique.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor49">JSON schema validation</a></p></dd>
<dt><strong>pattern</strong> : <span style="font-family: monospace;">string|null</span></dt>
<dd><p>A string instance is considered valid if the regular expression matches the instance successfully.</p></dd>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|\UnitEnum[]|class-string</span></dt>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string</span></dt>
<dd><p>A collection of allowable values for a property.<br />
<br />
A property instance is valid against this attribute if its value is one of the values specified in this collection.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor76">JSON schema validation</a></p></dd>
Expand Down Expand Up @@ -2700,7 +2700,7 @@ CommonMark syntax MAY be used for rich text representation.</p></dd>
<dd><p>The default value to use for substitution, and to send, if an alternate value is not supplied.<br />
<br />
Unlike the Schema Object's default, this value must be provided by the consumer.</p></dd>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|\UnitEnum[]|class-string|null</span></dt>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string|null</span></dt>
<dd><p>An enumeration of values to be used if the substitution options are from a limited set.</p></dd>
<dt><strong>variables</strong> : <span style="font-family: monospace;">array|null</span></dt>
<dd><p>A map between a variable name and its value.<br />
Expand Down Expand Up @@ -2973,7 +2973,7 @@ An array instance is valid against this property if its number of items is great
If this attribute is set to true, then all items in the array must be unique.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor49">JSON schema validation</a></p></dd>
<dt><strong>pattern</strong> : <span style="font-family: monospace;">string|null</span></dt>
<dd><p>A string instance is considered valid if the regular expression matches the instance successfully.</p></dd>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|\UnitEnum[]|class-string</span></dt>
<dt><strong>enum</strong> : <span style="font-family: monospace;">string[]|int[]|float[]|bool[]|\UnitEnum[]|class-string</span></dt>
<dd><p>A collection of allowable values for a property.<br />
<br />
A property instance is valid against this attribute if its value is one of the values specified in this collection.</p><p><i>See</i>: <a href="http://json-schema.org/latest/json-schema-validation.html#anchor76">JSON schema validation</a></p></dd>
Expand Down
5 changes: 5 additions & 0 deletions src/Analysers/AnnotationFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

interface AnnotationFactoryInterface
{
/**
* Checks if this factory is supported by the current runtime.
*/
public function isSupported(): bool;

public function setGenerator(Generator $generator): void;

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Analysers/AttributeAnnotationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ class AttributeAnnotationFactory implements AnnotationFactoryInterface
/** @var Generator|null */
protected $generator;

public function isSupported(): bool
{
return \PHP_VERSION_ID >= 80100;
}

public function setGenerator(Generator $generator): void
{
$this->generator = $generator;
}

public function build(\Reflector $reflector, Context $context): array
{
if (\PHP_VERSION_ID < 80100 || !method_exists($reflector, 'getAttributes')) {
if (!$this->isSupported() || !method_exists($reflector, 'getAttributes')) {
return [];
}

Expand Down
5 changes: 5 additions & 0 deletions src/Analysers/DocBlockAnnotationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public function __construct(?DocBlockParser $docBlockParser = null)
$this->docBlockParser = $docBlockParser ?: new DocBlockParser();
}

public function isSupported(): bool
{
return DocBlockParser::isEnabled();
}

public function setGenerator(Generator $generator): void
{
$this->generator = $generator;
Expand Down
18 changes: 14 additions & 4 deletions src/Analysers/DocBlockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@ class DocBlockParser
*/
public function __construct(array $aliases = [])
{
$docParser = new DocParser();
$docParser->setIgnoreNotImportedAnnotations(true);
$docParser->setImports($aliases);
$this->docParser = $docParser;
if (DocBlockParser::isEnabled()) {
$docParser = new DocParser();
$docParser->setIgnoreNotImportedAnnotations(true);
$docParser->setImports($aliases);
$this->docParser = $docParser;
}
}

/**
* Check if we can process annotations.
*/
public static function isEnabled(): bool
{
return class_exists('Doctrine\\Common\\Annotations\\DocParser');
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/Analysers/ReflectionAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ class ReflectionAnalyser implements AnalyserInterface
*/
public function __construct(array $annotationFactories = [])
{
$this->annotationFactories = $annotationFactories;
$this->annotationFactories = [];
foreach ($annotationFactories as $annotationFactory) {
if ($annotationFactory->isSupported()) {
$this->annotationFactories[] = $annotationFactory;
}
}
if (!$this->annotationFactories) {
throw new \InvalidArgumentException('Need at least one annotation factory');
throw new \RuntimeException('No suitable annotation factory found. At least one of "Doctrine Annotations" or PHP 8.1 are required');
}
}

Expand Down
5 changes: 5 additions & 0 deletions tests/Analysers/ReflectionAnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public function build(\Reflector $reflector, Context $context): array
return [];
}

public function isSupported(): bool
{
return true;
}

public function setGenerator(Generator $generator): void
{
// noop
Expand Down
Loading