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\OperationEnricher; |
15
|
|
|
|
16
|
|
|
use ReflectionAttribute; |
17
|
|
|
use ReflectionClass; |
18
|
|
|
use ReflectionMethod; |
19
|
|
|
use Sunrise\Http\Router\Annotation\RequestHeader; |
20
|
|
|
use Sunrise\Http\Router\OpenApi\OpenApiOperationEnricherInterface; |
21
|
|
|
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverManagerAwareInterface; |
22
|
|
|
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverManagerInterface; |
23
|
|
|
use Sunrise\Http\Router\OpenApi\TypeFactory; |
24
|
|
|
use Sunrise\Http\Router\RouteInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @since 3.0.0 |
28
|
|
|
*/ |
29
|
|
|
final class RequestHeadersOperationEnricher implements |
30
|
|
|
OpenApiOperationEnricherInterface, |
31
|
|
|
OpenApiPhpTypeSchemaResolverManagerAwareInterface |
32
|
|
|
{ |
33
|
|
|
private readonly OpenApiPhpTypeSchemaResolverManagerInterface $openApiPhpTypeSchemaResolverManager; |
34
|
|
|
|
35
|
5 |
|
public function setOpenApiPhpTypeSchemaResolverManager( |
36
|
|
|
OpenApiPhpTypeSchemaResolverManagerInterface $openApiPhpTypeSchemaResolverManager, |
37
|
|
|
): void { |
38
|
5 |
|
$this->openApiPhpTypeSchemaResolverManager = $openApiPhpTypeSchemaResolverManager; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritDoc |
43
|
|
|
*/ |
44
|
3 |
|
public function enrichOperation( |
45
|
|
|
RouteInterface $route, |
46
|
|
|
ReflectionClass|ReflectionMethod $requestHandler, |
47
|
|
|
array &$operation, |
48
|
|
|
): void { |
49
|
3 |
|
if (! $requestHandler instanceof ReflectionMethod) { |
50
|
3 |
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
foreach ($requestHandler->getParameters() as $requestHandlerParameter) { |
54
|
|
|
/** @var list<ReflectionAttribute<RequestHeader>> $annotations */ |
55
|
3 |
|
$annotations = $requestHandlerParameter->getAttributes(RequestHeader::class); |
56
|
3 |
|
if (isset($annotations[0])) { |
57
|
3 |
|
$requestHeader = $annotations[0]->newInstance(); |
58
|
3 |
|
$requestHeaderType = TypeFactory::fromPhpTypeReflection($requestHandlerParameter->getType()); |
59
|
3 |
|
$requestHeaderSchema = $this->openApiPhpTypeSchemaResolverManager |
60
|
3 |
|
->resolvePhpTypeSchema($requestHeaderType, $requestHandlerParameter); |
61
|
|
|
|
62
|
3 |
|
$operation['parameters'][] = [ |
63
|
3 |
|
'in' => 'header', |
64
|
3 |
|
'name' => $requestHeader->name, |
65
|
3 |
|
'schema' => $requestHeaderSchema, |
66
|
3 |
|
'required' => !$requestHandlerParameter->isDefaultValueAvailable(), |
67
|
3 |
|
]; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
public function getWeight(): int |
73
|
|
|
{ |
74
|
3 |
|
return 20; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|