1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Flight Routing. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.1 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\Traits; |
19
|
|
|
|
20
|
|
|
use Biurad\Annotations\LoaderInterface; |
21
|
|
|
use Flight\Routing\DebugRoute; |
22
|
|
|
use Flight\Routing\Exceptions\RouteNotFoundException; |
23
|
|
|
use Flight\Routing\Interfaces\RouteInterface; |
24
|
|
|
use Flight\Routing\Interfaces\RouteListenerInterface; |
25
|
|
|
use Flight\Routing\Interfaces\RouteListInterface; |
26
|
|
|
use Flight\Routing\Interfaces\RouteMatcherInterface; |
27
|
|
|
use Flight\Routing\Router; |
28
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
29
|
|
|
use Psr\Http\Message\UriFactoryInterface; |
30
|
|
|
|
31
|
|
|
trait RouterTrait |
32
|
|
|
{ |
33
|
|
|
use ResolveTrait; |
34
|
|
|
use DumperTrait; |
35
|
|
|
|
36
|
|
|
/** @var RouteMatcherInterface */ |
37
|
|
|
private $matcher; |
38
|
|
|
|
39
|
|
|
/** @var ResponseFactoryInterface */ |
40
|
|
|
private $responseFactory; |
41
|
|
|
|
42
|
|
|
/** @var UriFactoryInterface */ |
43
|
|
|
private $uriFactory; |
44
|
|
|
|
45
|
|
|
/** @var null|DebugRoute */ |
46
|
|
|
private $debug; |
47
|
|
|
|
48
|
|
|
/** @var RouteInterface[] */ |
49
|
|
|
private $routes = []; |
50
|
|
|
|
51
|
|
|
/** @var RouteListenerInterface[] */ |
52
|
|
|
private $listeners = []; |
53
|
|
|
|
54
|
|
|
/** @var array<int,mixed> */ |
55
|
|
|
private $attributes = []; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Gets allowed methods |
59
|
|
|
* |
60
|
|
|
* @return string[] |
61
|
|
|
*/ |
62
|
1 |
|
public function getAllowedMethods(): array |
63
|
|
|
{ |
64
|
1 |
|
$methods = []; |
65
|
|
|
|
66
|
1 |
|
foreach ($this->getCollection()->getRoutes() as $route) { |
|
|
|
|
67
|
1 |
|
foreach ($route->getMethods() as $method) { |
68
|
1 |
|
$methods[$method] = true; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return \array_keys($methods); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Gets a route for the given name |
77
|
|
|
* |
78
|
|
|
* @param string $name |
79
|
|
|
* |
80
|
|
|
* @throws RouteNotFoundException |
81
|
|
|
* |
82
|
|
|
* @return RouteInterface |
83
|
|
|
*/ |
84
|
6 |
|
public function getRoute(string $name): RouteInterface |
85
|
|
|
{ |
86
|
6 |
|
if (!isset($this->routes[$name])) { |
87
|
2 |
|
throw new RouteNotFoundException(\sprintf('No route found for the name "%s".', $name)); |
88
|
|
|
} |
89
|
|
|
|
90
|
4 |
|
return $this->routes[$name]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the profiled routes |
95
|
|
|
* |
96
|
|
|
* @return null|DebugRoute |
97
|
|
|
*/ |
98
|
1 |
|
public function getProfile(): ?DebugRoute |
99
|
|
|
{ |
100
|
1 |
|
return $this->debug; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set profiling for routes |
105
|
|
|
*/ |
106
|
1 |
|
public function setProfile(): void |
107
|
|
|
{ |
108
|
1 |
|
$this->debug = new DebugRoute(); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set Namespace for route handlers/controllers |
113
|
|
|
* |
114
|
|
|
* @param string $namespace |
115
|
|
|
*/ |
116
|
4 |
|
public function setNamespace(string $namespace): void |
117
|
|
|
{ |
118
|
4 |
|
$this->namespace = \rtrim($namespace, '\\/') . '\\'; |
119
|
4 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Adds parameters. |
123
|
|
|
* |
124
|
|
|
* This method implements a fluent interface. |
125
|
|
|
* |
126
|
|
|
* @param array<string,mixed> $parameters The parameters |
127
|
|
|
* @param int $type |
128
|
|
|
*/ |
129
|
5 |
|
public function addParameters(array $parameters, int $type = Router::TYPE_REQUIREMENT): void |
130
|
|
|
{ |
131
|
5 |
|
foreach ($parameters as $key => $regex) { |
132
|
5 |
|
if (Router::TYPE_DEFAULT === $type) { |
133
|
3 |
|
$this->attributes[Router::TYPE_DEFAULT] = [$key => $regex]; |
134
|
|
|
|
135
|
3 |
|
continue; |
136
|
5 |
|
} elseif (Router::TYPE_CACHE === $type) { |
137
|
2 |
|
$this->attributes[Router::TYPE_CACHE] = $regex; |
138
|
|
|
|
139
|
2 |
|
continue; |
140
|
|
|
} |
141
|
|
|
|
142
|
3 |
|
$this->attributes[Router::TYPE_REQUIREMENT] = [$key => $regex]; |
143
|
|
|
} |
144
|
5 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Adds the given route(s) listener to the router |
148
|
|
|
* |
149
|
|
|
* @param RouteListenerInterface ...$listeners |
150
|
|
|
*/ |
151
|
1 |
|
public function addRouteListener(RouteListenerInterface ...$listeners): void |
152
|
|
|
{ |
153
|
1 |
|
foreach ($listeners as $listener) { |
154
|
1 |
|
$this->listeners[] = $listener; |
155
|
|
|
} |
156
|
1 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Load routes from annotation. |
160
|
|
|
* |
161
|
|
|
* @param LoaderInterface $loader |
162
|
|
|
*/ |
163
|
14 |
|
public function loadAnnotation(LoaderInterface $loader): void |
164
|
|
|
{ |
165
|
14 |
|
foreach ($loader->load() as $annotation) { |
166
|
4 |
|
if ($annotation instanceof RouteListInterface) { |
167
|
4 |
|
$this->addRoute(...$annotation->getRoutes()); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
} |
170
|
4 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get merged default parameters. |
174
|
|
|
* |
175
|
|
|
* @param RouteInterface $route |
176
|
|
|
* |
177
|
|
|
* @return array<string,string> Merged default parameters |
178
|
|
|
*/ |
179
|
43 |
|
private function mergeDefaults(RouteInterface $route): array |
180
|
|
|
{ |
181
|
43 |
|
$defaults = $route->getDefaults(); |
182
|
|
|
|
183
|
43 |
|
foreach ($route->getArguments() as $key => $value) { |
184
|
6 |
|
if (!isset($defaults[$key]) || null !== $value) { |
185
|
6 |
|
$defaults[$key] = $value; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
43 |
|
return $defaults; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Merge Router attributes in route default and patterns. |
194
|
|
|
* |
195
|
|
|
* @param RouteInterface $route |
196
|
|
|
* |
197
|
|
|
* @return RouteInterface |
198
|
|
|
*/ |
199
|
62 |
|
private function mergeAttributes(RouteInterface $route): RouteInterface |
200
|
|
|
{ |
201
|
62 |
|
foreach ($this->attributes as $type => $attributes) { |
202
|
5 |
|
if (Router::TYPE_CACHE === $type) { |
203
|
2 |
|
continue; |
204
|
|
|
} |
205
|
|
|
|
206
|
3 |
|
if (Router::TYPE_DEFAULT === $type) { |
207
|
3 |
|
$route->setDefaults($attributes); |
208
|
|
|
|
209
|
3 |
|
continue; |
210
|
|
|
} |
211
|
|
|
|
212
|
3 |
|
$route->setPatterns($attributes); |
213
|
|
|
} |
214
|
|
|
|
215
|
62 |
|
return $route; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|