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