Skip to content

Commit 85e0641

Browse files
committed
Introduce Generator settings
Generator settings are following the same pattern as processor config and are stored under the key `'generator'`. `Generator::getSetting()` provides a shortcut to access generator settings.
1 parent 0d35ed2 commit 85e0641

13 files changed

Lines changed: 91 additions & 17 deletions

bin/openapi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ foreach ($options["processor"] as $processor) {
218218
}
219219

220220
$analyser = new ReflectionAnalyser([new DocBlockAnnotationFactory(), new AttributeAnnotationFactory()]);
221+
$analyser->setGenerator($generator);
221222

222223
$openapi = $generator
223224
->setVersion($options['version'])

src/Analysers/AnalyserInterface.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88

99
use OpenApi\Analysis;
1010
use OpenApi\Context;
11-
use OpenApi\Generator;
1211

13-
interface AnalyserInterface
12+
interface AnalyserInterface extends GeneratorAwareInterface
1413
{
15-
public function setGenerator(Generator $generator): void;
16-
1714
public function fromFile(string $filename, Context $context): Analysis;
1815
}

src/Analysers/AnnotationFactoryInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
use OpenApi\Context;
1111
use OpenApi\Generator;
1212

13-
interface AnnotationFactoryInterface
13+
interface AnnotationFactoryInterface extends GeneratorAwareInterface
1414
{
1515
/**
1616
* Checks if this factory is supported by the current runtime.
1717
*/
1818
public function isSupported(): bool;
1919

20-
public function setGenerator(Generator $generator): void;
20+
public function setGenerator(Generator $generator);
2121

2222
/**
2323
* @return array<OA\AbstractAnnotation> top level annotations

src/Analysers/AttributeAnnotationFactory.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,17 @@ public function build(\Reflector $reflector, Context $context): array
3636
/** @var OA\AbstractAnnotation[] $annotations */
3737
$annotations = [];
3838
try {
39-
foreach ($reflector->getAttributes() as $attribute) {
39+
$attributeArgs = $this->generator->getSetting('ignoreOtherAttributes')
40+
? [OA\AbstractAnnotation::class, \ReflectionAttribute::IS_INSTANCEOF]
41+
: [];
42+
43+
foreach ($reflector->getAttributes(...$attributeArgs) as $attribute) {
4044
if (class_exists($attribute->getName())) {
4145
$instance = $attribute->newInstance();
4246
if ($instance instanceof OA\AbstractAnnotation) {
4347
$annotations[] = $instance;
4448
} else {
45-
if ($context->is('other') === false) {
49+
if (false === $context->is('other')) {
4650
$context->other = [];
4751
}
4852
$context->other[] = $instance;

src/Analysers/DocBlockAnnotationFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ public function isSupported(): bool
2626
return DocBlockParser::isEnabled();
2727
}
2828

29-
public function setGenerator(Generator $generator): void
29+
public function setGenerator(Generator $generator)
3030
{
3131
$this->generator = $generator;
3232

3333
$this->docBlockParser->setAliases($generator->getAliases());
34+
35+
return $this;
3436
}
3537

3638
public function build(\Reflector $reflector, Context $context): array
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
/**
4+
* @license Apache 2.0
5+
*/
6+
7+
namespace OpenApi\Analysers;
8+
9+
use OpenApi\Generator;
10+
11+
interface GeneratorAwareInterface
12+
{
13+
public function setGenerator(Generator $generator);
14+
}

src/Analysers/GeneratorAwareTrait.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ trait GeneratorAwareTrait
1212
{
1313
protected ?Generator $generator = null;
1414

15-
public function setGenerator(Generator $generator): void
15+
public function setGenerator(Generator $generator)
1616
{
1717
$this->generator = $generator;
18+
19+
return $this;
1820
}
1921
}

src/Analysers/ReflectionAnalyser.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ public function __construct(array $annotationFactories = [])
4242
}
4343
}
4444

45-
public function setGenerator(Generator $generator): void
45+
public function setGenerator(Generator $generator)
4646
{
4747
$this->generator = $generator;
4848

4949
foreach ($this->annotationFactories as $annotationFactory) {
5050
$annotationFactory->setGenerator($generator);
5151
}
52+
53+
return $this;
5254
}
5355

5456
public function fromFile(string $filename, Context $context): Analysis

src/Generator.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ public function setAnalyser(?AnalyserInterface $analyser): Generator
135135
return $this;
136136
}
137137

138+
/**
139+
* @deprecated
140+
*/
138141
public function getDefaultConfig(): array
139142
{
140143
return [
@@ -146,7 +149,19 @@ public function getDefaultConfig(): array
146149

147150
public function getConfig(): array
148151
{
149-
return $this->config + $this->getDefaultConfig();
152+
return $this->config + $this->getDefaultConfig() + [
153+
'generator' => [
154+
'ignoreOtherAttributes' => false,
155+
],
156+
];
157+
}
158+
159+
/**
160+
* Get the value of a `Generator` setting.
161+
*/
162+
public function getSetting(string $name)
163+
{
164+
return $this->getConfig()['generator'][$name] ?? null;
150165
}
151166

152167
protected function normaliseConfig(array $config): array

tests/Analysers/AttributeAnnotationFactoryTest.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace OpenApi\Tests\Analysers;
88

99
use OpenApi\Analysers\AttributeAnnotationFactory;
10+
use OpenApi\Generator;
1011
use OpenApi\Tests\Fixtures\UsingAttributes;
1112
use OpenApi\Tests\Fixtures\InvalidPropertyAttribute;
1213
use OpenApi\Tests\OpenApiTestCase;
@@ -16,11 +17,22 @@
1617
*/
1718
class AttributeAnnotationFactoryTest extends OpenApiTestCase
1819
{
19-
public function testReturnedAnnotationsCout(): void
20+
protected function getFactory(?array $config = null): AttributeAnnotationFactory
21+
{
22+
$generator = new Generator();
23+
if (null !== $config) {
24+
$generator->setConfig($config);
25+
}
26+
27+
return (new AttributeAnnotationFactory())
28+
->setGenerator($generator);
29+
}
30+
31+
public function testReturnedAnnotationsCount(): void
2032
{
2133
$rc = new \ReflectionClass(UsingAttributes::class);
2234

23-
$annotations = (new AttributeAnnotationFactory())->build($rc, $this->getContext());
35+
$annotations = $this->getFactory()->build($rc, $this->getContext());
2436
$this->assertCount(1, $annotations);
2537
}
2638

@@ -32,6 +44,18 @@ public function testErrorOnInvalidAttribute(): void
3244
$this->expectException(\TypeError::class);
3345
$this->expectExceptionMessage('OpenApi\Attributes\Property::__construct(): Argument #8 ($required) must be of type ?array');
3446

35-
(new AttributeAnnotationFactory())->build($rm, $this->getContext());
47+
$this->getFactory()->build($rm, $this->getContext());
48+
}
49+
50+
public function testIgnoreOtherAttributes(): void
51+
{
52+
$rc = new \ReflectionClass(UsingAttributes::class);
53+
54+
$this->getFactory()->build($rc, $context = $this->getContext());
55+
$this->assertIsArray($context->other);
56+
$this->assertCount(1, $context->other);
57+
58+
$this->getFactory(['generator' => ['ignoreOtherAttributes' => true]])->build($rc, $context = $this->getContext());
59+
$this->assertNull($context->other);
3660
}
3761
}

0 commit comments

Comments
 (0)