1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Flight Routing. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.4 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Flight\Routing\Annotation; |
19
|
|
|
|
20
|
|
|
use Biurad\Annotations\{InvalidAnnotationException, ListenerInterface}; |
21
|
|
|
use Flight\Routing\Handlers\ResourceHandler; |
22
|
|
|
use Flight\Routing\RouteCollection; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The Biurad Annotation's Listener bridge. |
26
|
|
|
* |
27
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class Listener implements ListenerInterface |
30
|
|
|
{ |
31
|
|
|
private RouteCollection $collector; |
32
|
|
|
|
33
|
|
|
public function __construct(RouteCollection $collector = null) |
34
|
|
|
{ |
35
|
|
|
$this->collector = $collector ?? new RouteCollection(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
9 |
|
*/ |
41
|
|
|
public function load(array $annotations): RouteCollection |
42
|
9 |
|
{ |
43
|
9 |
|
foreach ($annotations as $annotation) { |
44
|
|
|
$reflection = $annotation['type']; |
45
|
|
|
$attributes = $annotation['attributes'] ?? []; |
46
|
|
|
|
47
|
|
|
if (empty($methods = $annotation['methods'] ?? [])) { |
48
|
|
|
foreach ($attributes as $route) { |
49
|
9 |
|
$this->addRoute($this->collector, $route, $reflection->getName()); |
50
|
|
|
} |
51
|
9 |
|
continue; |
52
|
|
|
} |
53
|
9 |
|
|
54
|
9 |
|
if (empty($attributes)) { |
55
|
9 |
|
foreach ($methods as $method) { |
56
|
|
|
foreach (($method['attributes'] ?? []) as $route) { |
57
|
9 |
|
$controller = ($m = $method['type'])->isStatic() ? $reflection->name.'::'.$m->name : [$reflection->name, $m->name]; |
58
|
6 |
|
$this->addRoute($this->collector, $route, $controller); |
59
|
5 |
|
} |
60
|
|
|
} |
61
|
|
|
continue; |
62
|
8 |
|
} |
63
|
8 |
|
|
64
|
8 |
|
foreach ($attributes as $classAnnotation) { |
65
|
|
|
$group = empty($classAnnotation->resource) |
66
|
|
|
? $this->addRoute($this->collector->group($classAnnotation->name, return: true), $classAnnotation, true) |
67
|
7 |
|
: throw new InvalidAnnotationException('Restful annotated class cannot contain annotated method(s).'); |
68
|
7 |
|
|
69
|
5 |
|
foreach ($methods as $method) { |
70
|
5 |
|
foreach (($method['attributes'] ?? []) as $methodAnnotation) { |
71
|
|
|
$controller = ($m = $method['type'])->isStatic() ? $reflection->name.'::'.$m->name : [$reflection->name, $m->name]; |
72
|
|
|
$this->addRoute($group, $methodAnnotation, $controller); |
73
|
5 |
|
} |
74
|
5 |
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
7 |
|
|
78
|
7 |
|
return $this->collector; |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
6 |
|
* {@inheritdoc} |
83
|
6 |
|
*/ |
84
|
6 |
|
public function getAnnotations(): array |
85
|
6 |
|
{ |
86
|
6 |
|
return ['Flight\Routing\Annotation\Route']; |
87
|
6 |
|
} |
88
|
|
|
|
89
|
6 |
|
protected function addRoute(RouteCollection $collection, Route $route, mixed $handler): RouteCollection |
90
|
6 |
|
{ |
91
|
|
|
if (true !== $handler) { |
92
|
|
|
if (empty($route->path)) { |
93
|
6 |
|
throw new InvalidAnnotationException('Attributed method route path empty'); |
94
|
6 |
|
} |
95
|
|
|
|
96
|
|
|
if (!empty($route->resource)) { |
97
|
6 |
|
$handler = !\is_string($handler) || !\class_exists($handler) |
98
|
|
|
? throw new InvalidAnnotationException('Restful routing is only supported on attribute route classes.') |
99
|
|
|
: new ResourceHandler($handler, $route->resource); |
100
|
|
|
} |
101
|
|
|
|
102
|
6 |
|
$collection->add($route->path, $route->methods ?: ['GET'], $handler); |
103
|
|
|
|
104
|
|
|
if (!empty($route->name)) { |
105
|
|
|
$collection->bind($route->name); |
106
|
|
|
} |
107
|
|
|
} else { |
108
|
8 |
|
if (!empty($route->path)) { |
109
|
|
|
$collection->prefix($route->path); |
110
|
8 |
|
} |
111
|
|
|
|
112
|
|
|
if (!empty($route->methods)) { |
113
|
|
|
$collection->method(...$route->methods); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (!empty($route->schemes)) { |
118
|
9 |
|
$collection->scheme(...$route->schemes); |
119
|
|
|
} |
120
|
9 |
|
|
121
|
9 |
|
if (!empty($route->hosts)) { |
122
|
1 |
|
$collection->domain(...$route->hosts); |
123
|
|
|
} |
124
|
|
|
|
125
|
8 |
|
if (!empty($route->where)) { |
126
|
|
|
$collection->placeholders($route->where); |
127
|
7 |
|
} |
128
|
5 |
|
|
129
|
|
|
if (!empty($route->defaults)) { |
130
|
|
|
$collection->defaults($route->defaults); |
131
|
7 |
|
} |
132
|
|
|
|
133
|
|
|
if (!empty($route->arguments)) { |
134
|
|
|
$collection->arguments($route->arguments); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $collection; |
138
|
6 |
|
} |
139
|
|
|
} |
140
|
|
|
|