|
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\RequestVariable; |
|
20
|
|
|
use Sunrise\Http\Router\Helper\RouteCompiler; |
|
21
|
|
|
use Sunrise\Http\Router\Helper\RouteParser; |
|
22
|
|
|
use Sunrise\Http\Router\OpenApi\OpenApiOperationEnricherInterface; |
|
23
|
|
|
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverManagerAwareInterface; |
|
24
|
|
|
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverManagerInterface; |
|
25
|
|
|
use Sunrise\Http\Router\OpenApi\Type; |
|
26
|
|
|
use Sunrise\Http\Router\OpenApi\TypeFactory; |
|
27
|
|
|
use Sunrise\Http\Router\RouteInterface; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @since 3.0.0 |
|
31
|
|
|
*/ |
|
32
|
|
|
final class RequestVariablesOperationEnricher implements |
|
33
|
|
|
OpenApiOperationEnricherInterface, |
|
34
|
|
|
OpenApiPhpTypeSchemaResolverManagerAwareInterface |
|
35
|
|
|
{ |
|
36
|
|
|
private readonly OpenApiPhpTypeSchemaResolverManagerInterface $openApiPhpTypeSchemaResolverManager; |
|
37
|
|
|
|
|
38
|
5 |
|
public function setOpenApiPhpTypeSchemaResolverManager( |
|
39
|
|
|
OpenApiPhpTypeSchemaResolverManagerInterface $openApiPhpTypeSchemaResolverManager, |
|
40
|
|
|
): void { |
|
41
|
5 |
|
$this->openApiPhpTypeSchemaResolverManager = $openApiPhpTypeSchemaResolverManager; |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @inheritDoc |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function enrichOperation( |
|
48
|
|
|
RouteInterface $route, |
|
49
|
|
|
ReflectionClass|ReflectionMethod $requestHandler, |
|
50
|
|
|
array &$operation, |
|
51
|
|
|
): void { |
|
52
|
3 |
|
$variables = RouteParser::parseRoute($route->getPath()); |
|
53
|
3 |
|
$variablePatterns = $route->getPatterns(); |
|
54
|
|
|
|
|
55
|
3 |
|
foreach ($variables as $variable) { |
|
56
|
3 |
|
$variableName = $variable['name']; |
|
57
|
|
|
|
|
58
|
3 |
|
$variablePattern = $variable['pattern'] |
|
59
|
3 |
|
?? $variablePatterns[$variableName] |
|
60
|
3 |
|
?? RouteCompiler::DEFAULT_VARIABLE_PATTERN; |
|
61
|
|
|
|
|
62
|
3 |
|
$variableSchema = $this->getRequestVariableSchema($requestHandler, $variableName); |
|
63
|
3 |
|
$variableSchema ??= ['type' => Type::OAS_TYPE_NAME_STRING]; |
|
64
|
3 |
|
$variableSchema['pattern'] = '^' . $variablePattern . '$'; |
|
65
|
|
|
|
|
66
|
3 |
|
$operation['parameters'][] = [ |
|
67
|
3 |
|
'in' => 'path', |
|
68
|
3 |
|
'name' => $variableName, |
|
69
|
3 |
|
'schema' => $variableSchema, |
|
70
|
|
|
// https://github.com/OAI/OpenAPI-Specification/issues/93 |
|
71
|
3 |
|
'required' => true, |
|
72
|
3 |
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
public function getWeight(): int |
|
77
|
|
|
{ |
|
78
|
3 |
|
return 40; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param ReflectionClass<object>|ReflectionMethod $requestHandler |
|
83
|
|
|
* |
|
84
|
|
|
* @return array<array-key, mixed>|null |
|
|
|
|
|
|
85
|
|
|
*/ |
|
86
|
3 |
|
private function getRequestVariableSchema( |
|
87
|
|
|
ReflectionClass|ReflectionMethod $requestHandler, |
|
88
|
|
|
string $variableName, |
|
89
|
|
|
): ?array { |
|
90
|
3 |
|
if ($requestHandler instanceof ReflectionMethod) { |
|
91
|
3 |
|
foreach ($requestHandler->getParameters() as $requestHandlerParameter) { |
|
92
|
|
|
/** @var list<ReflectionAttribute<RequestVariable>> $annotations */ |
|
93
|
3 |
|
$annotations = $requestHandlerParameter->getAttributes(RequestVariable::class); |
|
94
|
3 |
|
if (isset($annotations[0])) { |
|
95
|
3 |
|
$requestVariable = $annotations[0]->newInstance(); |
|
96
|
3 |
|
$requestVariableName = $requestVariable->name ?? $requestHandlerParameter->name; |
|
97
|
3 |
|
if ($requestVariableName === $variableName) { |
|
98
|
3 |
|
$requestVariableType = TypeFactory::fromPhpTypeReflection($requestHandlerParameter->getType()); |
|
99
|
|
|
|
|
100
|
3 |
|
return $this->openApiPhpTypeSchemaResolverManager |
|
101
|
3 |
|
->resolvePhpTypeSchema($requestVariableType, $requestHandlerParameter); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
3 |
|
return null; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|