1 | <?php |
||
2 | |||
3 | /** |
||
4 | * It's free open-source software released under the MIT License. |
||
5 | * |
||
6 | * @author Anatoly Nekhay <[email protected]> |
||
7 | * @copyright Copyright (c) 2018, Anatoly Nekhay |
||
8 | * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
||
9 | * @link https://github.com/sunrise-php/http-router |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Sunrise\Http\Router\OpenApi; |
||
15 | |||
16 | use ReflectionClass; |
||
17 | use ReflectionMethod; |
||
18 | use Sunrise\Http\Router\OpenApi\OperationEnricher\EmptyResponseOperationEnricher; |
||
19 | use Sunrise\Http\Router\OpenApi\OperationEnricher\EncodableResponseOperationEnricher; |
||
20 | use Sunrise\Http\Router\OpenApi\OperationEnricher\RequestBodyOperationEnricher; |
||
21 | use Sunrise\Http\Router\OpenApi\OperationEnricher\RequestCookiesOperationEnricher; |
||
22 | use Sunrise\Http\Router\OpenApi\OperationEnricher\RequestHeadersOperationEnricher; |
||
23 | use Sunrise\Http\Router\OpenApi\OperationEnricher\RequestQueryOperationEnricher; |
||
24 | use Sunrise\Http\Router\OpenApi\OperationEnricher\RequestVariablesOperationEnricher; |
||
25 | use Sunrise\Http\Router\RouteInterface; |
||
26 | |||
27 | use function usort; |
||
28 | |||
29 | /** |
||
30 | * @since 3.0.0 |
||
31 | */ |
||
32 | final class OpenApiOperationEnricherManager implements OpenApiOperationEnricherManagerInterface |
||
33 | { |
||
34 | /** |
||
35 | * @var array<array-key, OpenApiOperationEnricherInterface> |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
36 | */ |
||
37 | private array $operationEnrichers = []; |
||
38 | |||
39 | private bool $isOperationEnrichersSorted = false; |
||
40 | |||
41 | /** |
||
42 | * @param array<array-key, OpenApiOperationEnricherInterface> $operationEnrichers |
||
0 ignored issues
–
show
|
|||
43 | */ |
||
44 | 5 | public function __construct( |
|
45 | private readonly OpenApiConfiguration $openApiConfiguration, |
||
46 | private readonly OpenApiPhpTypeSchemaResolverManagerInterface $openApiPhpTypeSchemaResolverManager, |
||
47 | array $operationEnrichers = [], |
||
48 | ) { |
||
49 | 5 | $this->setOperationEnrichers(self::getDefaultOperationEnrichers()); |
|
50 | 5 | $this->setOperationEnrichers($operationEnrichers); |
|
51 | } |
||
52 | |||
53 | /** |
||
54 | * @inheritDoc |
||
55 | */ |
||
56 | 3 | public function enrichOperation( |
|
57 | RouteInterface $route, |
||
58 | ReflectionMethod|ReflectionClass $requestHandler, |
||
59 | array &$operation, |
||
60 | ): void { |
||
61 | 3 | $this->isOperationEnrichersSorted or $this->sortOperationEnrichers(); |
|
62 | 3 | foreach ($this->operationEnrichers as $operationEnricher) { |
|
63 | 3 | $operationEnricher->enrichOperation($route, $requestHandler, $operation); |
|
64 | } |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param array<array-key, OpenApiOperationEnricherInterface> $operationEnrichers |
||
0 ignored issues
–
show
|
|||
69 | */ |
||
70 | 5 | private function setOperationEnrichers(array $operationEnrichers): void |
|
71 | { |
||
72 | 5 | foreach ($operationEnrichers as $operationEnricher) { |
|
73 | 5 | $this->operationEnrichers[] = $operationEnricher; |
|
74 | |||
75 | 5 | if ($operationEnricher instanceof OpenApiConfigurationAwareInterface) { |
|
76 | 5 | $operationEnricher->setOpenApiConfiguration($this->openApiConfiguration); |
|
77 | } |
||
78 | 5 | if ($operationEnricher instanceof OpenApiPhpTypeSchemaResolverManagerAwareInterface) { |
|
79 | 5 | $operationEnricher->setOpenApiPhpTypeSchemaResolverManager($this->openApiPhpTypeSchemaResolverManager); |
|
80 | } |
||
81 | } |
||
82 | } |
||
83 | |||
84 | 3 | private function sortOperationEnrichers(): void |
|
85 | { |
||
86 | 3 | $this->isOperationEnrichersSorted = usort($this->operationEnrichers, static fn( |
|
87 | 3 | OpenApiOperationEnricherInterface $a, |
|
88 | 3 | OpenApiOperationEnricherInterface $b, |
|
89 | 3 | ): int => $b->getWeight() <=> $a->getWeight()); |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * @return array<array-key, OpenApiOperationEnricherInterface> |
||
0 ignored issues
–
show
|
|||
94 | */ |
||
95 | 5 | private static function getDefaultOperationEnrichers(): array |
|
96 | { |
||
97 | 5 | return [ |
|
98 | 5 | new EmptyResponseOperationEnricher(), |
|
99 | 5 | new EncodableResponseOperationEnricher(), |
|
100 | 5 | new RequestBodyOperationEnricher(), |
|
101 | 5 | new RequestCookiesOperationEnricher(), |
|
102 | 5 | new RequestHeadersOperationEnricher(), |
|
103 | 5 | new RequestQueryOperationEnricher(), |
|
104 | 5 | new RequestVariablesOperationEnricher(), |
|
105 | 5 | ]; |
|
106 | } |
||
107 | } |
||
108 |