1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Fenric <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Fenric |
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Router; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Import classes |
16
|
|
|
*/ |
17
|
|
|
use Doctrine\Common\Annotations\SimpleAnnotationReader; |
18
|
|
|
use Sunrise\Http\Router\Annotation\OpenApi\Operation; |
19
|
|
|
use Sunrise\Http\Router\Annotation\OpenApi\Parameter; |
20
|
|
|
use Sunrise\Http\Router\Annotation\OpenApi\Schema; |
21
|
|
|
use Sunrise\Http\Router\OpenApi\Info; |
22
|
|
|
use ReflectionClass; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Import functions |
26
|
|
|
*/ |
27
|
|
|
use function strtolower; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* OpenApi |
31
|
|
|
*/ |
32
|
|
|
class OpenApi extends OpenApi\OpenApi |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public const VERSION = '3.0.2'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var SimpleAnnotationReader |
42
|
|
|
*/ |
43
|
|
|
private $annotationReader; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor of the class |
47
|
|
|
* |
48
|
|
|
* @param Info $info |
49
|
|
|
*/ |
50
|
|
|
public function __construct(Info $info) |
51
|
|
|
{ |
52
|
|
|
parent::__construct(self::VERSION, $info); |
53
|
|
|
|
54
|
|
|
$this->annotationReader = new SimpleAnnotationReader(); |
55
|
|
|
$this->annotationReader->addNamespace('Sunrise\Http\Router\Annotation'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param RouteInterface ...$routes |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
|
|
public function addRoute(RouteInterface ...$routes) : void |
64
|
|
|
{ |
65
|
|
|
foreach ($routes as $route) { |
66
|
|
|
$path = path_plain($route->getPath()); |
|
|
|
|
67
|
|
|
$operation = $this->createOperation($route); |
68
|
|
|
|
69
|
|
|
foreach ($route->getMethods() as $method) { |
70
|
|
|
$method = strtolower($method); |
71
|
|
|
|
72
|
|
|
$this->paths[$path][$method] = $operation; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param RouteInterface $route |
79
|
|
|
* |
80
|
|
|
* @return Operation |
81
|
|
|
*/ |
82
|
|
|
private function createOperation(RouteInterface $route) : Operation |
83
|
|
|
{ |
84
|
|
|
$target = new ReflectionClass($route->getRequestHandler()); |
85
|
|
|
|
86
|
|
|
$operation = $this->annotationReader->getClassAnnotation($target, Operation::class) ?? new Operation(); |
87
|
|
|
|
88
|
|
|
$operation->operationId = $operation->operationId ?? $route->getName(); |
89
|
|
|
|
90
|
|
|
$this->addComponentObject(...$operation->getComponentObjects($this->annotationReader)); |
91
|
|
|
|
92
|
|
|
$attributes = path_parse($route->getPath()); |
|
|
|
|
93
|
|
|
|
94
|
|
|
foreach ($attributes as $attribute) { |
95
|
|
|
$parameter = new Parameter(); |
96
|
|
|
$parameter->in = 'path'; |
97
|
|
|
$parameter->name = $attribute['name']; |
98
|
|
|
$parameter->required = !$attribute['isOptional']; |
99
|
|
|
|
100
|
|
|
if (isset($attribute['pattern'])) { |
101
|
|
|
$parameter->schema = new Schema(); |
102
|
|
|
$parameter->schema->type = 'string'; |
103
|
|
|
$parameter->schema->pattern = $attribute['pattern']; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$operation->parameters[] = $parameter; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $operation; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|