@@ -27,17 +27,17 @@ |
||
27 | 27 | */ |
28 | 28 | function path_match(string $path, string $subject, &$attributes = []) : bool |
29 | 29 | { |
30 | - $regex = path_regex($path); |
|
31 | - if (!preg_match($regex, $subject, $matches)) { |
|
32 | - return false; |
|
33 | - } |
|
30 | + $regex = path_regex($path); |
|
31 | + if (!preg_match($regex, $subject, $matches)) { |
|
32 | + return false; |
|
33 | + } |
|
34 | 34 | |
35 | - $attributes = []; |
|
36 | - foreach ($matches as $key => $value) { |
|
37 | - if (!is_int($key) && '' !== $value) { |
|
38 | - $attributes[$key] = $value; |
|
39 | - } |
|
40 | - } |
|
35 | + $attributes = []; |
|
36 | + foreach ($matches as $key => $value) { |
|
37 | + if (!is_int($key) && '' !== $value) { |
|
38 | + $attributes[$key] = $value; |
|
39 | + } |
|
40 | + } |
|
41 | 41 | |
42 | - return true; |
|
42 | + return true; |
|
43 | 43 | } |
@@ -36,25 +36,25 @@ |
||
36 | 36 | */ |
37 | 37 | function path_build(string $path, array $attributes = [], bool $strict = false) : string |
38 | 38 | { |
39 | - $regex = '/{([0-9A-Za-z_]+)(?:<([^<#>]+)>)?}/'; |
|
40 | - |
|
41 | - return preg_replace_callback($regex, function ($match) use ($path, $attributes, $strict) { |
|
42 | - if (!isset($attributes[$match[1]])) { |
|
43 | - throw new InvalidArgumentException( |
|
44 | - sprintf('[%s] missing attribute "%s".', $path, $match[1]) |
|
45 | - ); |
|
46 | - } |
|
47 | - |
|
48 | - $attributes[$match[1]] = (string) $attributes[$match[1]]; |
|
49 | - |
|
50 | - if ($strict && isset($match[2])) { |
|
51 | - if (!preg_match('#' . $match[2] . '#u', $attributes[$match[1]])) { |
|
52 | - throw new InvalidArgumentException( |
|
53 | - sprintf('[%s] "%s" must match "%s".', $path, $match[1], $match[2]) |
|
54 | - ); |
|
55 | - } |
|
56 | - } |
|
57 | - |
|
58 | - return $attributes[$match[1]]; |
|
59 | - }, $path); |
|
39 | + $regex = '/{([0-9A-Za-z_]+)(?:<([^<#>]+)>)?}/'; |
|
40 | + |
|
41 | + return preg_replace_callback($regex, function ($match) use ($path, $attributes, $strict) { |
|
42 | + if (!isset($attributes[$match[1]])) { |
|
43 | + throw new InvalidArgumentException( |
|
44 | + sprintf('[%s] missing attribute "%s".', $path, $match[1]) |
|
45 | + ); |
|
46 | + } |
|
47 | + |
|
48 | + $attributes[$match[1]] = (string) $attributes[$match[1]]; |
|
49 | + |
|
50 | + if ($strict && isset($match[2])) { |
|
51 | + if (!preg_match('#' . $match[2] . '#u', $attributes[$match[1]])) { |
|
52 | + throw new InvalidArgumentException( |
|
53 | + sprintf('[%s] "%s" must match "%s".', $path, $match[1], $match[2]) |
|
54 | + ); |
|
55 | + } |
|
56 | + } |
|
57 | + |
|
58 | + return $attributes[$match[1]]; |
|
59 | + }, $path); |
|
60 | 60 | } |
@@ -38,7 +38,7 @@ |
||
38 | 38 | { |
39 | 39 | $regex = '/{([0-9A-Za-z_]+)(?:<([^<#>]+)>)?}/'; |
40 | 40 | |
41 | - return preg_replace_callback($regex, function ($match) use ($path, $attributes, $strict) { |
|
41 | + return preg_replace_callback($regex, function($match) use ($path, $attributes, $strict) { |
|
42 | 42 | if (!isset($attributes[$match[1]])) { |
43 | 43 | throw new InvalidArgumentException( |
44 | 44 | sprintf('[%s] missing attribute "%s".', $path, $match[1]) |
@@ -34,21 +34,21 @@ |
||
34 | 34 | */ |
35 | 35 | function path_regex(string $path) : string |
36 | 36 | { |
37 | - preg_match_all('/{([0-9A-Za-z_]+)(?:<([^<#>]+)>)?}/', $path, $matches, PREG_SET_ORDER); |
|
37 | + preg_match_all('/{([0-9A-Za-z_]+)(?:<([^<#>]+)>)?}/', $path, $matches, PREG_SET_ORDER); |
|
38 | 38 | |
39 | - foreach ($matches as $match) { |
|
40 | - $path = str_replace($match[0], '{' . $match[1] . '}', $path); |
|
41 | - } |
|
39 | + foreach ($matches as $match) { |
|
40 | + $path = str_replace($match[0], '{' . $match[1] . '}', $path); |
|
41 | + } |
|
42 | 42 | |
43 | - $path = addcslashes($path, '#$*+-.?[\]^|'); |
|
44 | - $path = str_replace(['(', ')'], ['(?:', ')?'], $path); |
|
43 | + $path = addcslashes($path, '#$*+-.?[\]^|'); |
|
44 | + $path = str_replace(['(', ')'], ['(?:', ')?'], $path); |
|
45 | 45 | |
46 | - foreach ($matches as $match) { |
|
47 | - $pattern = $match[2] ?? '[^/]+'; |
|
48 | - $subpattern = '(?<' . $match[1] . '>' . $pattern . ')'; |
|
46 | + foreach ($matches as $match) { |
|
47 | + $pattern = $match[2] ?? '[^/]+'; |
|
48 | + $subpattern = '(?<' . $match[1] . '>' . $pattern . ')'; |
|
49 | 49 | |
50 | - $path = str_replace('{' . $match[1] . '}', $subpattern, $path); |
|
51 | - } |
|
50 | + $path = str_replace('{' . $match[1] . '}', $subpattern, $path); |
|
51 | + } |
|
52 | 52 | |
53 | - return '#^' . $path . '$#uD'; |
|
53 | + return '#^' . $path . '$#uD'; |
|
54 | 54 | } |
@@ -23,297 +23,297 @@ |
||
23 | 23 | class AnnotationRouteLoaderTest extends TestCase |
24 | 24 | { |
25 | 25 | |
26 | - /** |
|
27 | - * @return void |
|
28 | - */ |
|
29 | - public static function setUpBeforeClass() : void |
|
30 | - { |
|
31 | - if (!class_exists('Route')) { |
|
32 | - class_alias(AnnotationRoute::class, 'Route'); |
|
33 | - } |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * @return void |
|
38 | - */ |
|
39 | - public function testAnnotationRouteNameMissing() : void |
|
40 | - { |
|
41 | - $this->expectException(InvalidArgumentException::class); |
|
42 | - $this->expectExceptionMessage('@Route.name must be not an empty string.'); |
|
43 | - |
|
44 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
45 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteNameMissing'); |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * @return void |
|
50 | - */ |
|
51 | - public function testAnnotationRouteNameEmpty() : void |
|
52 | - { |
|
53 | - $this->expectException(InvalidArgumentException::class); |
|
54 | - $this->expectExceptionMessage('@Route.name must be not an empty string.'); |
|
55 | - |
|
56 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
57 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteNameEmpty'); |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * @return void |
|
62 | - */ |
|
63 | - public function testAnnotationRouteNameNotString() : void |
|
64 | - { |
|
65 | - $this->expectException(InvalidArgumentException::class); |
|
66 | - $this->expectExceptionMessage('@Route.name must be not an empty string.'); |
|
67 | - |
|
68 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
69 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteNameNotString'); |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * @return void |
|
74 | - */ |
|
75 | - public function testAnnotationRoutePathMissing() : void |
|
76 | - { |
|
77 | - $this->expectException(InvalidArgumentException::class); |
|
78 | - $this->expectExceptionMessage('@Route.path must be not an empty string.'); |
|
79 | - |
|
80 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
81 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePathMissing'); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * @return void |
|
86 | - */ |
|
87 | - public function testAnnotationRoutePathEmpty() : void |
|
88 | - { |
|
89 | - $this->expectException(InvalidArgumentException::class); |
|
90 | - $this->expectExceptionMessage('@Route.path must be not an empty string.'); |
|
91 | - |
|
92 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
93 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePathEmpty'); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @return void |
|
98 | - */ |
|
99 | - public function testAnnotationRoutePathNotString() : void |
|
100 | - { |
|
101 | - $this->expectException(InvalidArgumentException::class); |
|
102 | - $this->expectExceptionMessage('@Route.path must be not an empty string.'); |
|
103 | - |
|
104 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
105 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePathNotString'); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * @return void |
|
110 | - */ |
|
111 | - public function testAnnotationRouteMethodsMissing() : void |
|
112 | - { |
|
113 | - $this->expectException(InvalidArgumentException::class); |
|
114 | - $this->expectExceptionMessage('@Route.methods must be not an empty array.'); |
|
115 | - |
|
116 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
117 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsMissing'); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @return void |
|
122 | - */ |
|
123 | - public function testAnnotationRouteMethodsEmpty() : void |
|
124 | - { |
|
125 | - $this->expectException(InvalidArgumentException::class); |
|
126 | - $this->expectExceptionMessage('@Route.methods must be not an empty array.'); |
|
127 | - |
|
128 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
129 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsEmpty'); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * @return void |
|
134 | - */ |
|
135 | - public function testAnnotationRouteMethodsNotArray() : void |
|
136 | - { |
|
137 | - $this->expectException(InvalidArgumentException::class); |
|
138 | - $this->expectExceptionMessage('@Route.methods must be not an empty array.'); |
|
139 | - |
|
140 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
141 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsNotArray'); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * @return void |
|
146 | - */ |
|
147 | - public function testAnnotationRouteMethodsNotStringable() : void |
|
148 | - { |
|
149 | - $this->expectException(InvalidArgumentException::class); |
|
150 | - $this->expectExceptionMessage('@Route.methods must contain only strings.'); |
|
151 | - |
|
152 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
153 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsNotStringable'); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * @return void |
|
158 | - */ |
|
159 | - public function testAnnotationRouteMiddlewaresNotArray() : void |
|
160 | - { |
|
161 | - $this->expectException(InvalidArgumentException::class); |
|
162 | - $this->expectExceptionMessage('@Route.middlewares must be an array.'); |
|
163 | - |
|
164 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
165 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresNotArray'); |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * @return void |
|
170 | - */ |
|
171 | - public function testAnnotationRouteMiddlewaresNotStringable() : void |
|
172 | - { |
|
173 | - $this->expectException(InvalidArgumentException::class); |
|
174 | - $this->expectExceptionMessage('@Route.middlewares must contain only middlewares.'); |
|
175 | - |
|
176 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
177 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresNotStringable'); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @return void |
|
182 | - */ |
|
183 | - public function testAnnotationRouteMiddlewaresContainNonexistenceClass() : void |
|
184 | - { |
|
185 | - $this->expectException(InvalidArgumentException::class); |
|
186 | - $this->expectExceptionMessage('@Route.middlewares must contain only middlewares.'); |
|
187 | - |
|
188 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
189 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresContainNonexistenceClass'); |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * @return void |
|
194 | - */ |
|
195 | - public function testAnnotationRouteMiddlewaresContainNotMiddleware() : void |
|
196 | - { |
|
197 | - $this->expectException(InvalidArgumentException::class); |
|
198 | - $this->expectExceptionMessage('@Route.middlewares must contain only middlewares.'); |
|
199 | - |
|
200 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
201 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresContainNotMiddleware'); |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * @return void |
|
206 | - */ |
|
207 | - public function testAnnotationRouteAttributesNotArray() : void |
|
208 | - { |
|
209 | - $this->expectException(InvalidArgumentException::class); |
|
210 | - $this->expectExceptionMessage('@Route.attributes must be an array.'); |
|
211 | - |
|
212 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
213 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteAttributesNotArray'); |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * @return void |
|
218 | - */ |
|
219 | - public function testAnnotationRoutePriorityNotInteger() : void |
|
220 | - { |
|
221 | - $this->expectException(InvalidArgumentException::class); |
|
222 | - $this->expectExceptionMessage('@Route.priority must be an integer.'); |
|
223 | - |
|
224 | - $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
225 | - (new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePriorityNotInteger'); |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * @return void |
|
230 | - * |
|
231 | - * @todo This test needs to be improved... |
|
232 | - */ |
|
233 | - public function testBuildRoutes() : void |
|
234 | - { |
|
235 | - $loader = new AnnotationRouteLoader(); |
|
236 | - $loader->discover(__DIR__ . '/../Fixture/Annotation/AnnotationRouteValid'); |
|
237 | - |
|
238 | - $routesMap = []; |
|
239 | - $builtRoutes = $loader->buildRoutes(); |
|
240 | - |
|
241 | - foreach ($builtRoutes as $i => $route) { |
|
242 | - $routesMap[$i]['name'] = $route->getName(); |
|
243 | - $routesMap[$i]['path'] = $route->getPath(); |
|
244 | - $routesMap[$i]['methods'] = $route->getMethods(); |
|
245 | - $routesMap[$i]['requestHandler'] = get_class($route->getRequestHandler()); |
|
246 | - $routesMap[$i]['middlewares'] = []; |
|
247 | - $routesMap[$i]['attributes'] = $route->getAttributes(); |
|
248 | - |
|
249 | - foreach ($route->getMiddlewares() as $middleware) { |
|
250 | - $routesMap[$i]['middlewares'][] = get_class($middleware); |
|
251 | - } |
|
252 | - } |
|
253 | - |
|
254 | - $expectedMap = [ |
|
255 | - [ |
|
256 | - 'name' => 'quuuux', |
|
257 | - 'path' => '/quuuux', |
|
258 | - 'methods' => ['PATCH', 'DELETE'], |
|
259 | - 'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
|
260 | - '\AnnotationRouteValid\Subdirectory\QuuuuxRequestHandler', |
|
261 | - 'middlewares' => [ |
|
262 | - 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
263 | - 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
264 | - ], |
|
265 | - 'attributes' => [ |
|
266 | - 'foo' => 'bar', |
|
267 | - 'source' => 'quuuux', |
|
268 | - ], |
|
269 | - ], |
|
270 | - [ |
|
271 | - 'name' => 'quuux', |
|
272 | - 'path' => '/quuux', |
|
273 | - 'methods' => ['PUT', 'PATCH'], |
|
274 | - 'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
|
275 | - '\AnnotationRouteValid\Subdirectory\QuuuxRequestHandler', |
|
276 | - 'middlewares' => [ |
|
277 | - 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
278 | - 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
279 | - ], |
|
280 | - 'attributes' => [ |
|
281 | - 'foo' => 'bar', |
|
282 | - 'source' => 'quuux', |
|
283 | - ], |
|
284 | - ], |
|
285 | - [ |
|
286 | - 'name' => 'quux', |
|
287 | - 'path' => '/quux', |
|
288 | - 'methods' => ['POST', 'PUT'], |
|
289 | - 'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
|
290 | - '\AnnotationRouteValid\QuuxRequestHandler', |
|
291 | - 'middlewares' => [ |
|
292 | - 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
293 | - 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
294 | - ], |
|
295 | - 'attributes' => [ |
|
296 | - 'foo' => 'bar', |
|
297 | - 'source' => 'quux', |
|
298 | - ], |
|
299 | - ], |
|
300 | - [ |
|
301 | - 'name' => 'qux', |
|
302 | - 'path' => '/qux', |
|
303 | - 'methods' => ['GET', 'POST'], |
|
304 | - 'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
|
305 | - '\AnnotationRouteValid\QuxRequestHandler', |
|
306 | - 'middlewares' => [ |
|
307 | - 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
308 | - 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
309 | - ], |
|
310 | - 'attributes' => [ |
|
311 | - 'foo' => 'bar', |
|
312 | - 'source' => 'qux', |
|
313 | - ], |
|
314 | - ], |
|
315 | - ]; |
|
316 | - |
|
317 | - $this->assertSame($expectedMap, $routesMap); |
|
318 | - } |
|
26 | + /** |
|
27 | + * @return void |
|
28 | + */ |
|
29 | + public static function setUpBeforeClass() : void |
|
30 | + { |
|
31 | + if (!class_exists('Route')) { |
|
32 | + class_alias(AnnotationRoute::class, 'Route'); |
|
33 | + } |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * @return void |
|
38 | + */ |
|
39 | + public function testAnnotationRouteNameMissing() : void |
|
40 | + { |
|
41 | + $this->expectException(InvalidArgumentException::class); |
|
42 | + $this->expectExceptionMessage('@Route.name must be not an empty string.'); |
|
43 | + |
|
44 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
45 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteNameMissing'); |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * @return void |
|
50 | + */ |
|
51 | + public function testAnnotationRouteNameEmpty() : void |
|
52 | + { |
|
53 | + $this->expectException(InvalidArgumentException::class); |
|
54 | + $this->expectExceptionMessage('@Route.name must be not an empty string.'); |
|
55 | + |
|
56 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
57 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteNameEmpty'); |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * @return void |
|
62 | + */ |
|
63 | + public function testAnnotationRouteNameNotString() : void |
|
64 | + { |
|
65 | + $this->expectException(InvalidArgumentException::class); |
|
66 | + $this->expectExceptionMessage('@Route.name must be not an empty string.'); |
|
67 | + |
|
68 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
69 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteNameNotString'); |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * @return void |
|
74 | + */ |
|
75 | + public function testAnnotationRoutePathMissing() : void |
|
76 | + { |
|
77 | + $this->expectException(InvalidArgumentException::class); |
|
78 | + $this->expectExceptionMessage('@Route.path must be not an empty string.'); |
|
79 | + |
|
80 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
81 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePathMissing'); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * @return void |
|
86 | + */ |
|
87 | + public function testAnnotationRoutePathEmpty() : void |
|
88 | + { |
|
89 | + $this->expectException(InvalidArgumentException::class); |
|
90 | + $this->expectExceptionMessage('@Route.path must be not an empty string.'); |
|
91 | + |
|
92 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
93 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePathEmpty'); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @return void |
|
98 | + */ |
|
99 | + public function testAnnotationRoutePathNotString() : void |
|
100 | + { |
|
101 | + $this->expectException(InvalidArgumentException::class); |
|
102 | + $this->expectExceptionMessage('@Route.path must be not an empty string.'); |
|
103 | + |
|
104 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
105 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePathNotString'); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * @return void |
|
110 | + */ |
|
111 | + public function testAnnotationRouteMethodsMissing() : void |
|
112 | + { |
|
113 | + $this->expectException(InvalidArgumentException::class); |
|
114 | + $this->expectExceptionMessage('@Route.methods must be not an empty array.'); |
|
115 | + |
|
116 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
117 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsMissing'); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @return void |
|
122 | + */ |
|
123 | + public function testAnnotationRouteMethodsEmpty() : void |
|
124 | + { |
|
125 | + $this->expectException(InvalidArgumentException::class); |
|
126 | + $this->expectExceptionMessage('@Route.methods must be not an empty array.'); |
|
127 | + |
|
128 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
129 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsEmpty'); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * @return void |
|
134 | + */ |
|
135 | + public function testAnnotationRouteMethodsNotArray() : void |
|
136 | + { |
|
137 | + $this->expectException(InvalidArgumentException::class); |
|
138 | + $this->expectExceptionMessage('@Route.methods must be not an empty array.'); |
|
139 | + |
|
140 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
141 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsNotArray'); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * @return void |
|
146 | + */ |
|
147 | + public function testAnnotationRouteMethodsNotStringable() : void |
|
148 | + { |
|
149 | + $this->expectException(InvalidArgumentException::class); |
|
150 | + $this->expectExceptionMessage('@Route.methods must contain only strings.'); |
|
151 | + |
|
152 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
153 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMethodsNotStringable'); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * @return void |
|
158 | + */ |
|
159 | + public function testAnnotationRouteMiddlewaresNotArray() : void |
|
160 | + { |
|
161 | + $this->expectException(InvalidArgumentException::class); |
|
162 | + $this->expectExceptionMessage('@Route.middlewares must be an array.'); |
|
163 | + |
|
164 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
165 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresNotArray'); |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * @return void |
|
170 | + */ |
|
171 | + public function testAnnotationRouteMiddlewaresNotStringable() : void |
|
172 | + { |
|
173 | + $this->expectException(InvalidArgumentException::class); |
|
174 | + $this->expectExceptionMessage('@Route.middlewares must contain only middlewares.'); |
|
175 | + |
|
176 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
177 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresNotStringable'); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @return void |
|
182 | + */ |
|
183 | + public function testAnnotationRouteMiddlewaresContainNonexistenceClass() : void |
|
184 | + { |
|
185 | + $this->expectException(InvalidArgumentException::class); |
|
186 | + $this->expectExceptionMessage('@Route.middlewares must contain only middlewares.'); |
|
187 | + |
|
188 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
189 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresContainNonexistenceClass'); |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * @return void |
|
194 | + */ |
|
195 | + public function testAnnotationRouteMiddlewaresContainNotMiddleware() : void |
|
196 | + { |
|
197 | + $this->expectException(InvalidArgumentException::class); |
|
198 | + $this->expectExceptionMessage('@Route.middlewares must contain only middlewares.'); |
|
199 | + |
|
200 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
201 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteMiddlewaresContainNotMiddleware'); |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * @return void |
|
206 | + */ |
|
207 | + public function testAnnotationRouteAttributesNotArray() : void |
|
208 | + { |
|
209 | + $this->expectException(InvalidArgumentException::class); |
|
210 | + $this->expectExceptionMessage('@Route.attributes must be an array.'); |
|
211 | + |
|
212 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
213 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRouteAttributesNotArray'); |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * @return void |
|
218 | + */ |
|
219 | + public function testAnnotationRoutePriorityNotInteger() : void |
|
220 | + { |
|
221 | + $this->expectException(InvalidArgumentException::class); |
|
222 | + $this->expectExceptionMessage('@Route.priority must be an integer.'); |
|
223 | + |
|
224 | + $root = __DIR__ . '/../Fixture/Annotation/AnnotationRouteInvalid'; |
|
225 | + (new AnnotationRouteLoader)->discover($root . '/AnnotationRoutePriorityNotInteger'); |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * @return void |
|
230 | + * |
|
231 | + * @todo This test needs to be improved... |
|
232 | + */ |
|
233 | + public function testBuildRoutes() : void |
|
234 | + { |
|
235 | + $loader = new AnnotationRouteLoader(); |
|
236 | + $loader->discover(__DIR__ . '/../Fixture/Annotation/AnnotationRouteValid'); |
|
237 | + |
|
238 | + $routesMap = []; |
|
239 | + $builtRoutes = $loader->buildRoutes(); |
|
240 | + |
|
241 | + foreach ($builtRoutes as $i => $route) { |
|
242 | + $routesMap[$i]['name'] = $route->getName(); |
|
243 | + $routesMap[$i]['path'] = $route->getPath(); |
|
244 | + $routesMap[$i]['methods'] = $route->getMethods(); |
|
245 | + $routesMap[$i]['requestHandler'] = get_class($route->getRequestHandler()); |
|
246 | + $routesMap[$i]['middlewares'] = []; |
|
247 | + $routesMap[$i]['attributes'] = $route->getAttributes(); |
|
248 | + |
|
249 | + foreach ($route->getMiddlewares() as $middleware) { |
|
250 | + $routesMap[$i]['middlewares'][] = get_class($middleware); |
|
251 | + } |
|
252 | + } |
|
253 | + |
|
254 | + $expectedMap = [ |
|
255 | + [ |
|
256 | + 'name' => 'quuuux', |
|
257 | + 'path' => '/quuuux', |
|
258 | + 'methods' => ['PATCH', 'DELETE'], |
|
259 | + 'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
|
260 | + '\AnnotationRouteValid\Subdirectory\QuuuuxRequestHandler', |
|
261 | + 'middlewares' => [ |
|
262 | + 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
263 | + 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
264 | + ], |
|
265 | + 'attributes' => [ |
|
266 | + 'foo' => 'bar', |
|
267 | + 'source' => 'quuuux', |
|
268 | + ], |
|
269 | + ], |
|
270 | + [ |
|
271 | + 'name' => 'quuux', |
|
272 | + 'path' => '/quuux', |
|
273 | + 'methods' => ['PUT', 'PATCH'], |
|
274 | + 'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
|
275 | + '\AnnotationRouteValid\Subdirectory\QuuuxRequestHandler', |
|
276 | + 'middlewares' => [ |
|
277 | + 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
278 | + 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
279 | + ], |
|
280 | + 'attributes' => [ |
|
281 | + 'foo' => 'bar', |
|
282 | + 'source' => 'quuux', |
|
283 | + ], |
|
284 | + ], |
|
285 | + [ |
|
286 | + 'name' => 'quux', |
|
287 | + 'path' => '/quux', |
|
288 | + 'methods' => ['POST', 'PUT'], |
|
289 | + 'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
|
290 | + '\AnnotationRouteValid\QuuxRequestHandler', |
|
291 | + 'middlewares' => [ |
|
292 | + 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
293 | + 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
294 | + ], |
|
295 | + 'attributes' => [ |
|
296 | + 'foo' => 'bar', |
|
297 | + 'source' => 'quux', |
|
298 | + ], |
|
299 | + ], |
|
300 | + [ |
|
301 | + 'name' => 'qux', |
|
302 | + 'path' => '/qux', |
|
303 | + 'methods' => ['GET', 'POST'], |
|
304 | + 'requestHandler' => 'Sunrise\Http\Router\Tests\Fixture\Annotation' . |
|
305 | + '\AnnotationRouteValid\QuxRequestHandler', |
|
306 | + 'middlewares' => [ |
|
307 | + 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
308 | + 'Sunrise\Http\Router\Tests\Fixture\BlankMiddleware', |
|
309 | + ], |
|
310 | + 'attributes' => [ |
|
311 | + 'foo' => 'bar', |
|
312 | + 'source' => 'qux', |
|
313 | + ], |
|
314 | + ], |
|
315 | + ]; |
|
316 | + |
|
317 | + $this->assertSame($expectedMap, $routesMap); |
|
318 | + } |
|
319 | 319 | } |
@@ -34,94 +34,94 @@ |
||
34 | 34 | class Router extends RouteCollection implements MiddlewareInterface, RequestHandlerInterface |
35 | 35 | { |
36 | 36 | |
37 | - /** |
|
38 | - * Server Request attribute name for routing error instance |
|
39 | - * |
|
40 | - * @var string |
|
41 | - */ |
|
42 | - public const ATTR_NAME_FOR_ROUTING_ERROR = '@routing-error'; |
|
43 | - |
|
44 | - /** |
|
45 | - * Gets a route for the given name |
|
46 | - * |
|
47 | - * @param string $name |
|
48 | - * |
|
49 | - * @return RouteInterface |
|
50 | - * |
|
51 | - * @throws RouteNotFoundException |
|
52 | - */ |
|
53 | - public function getRoute(string $name) : RouteInterface |
|
54 | - { |
|
55 | - foreach ($this->getRoutes() as $route) { |
|
56 | - if ($name === $route->getName()) { |
|
57 | - return $route; |
|
58 | - } |
|
59 | - } |
|
60 | - |
|
61 | - throw new RouteNotFoundException(); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Looks for a route that matches the given request |
|
66 | - * |
|
67 | - * @param ServerRequestInterface $request |
|
68 | - * |
|
69 | - * @return RouteInterface |
|
70 | - * |
|
71 | - * @throws MethodNotAllowedException |
|
72 | - * @throws RouteNotFoundException |
|
73 | - * |
|
74 | - * @todo Matching by strategy for detecting verbs... |
|
75 | - */ |
|
76 | - public function match(ServerRequestInterface $request) : RouteInterface |
|
77 | - { |
|
78 | - $routes = []; |
|
79 | - foreach ($this->getRoutes() as $route) { |
|
80 | - foreach ($route->getMethods() as $method) { |
|
81 | - $routes[$method][] = $route; |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - $method = $request->getMethod(); |
|
86 | - if (!isset($routes[$method])) { |
|
87 | - throw new MethodNotAllowedException( |
|
88 | - array_keys($routes) |
|
89 | - ); |
|
90 | - } |
|
91 | - |
|
92 | - $target = $request->getUri()->getPath(); |
|
93 | - foreach ($routes[$method] as $route) { |
|
94 | - if (path_match($route->getPath(), $target, $attributes)) { |
|
95 | - return $route->withAddedAttributes($attributes); |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - throw new RouteNotFoundException(); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * {@inheritDoc} |
|
104 | - */ |
|
105 | - public function handle(ServerRequestInterface $request) : ResponseInterface |
|
106 | - { |
|
107 | - $route = $this->match($request); |
|
108 | - |
|
109 | - $requestHandler = new RoutableRequestHandler($route); |
|
110 | - |
|
111 | - return $requestHandler->handle($request); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * {@inheritDoc} |
|
116 | - */ |
|
117 | - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface |
|
118 | - { |
|
119 | - try { |
|
120 | - return $this->handle($request); |
|
121 | - } catch (ExceptionInterface $e) { |
|
122 | - return $handler->handle( |
|
123 | - $request->withAttribute(self::ATTR_NAME_FOR_ROUTING_ERROR, $e) |
|
124 | - ); |
|
125 | - } |
|
126 | - } |
|
37 | + /** |
|
38 | + * Server Request attribute name for routing error instance |
|
39 | + * |
|
40 | + * @var string |
|
41 | + */ |
|
42 | + public const ATTR_NAME_FOR_ROUTING_ERROR = '@routing-error'; |
|
43 | + |
|
44 | + /** |
|
45 | + * Gets a route for the given name |
|
46 | + * |
|
47 | + * @param string $name |
|
48 | + * |
|
49 | + * @return RouteInterface |
|
50 | + * |
|
51 | + * @throws RouteNotFoundException |
|
52 | + */ |
|
53 | + public function getRoute(string $name) : RouteInterface |
|
54 | + { |
|
55 | + foreach ($this->getRoutes() as $route) { |
|
56 | + if ($name === $route->getName()) { |
|
57 | + return $route; |
|
58 | + } |
|
59 | + } |
|
60 | + |
|
61 | + throw new RouteNotFoundException(); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Looks for a route that matches the given request |
|
66 | + * |
|
67 | + * @param ServerRequestInterface $request |
|
68 | + * |
|
69 | + * @return RouteInterface |
|
70 | + * |
|
71 | + * @throws MethodNotAllowedException |
|
72 | + * @throws RouteNotFoundException |
|
73 | + * |
|
74 | + * @todo Matching by strategy for detecting verbs... |
|
75 | + */ |
|
76 | + public function match(ServerRequestInterface $request) : RouteInterface |
|
77 | + { |
|
78 | + $routes = []; |
|
79 | + foreach ($this->getRoutes() as $route) { |
|
80 | + foreach ($route->getMethods() as $method) { |
|
81 | + $routes[$method][] = $route; |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + $method = $request->getMethod(); |
|
86 | + if (!isset($routes[$method])) { |
|
87 | + throw new MethodNotAllowedException( |
|
88 | + array_keys($routes) |
|
89 | + ); |
|
90 | + } |
|
91 | + |
|
92 | + $target = $request->getUri()->getPath(); |
|
93 | + foreach ($routes[$method] as $route) { |
|
94 | + if (path_match($route->getPath(), $target, $attributes)) { |
|
95 | + return $route->withAddedAttributes($attributes); |
|
96 | + } |
|
97 | + } |
|
98 | + |
|
99 | + throw new RouteNotFoundException(); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * {@inheritDoc} |
|
104 | + */ |
|
105 | + public function handle(ServerRequestInterface $request) : ResponseInterface |
|
106 | + { |
|
107 | + $route = $this->match($request); |
|
108 | + |
|
109 | + $requestHandler = new RoutableRequestHandler($route); |
|
110 | + |
|
111 | + return $requestHandler->handle($request); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * {@inheritDoc} |
|
116 | + */ |
|
117 | + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface |
|
118 | + { |
|
119 | + try { |
|
120 | + return $this->handle($request); |
|
121 | + } catch (ExceptionInterface $e) { |
|
122 | + return $handler->handle( |
|
123 | + $request->withAttribute(self::ATTR_NAME_FOR_ROUTING_ERROR, $e) |
|
124 | + ); |
|
125 | + } |
|
126 | + } |
|
127 | 127 | } |
@@ -26,56 +26,56 @@ |
||
26 | 26 | class QueueableRequestHandler implements RequestHandlerInterface |
27 | 27 | { |
28 | 28 | |
29 | - /** |
|
30 | - * The request handler queue |
|
31 | - * |
|
32 | - * @var SplQueue |
|
33 | - */ |
|
34 | - private $queue; |
|
29 | + /** |
|
30 | + * The request handler queue |
|
31 | + * |
|
32 | + * @var SplQueue |
|
33 | + */ |
|
34 | + private $queue; |
|
35 | 35 | |
36 | - /** |
|
37 | - * The request handler endpoint |
|
38 | - * |
|
39 | - * @var RequestHandlerInterface |
|
40 | - */ |
|
41 | - private $endpoint; |
|
36 | + /** |
|
37 | + * The request handler endpoint |
|
38 | + * |
|
39 | + * @var RequestHandlerInterface |
|
40 | + */ |
|
41 | + private $endpoint; |
|
42 | 42 | |
43 | - /** |
|
44 | - * Constructor of the class |
|
45 | - * |
|
46 | - * @param RequestHandlerInterface $endpoint |
|
47 | - */ |
|
48 | - public function __construct(RequestHandlerInterface $endpoint) |
|
49 | - { |
|
50 | - $this->queue = new SplQueue(); |
|
51 | - $this->endpoint = $endpoint; |
|
52 | - } |
|
43 | + /** |
|
44 | + * Constructor of the class |
|
45 | + * |
|
46 | + * @param RequestHandlerInterface $endpoint |
|
47 | + */ |
|
48 | + public function __construct(RequestHandlerInterface $endpoint) |
|
49 | + { |
|
50 | + $this->queue = new SplQueue(); |
|
51 | + $this->endpoint = $endpoint; |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Adds the given middleware(s) to the request handler queue |
|
56 | - * |
|
57 | - * @param MiddlewareInterface ...$middlewares |
|
58 | - * |
|
59 | - * @return RequestHandlerInterface |
|
60 | - */ |
|
61 | - public function add(MiddlewareInterface ...$middlewares) : RequestHandlerInterface |
|
62 | - { |
|
63 | - foreach ($middlewares as $middleware) { |
|
64 | - $this->queue->enqueue($middleware); |
|
65 | - } |
|
54 | + /** |
|
55 | + * Adds the given middleware(s) to the request handler queue |
|
56 | + * |
|
57 | + * @param MiddlewareInterface ...$middlewares |
|
58 | + * |
|
59 | + * @return RequestHandlerInterface |
|
60 | + */ |
|
61 | + public function add(MiddlewareInterface ...$middlewares) : RequestHandlerInterface |
|
62 | + { |
|
63 | + foreach ($middlewares as $middleware) { |
|
64 | + $this->queue->enqueue($middleware); |
|
65 | + } |
|
66 | 66 | |
67 | - return $this; |
|
68 | - } |
|
67 | + return $this; |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * {@inheritDoc} |
|
72 | - */ |
|
73 | - public function handle(ServerRequestInterface $request) : ResponseInterface |
|
74 | - { |
|
75 | - if (!$this->queue->isEmpty()) { |
|
76 | - return $this->queue->dequeue()->process($request, $this); |
|
77 | - } |
|
70 | + /** |
|
71 | + * {@inheritDoc} |
|
72 | + */ |
|
73 | + public function handle(ServerRequestInterface $request) : ResponseInterface |
|
74 | + { |
|
75 | + if (!$this->queue->isEmpty()) { |
|
76 | + return $this->queue->dequeue()->process($request, $this); |
|
77 | + } |
|
78 | 78 | |
79 | - return $this->endpoint->handle($request); |
|
80 | - } |
|
79 | + return $this->endpoint->handle($request); |
|
80 | + } |
|
81 | 81 | } |