@@ -19,7 +19,7 @@ |
||
19 | 19 | class PreconditionFailedException extends HttpExceptionAbstract |
20 | 20 | { |
21 | 21 | |
22 | - protected $code = 412; |
|
23 | - protected $message = "Precondition Failed"; |
|
22 | + protected $code = 412; |
|
23 | + protected $message = "Precondition Failed"; |
|
24 | 24 | |
25 | 25 | } |
@@ -19,7 +19,7 @@ |
||
19 | 19 | class RequestTimeOutException extends HttpExceptionAbstract |
20 | 20 | { |
21 | 21 | |
22 | - protected $code = 408; |
|
23 | - protected $message = "Request Time Out"; |
|
22 | + protected $code = 408; |
|
23 | + protected $message = "Request Time Out"; |
|
24 | 24 | |
25 | 25 | } |
@@ -19,7 +19,7 @@ |
||
19 | 19 | class ServiceUnavailableException extends HttpExceptionAbstract |
20 | 20 | { |
21 | 21 | |
22 | - protected $code = 503; |
|
23 | - protected $message = "Service Unavailable"; |
|
22 | + protected $code = 503; |
|
23 | + protected $message = "Service Unavailable"; |
|
24 | 24 | |
25 | 25 | } |
@@ -19,7 +19,7 @@ |
||
19 | 19 | class UnauthorizedException extends HttpExceptionAbstract |
20 | 20 | { |
21 | 21 | |
22 | - protected $code = 401; |
|
23 | - protected $message = "Unauthorized Exception"; |
|
22 | + protected $code = 401; |
|
23 | + protected $message = "Unauthorized Exception"; |
|
24 | 24 | |
25 | 25 | } |
@@ -19,7 +19,7 @@ |
||
19 | 19 | class UnsupportedMediaTypeException extends HttpExceptionAbstract |
20 | 20 | { |
21 | 21 | |
22 | - protected $code = 415; |
|
23 | - protected $message = "Unsupported Media Type"; |
|
22 | + protected $code = 415; |
|
23 | + protected $message = "Unsupported Media Type"; |
|
24 | 24 | |
25 | 25 | } |
@@ -19,8 +19,8 @@ |
||
19 | 19 | class MethodNotSupportedException extends \Exception |
20 | 20 | { |
21 | 21 | |
22 | - public function __construct($method) { |
|
23 | - $this->message = "The HTTP method '$method' is not supported by the route collector."; |
|
24 | - } |
|
22 | + public function __construct($method) { |
|
23 | + $this->message = "The HTTP method '$method' is not supported by the route collector."; |
|
24 | + } |
|
25 | 25 | |
26 | 26 | } |
@@ -208,7 +208,7 @@ |
||
208 | 208 | |
209 | 209 | private function parseCallableController($controller) |
210 | 210 | { |
211 | - $controller = rtrim($this->namespace, "\\") . "\\" . $this->parseCallablePlaceholders($controller); |
|
211 | + $controller = rtrim($this->namespace, "\\") . "\\" . $this->parseCallablePlaceholders($controller); |
|
212 | 212 | |
213 | 213 | if ($this->controllerCreationFunction === null) { |
214 | 214 | return new $controller; |
@@ -385,7 +385,7 @@ discard block |
||
385 | 385 | } |
386 | 386 | |
387 | 387 | /** |
388 | - * @return string|null |
|
388 | + * @return string |
|
389 | 389 | */ |
390 | 390 | |
391 | 391 | public function getStrategy() |
@@ -558,7 +558,7 @@ discard block |
||
558 | 558 | |
559 | 559 | /** |
560 | 560 | * @param string $key |
561 | - * @param mixed $value |
|
561 | + * @param string $value |
|
562 | 562 | * |
563 | 563 | * @return $this |
564 | 564 | */ |
@@ -23,626 +23,626 @@ |
||
23 | 23 | class Route |
24 | 24 | { |
25 | 25 | |
26 | - /** |
|
27 | - * @var Collector |
|
28 | - */ |
|
26 | + /** |
|
27 | + * @var Collector |
|
28 | + */ |
|
29 | 29 | |
30 | - protected $collector; |
|
30 | + protected $collector; |
|
31 | 31 | |
32 | - /** |
|
33 | - * @var string |
|
34 | - */ |
|
32 | + /** |
|
33 | + * @var string |
|
34 | + */ |
|
35 | 35 | |
36 | - protected $method; |
|
36 | + protected $method; |
|
37 | 37 | |
38 | - /** |
|
39 | - * @var string |
|
40 | - */ |
|
41 | - |
|
42 | - protected $pattern; |
|
38 | + /** |
|
39 | + * @var string |
|
40 | + */ |
|
41 | + |
|
42 | + protected $pattern; |
|
43 | 43 | |
44 | - /** |
|
45 | - * @var callable |
|
46 | - */ |
|
47 | - |
|
48 | - protected $action; |
|
49 | - |
|
50 | - /** |
|
51 | - * @var string |
|
52 | - */ |
|
53 | - |
|
54 | - protected $namespace = ""; |
|
55 | - |
|
56 | - /** |
|
57 | - * @var string[] |
|
58 | - */ |
|
59 | - |
|
60 | - protected $params = []; |
|
61 | - |
|
62 | - /** |
|
63 | - * Defaults are parameters set by the user, and don't |
|
64 | - * appear on the pattern. |
|
65 | - * |
|
66 | - * @var array |
|
67 | - */ |
|
68 | - |
|
69 | - protected $defaults = []; |
|
70 | - |
|
71 | - /** |
|
72 | - * Metadata can be set to be used on filters, dispatch strategies |
|
73 | - * or anywhere the route object is used. |
|
74 | - * |
|
75 | - * @var array |
|
76 | - */ |
|
77 | - |
|
78 | - protected $metadata = []; |
|
79 | - |
|
80 | - /** |
|
81 | - * @var string|StrategyInterface |
|
82 | - */ |
|
83 | - |
|
84 | - protected $strategy; |
|
85 | - |
|
86 | - /** |
|
87 | - * Blocked routes are dynamic routes selected to pass by the matcher. |
|
88 | - * |
|
89 | - * @var boolean |
|
90 | - */ |
|
91 | - |
|
92 | - protected $blocked = false; |
|
93 | - |
|
94 | - /** |
|
95 | - * The matcher that dispatched this route. |
|
96 | - * |
|
97 | - * @var Matcher $matcher |
|
98 | - */ |
|
99 | - |
|
100 | - protected $matcher; |
|
101 | - |
|
102 | - /** |
|
103 | - * The function used to create controllers from name. |
|
104 | - * |
|
105 | - * @var callable |
|
106 | - */ |
|
107 | - |
|
108 | - protected $controllerCreationFunction; |
|
109 | - |
|
110 | - /** |
|
111 | - * @param Collector $collector |
|
112 | - * @param string $method |
|
113 | - * @param string $pattern |
|
114 | - * @param callable $action |
|
115 | - */ |
|
116 | - |
|
117 | - public function __construct(Collector $collector, $method, $pattern, $action) |
|
118 | - { |
|
119 | - $this->collector = $collector; |
|
120 | - $this->method = $method; |
|
121 | - $this->pattern = $pattern; |
|
122 | - $this->action = $action; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Clone this route and set it into the collector. |
|
127 | - * |
|
128 | - * @return Route |
|
129 | - */ |
|
130 | - |
|
131 | - public function reset() |
|
132 | - { |
|
133 | - return $this->collector->set($this->method, $this->pattern, $this->action)->nth(0) |
|
134 | - ->setStrategy($this->strategy)->setParams($this->params) |
|
135 | - ->setDefaults($this->defaults)->setMetadataArray($this->metadata); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Remove this route from the collector. |
|
140 | - * |
|
141 | - * @return self |
|
142 | - */ |
|
143 | - |
|
144 | - public function forget() |
|
145 | - { |
|
146 | - $this->collector->forget($this->method, $this->pattern); |
|
147 | - return $this; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Execute the route action, if no strategy was provided the action |
|
152 | - * will be executed by the call_user_func PHP function. |
|
153 | - * |
|
154 | - * @param callable $container |
|
155 | - * @throws BadRouteException |
|
156 | - * @return mixed |
|
157 | - */ |
|
158 | - |
|
159 | - public function call(callable $container = null) |
|
160 | - { |
|
161 | - $this->action = $this->buildCallable($this->action, $container); |
|
162 | - |
|
163 | - if ($this->strategy === null) { |
|
164 | - return call_user_func_array($this->action, array_merge($this->defaults, $this->params)); |
|
165 | - } |
|
166 | - |
|
167 | - if (!is_object($this->strategy)) { |
|
168 | - if ($container === null) { |
|
169 | - $this->strategy = new $this->strategy; |
|
170 | - } else $this->strategy = $container($this->strategy); |
|
171 | - } |
|
172 | - |
|
173 | - return $this->callWithStrategy(); |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Seek for dynamic content in one callable. This allow to use parameters defined on pattern on callable |
|
178 | - * definition, eg. "get" "/{resource:string+}/{slug:slug+}" "{resource}::find". |
|
179 | - * |
|
180 | - * This will snakecase the resource parameter and deal with as a controller, then call the find method. |
|
181 | - * A request for "/articles/my-first-article" will execute find method of Articles controller with only |
|
182 | - * "my-first-article" as parameter. |
|
183 | - * |
|
184 | - * @param callable $callable |
|
185 | - * @param callable $container |
|
186 | - * |
|
187 | - * @return callable |
|
188 | - */ |
|
189 | - |
|
190 | - private function buildCallable($callable, $container) |
|
191 | - { |
|
192 | - if (is_string($callable) && strpos($callable, "::")) { |
|
193 | - $callable = explode("::", $callable); |
|
194 | - } |
|
195 | - |
|
196 | - if (is_array($callable)) { |
|
197 | - if (is_string($callable[0])) { |
|
198 | - $callable[0] = $this->parseCallableController($callable[0], $container); |
|
199 | - } |
|
200 | - |
|
201 | - $callable[1] = $this->parseCallablePlaceholders($callable[1]); |
|
202 | - } |
|
203 | - |
|
204 | - return $callable; |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * Get the controller object. |
|
209 | - * |
|
210 | - * @param string $controller |
|
211 | - * @param callable $container |
|
212 | - * |
|
213 | - * @return Object |
|
214 | - */ |
|
215 | - |
|
216 | - private function parseCallableController($controller, $container) |
|
217 | - { |
|
218 | - $controller = rtrim($this->namespace, "\\") . "\\" . $this->parseCallablePlaceholders($controller); |
|
219 | - |
|
220 | - if ($container === null) { |
|
221 | - return new $controller; |
|
222 | - } else return $container($controller); |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Parse and replace dynamic content on route action. |
|
227 | - * |
|
228 | - * @param string $fragment Part of callable |
|
229 | - * @return string |
|
230 | - */ |
|
231 | - |
|
232 | - private function parseCallablePlaceholders($fragment) |
|
233 | - { |
|
234 | - if (strpos($fragment, "{") !== false) { |
|
235 | - foreach ($this->params as $placeholder => $value) { |
|
236 | - if (strpos($fragment, "{" . $placeholder . "}") !== false) { |
|
237 | - $fragment = str_replace("{" . $placeholder . "}", ucwords(str_replace("-", " ", $value)), $fragment); |
|
238 | - } |
|
239 | - } |
|
240 | - } |
|
241 | - |
|
242 | - return $fragment; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Execute the route action with the given strategy. |
|
247 | - * |
|
248 | - * @throws BadRouteException |
|
249 | - * @return mixed |
|
250 | - */ |
|
251 | - |
|
252 | - private function callWithStrategy() |
|
253 | - { |
|
254 | - if ($this->strategy instanceof StrategyInterface) { |
|
255 | - if ($this->strategy instanceof MatcherAwareInterface) { |
|
256 | - $this->strategy->setMatcher($this->matcher); |
|
257 | - } |
|
258 | - |
|
259 | - return $this->strategy->call($this); |
|
260 | - } |
|
261 | - |
|
262 | - throw new BadRouteException(str_replace("%s", get_class($this->strategy), BadRouteException::BAD_STRATEGY)); |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * @return Collector |
|
267 | - */ |
|
268 | - |
|
269 | - public function getCollector() |
|
270 | - { |
|
271 | - return $this->collector; |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * @return string |
|
276 | - */ |
|
277 | - |
|
278 | - public function getMethod() |
|
279 | - { |
|
280 | - return $this->method; |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * @return string |
|
285 | - */ |
|
286 | - |
|
287 | - public function getPattern() |
|
288 | - { |
|
289 | - return $this->pattern; |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * @return string[] |
|
294 | - */ |
|
295 | - |
|
296 | - public function getSegments() |
|
297 | - { |
|
298 | - return explode("/", $this->pattern); |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * @return callable |
|
303 | - */ |
|
304 | - |
|
305 | - public function getAction() |
|
306 | - { |
|
307 | - return $this->action; |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * @return string |
|
312 | - */ |
|
313 | - |
|
314 | - public function getNamespace() |
|
315 | - { |
|
316 | - return $this->namespace; |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * @return string[] |
|
321 | - */ |
|
322 | - |
|
323 | - public function getParams() |
|
324 | - { |
|
325 | - return $this->params; |
|
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * @param string $key |
|
330 | - * @return string |
|
331 | - */ |
|
332 | - |
|
333 | - public function getParam($key) |
|
334 | - { |
|
335 | - return $this->params[$key]; |
|
336 | - } |
|
337 | - |
|
338 | - /** |
|
339 | - * Return defaults and params merged in one array. |
|
340 | - * |
|
341 | - * @return array |
|
342 | - */ |
|
343 | - |
|
344 | - public function getMergedParams() |
|
345 | - { |
|
346 | - return array_merge($this->defaults, $this->params); |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * @return array |
|
351 | - */ |
|
352 | - |
|
353 | - public function getDefaults() |
|
354 | - { |
|
355 | - return $this->defaults; |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * @param string $key |
|
360 | - * @return mixed |
|
361 | - */ |
|
362 | - |
|
363 | - public function getDefault($key) |
|
364 | - { |
|
365 | - return $this->defaults[$key]; |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * @return array |
|
370 | - */ |
|
371 | - |
|
372 | - public function getMetadataArray() |
|
373 | - { |
|
374 | - return $this->metadata; |
|
375 | - } |
|
376 | - |
|
377 | - /** |
|
378 | - * @param string $key |
|
379 | - * @return mixed |
|
380 | - */ |
|
381 | - |
|
382 | - public function getMetadata($key) |
|
383 | - { |
|
384 | - return $this->metadata[$key]; |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * @return string|null |
|
389 | - */ |
|
390 | - |
|
391 | - public function getStrategy() |
|
392 | - { |
|
393 | - if ($this->strategy instanceof StrategyInterface) { |
|
394 | - return get_class($this->strategy); |
|
395 | - } |
|
396 | - |
|
397 | - return $this->strategy; |
|
398 | - } |
|
399 | - |
|
400 | - /** |
|
401 | - * @return StrategyInterface|string |
|
402 | - */ |
|
403 | - |
|
404 | - public function getRawStrategy() |
|
405 | - { |
|
406 | - return $this->strategy; |
|
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * @return Matcher |
|
411 | - */ |
|
412 | - |
|
413 | - public function getMatcher() |
|
414 | - { |
|
415 | - return $this->matcher; |
|
416 | - } |
|
417 | - |
|
418 | - /** |
|
419 | - * Verify if a Route have already been blocked. |
|
420 | - * |
|
421 | - * @return boolean |
|
422 | - */ |
|
423 | - |
|
424 | - public function getBlock() |
|
425 | - { |
|
426 | - return $this->blocked; |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * Blocking a route indicate that that route have been selected and |
|
431 | - * parsed, now it will be given to the matcher. |
|
432 | - * |
|
433 | - * @param bool $blocked |
|
434 | - * @return self |
|
435 | - */ |
|
436 | - |
|
437 | - public function setBlock($blocked) |
|
438 | - { |
|
439 | - $this->blocked = $blocked; |
|
440 | - return $this; |
|
441 | - } |
|
442 | - |
|
443 | - /** |
|
444 | - * @param string $method |
|
445 | - * @return Route |
|
446 | - */ |
|
447 | - |
|
448 | - public function setMethod($method) |
|
449 | - { |
|
450 | - $this->forget(); |
|
451 | - $this->method = $method; |
|
452 | - return $this->reset(); |
|
453 | - } |
|
454 | - |
|
455 | - /** |
|
456 | - * @param string $pattern |
|
457 | - * @return Route |
|
458 | - */ |
|
459 | - |
|
460 | - public function setPattern($pattern) |
|
461 | - { |
|
462 | - $this->forget(); |
|
463 | - $this->pattern = $pattern; |
|
464 | - return $this->reset(); |
|
465 | - } |
|
466 | - |
|
467 | - /** |
|
468 | - * @param string $pattern |
|
469 | - * @return self |
|
470 | - */ |
|
471 | - |
|
472 | - public function setPatternWithoutReset($pattern) |
|
473 | - { |
|
474 | - $this->pattern = $pattern; |
|
475 | - return $this; |
|
476 | - } |
|
477 | - |
|
478 | - /** |
|
479 | - * @param string $action |
|
480 | - * @return self |
|
481 | - */ |
|
482 | - |
|
483 | - public function setAction($action) |
|
484 | - { |
|
485 | - $this->action = $action; |
|
486 | - return $this; |
|
487 | - } |
|
488 | - |
|
489 | - /** |
|
490 | - * @param string $namespace |
|
491 | - * @return self |
|
492 | - */ |
|
493 | - |
|
494 | - public function setNamespace($namespace) |
|
495 | - { |
|
496 | - $this->namespace = $namespace; |
|
497 | - return $this; |
|
498 | - } |
|
499 | - |
|
500 | - /** |
|
501 | - * @param string[] $params |
|
502 | - * @return self |
|
503 | - */ |
|
504 | - |
|
505 | - public function setParams(array $params) |
|
506 | - { |
|
507 | - $this->params = $params; |
|
508 | - return $this; |
|
509 | - } |
|
510 | - |
|
511 | - /** |
|
512 | - * @param string $key |
|
513 | - * @param string $value |
|
514 | - * |
|
515 | - * @return self |
|
516 | - */ |
|
517 | - |
|
518 | - public function setParam($key, $value) |
|
519 | - { |
|
520 | - $this->params[$key] = $value; |
|
521 | - return $this; |
|
522 | - } |
|
523 | - |
|
524 | - /** |
|
525 | - * @param mixed[] $defaults |
|
526 | - * @return self |
|
527 | - */ |
|
528 | - |
|
529 | - public function setDefaults(array $defaults) |
|
530 | - { |
|
531 | - $this->defaults = $defaults; |
|
532 | - return $this; |
|
533 | - } |
|
534 | - |
|
535 | - /** |
|
536 | - * @param string $key |
|
537 | - * @param mixed $value |
|
538 | - * |
|
539 | - * @return self |
|
540 | - */ |
|
541 | - |
|
542 | - public function setDefault($key, $value) |
|
543 | - { |
|
544 | - $this->defaults[$key] = $value; |
|
545 | - return $this; |
|
546 | - } |
|
547 | - |
|
548 | - /** |
|
549 | - * @param mixed[] $metadata |
|
550 | - * @return self |
|
551 | - */ |
|
552 | - |
|
553 | - public function setMetadataArray(array $metadata) |
|
554 | - { |
|
555 | - $this->metadata = $metadata; |
|
556 | - return $this; |
|
557 | - } |
|
558 | - |
|
559 | - /** |
|
560 | - * @param string $key |
|
561 | - * @param mixed $value |
|
562 | - * |
|
563 | - * @return $this |
|
564 | - */ |
|
565 | - |
|
566 | - public function setMetadata($key, $value) |
|
567 | - { |
|
568 | - $this->metadata[$key] = $value; |
|
569 | - return $this; |
|
570 | - } |
|
571 | - |
|
572 | - /** |
|
573 | - * @param null|string|StrategyInterface $strategy |
|
574 | - * @return self |
|
575 | - */ |
|
576 | - |
|
577 | - public function setStrategy($strategy) |
|
578 | - { |
|
579 | - $this->strategy = $strategy; |
|
580 | - return $this; |
|
581 | - } |
|
582 | - |
|
583 | - /** |
|
584 | - * @param Matcher $matcher |
|
585 | - * @return self |
|
586 | - */ |
|
587 | - |
|
588 | - public function setMatcher(Matcher $matcher) |
|
589 | - { |
|
590 | - $this->matcher = $matcher; |
|
591 | - return $this; |
|
592 | - } |
|
593 | - |
|
594 | - /** |
|
595 | - * Set a constraint to a token in the route pattern. |
|
596 | - * |
|
597 | - * @param string $token |
|
598 | - * @param string $regex |
|
599 | - * |
|
600 | - * @return self |
|
601 | - */ |
|
602 | - |
|
603 | - public function setConstraint($token, $regex) |
|
604 | - { |
|
605 | - $initPos = strpos($this->pattern, "{" . $token); |
|
606 | - |
|
607 | - if ($initPos !== false) { |
|
608 | - $endPos = strpos($this->pattern, "}", $initPos); |
|
609 | - $newPattern = substr_replace($this->pattern, "{" . "$token:$regex" . "}", $initPos, $endPos - $initPos + 1); |
|
610 | - $wildcards = $this->collector->getParser()->getWildcardTokens(); |
|
611 | - $newPattern = str_replace(array_keys($wildcards), $wildcards, $newPattern); |
|
612 | - $this->setPatternWithoutReset($newPattern); |
|
613 | - } |
|
614 | - |
|
615 | - return $this; |
|
616 | - } |
|
617 | - |
|
618 | - /** |
|
619 | - * @param string $key |
|
620 | - * @return bool |
|
621 | - */ |
|
622 | - |
|
623 | - public function hasParam($key) |
|
624 | - { |
|
625 | - return isset($this->params[$key]); |
|
626 | - } |
|
627 | - |
|
628 | - /** |
|
629 | - * @param string $key |
|
630 | - * @return bool |
|
631 | - */ |
|
632 | - |
|
633 | - public function hasDefault($key) |
|
634 | - { |
|
635 | - return isset($this->defaults[$key]); |
|
636 | - } |
|
637 | - |
|
638 | - /** |
|
639 | - * @param string $key |
|
640 | - * @return bool |
|
641 | - */ |
|
642 | - |
|
643 | - public function hasMetadata($key) |
|
644 | - { |
|
645 | - return isset($this->metadata[$key]); |
|
646 | - } |
|
44 | + /** |
|
45 | + * @var callable |
|
46 | + */ |
|
47 | + |
|
48 | + protected $action; |
|
49 | + |
|
50 | + /** |
|
51 | + * @var string |
|
52 | + */ |
|
53 | + |
|
54 | + protected $namespace = ""; |
|
55 | + |
|
56 | + /** |
|
57 | + * @var string[] |
|
58 | + */ |
|
59 | + |
|
60 | + protected $params = []; |
|
61 | + |
|
62 | + /** |
|
63 | + * Defaults are parameters set by the user, and don't |
|
64 | + * appear on the pattern. |
|
65 | + * |
|
66 | + * @var array |
|
67 | + */ |
|
68 | + |
|
69 | + protected $defaults = []; |
|
70 | + |
|
71 | + /** |
|
72 | + * Metadata can be set to be used on filters, dispatch strategies |
|
73 | + * or anywhere the route object is used. |
|
74 | + * |
|
75 | + * @var array |
|
76 | + */ |
|
77 | + |
|
78 | + protected $metadata = []; |
|
79 | + |
|
80 | + /** |
|
81 | + * @var string|StrategyInterface |
|
82 | + */ |
|
83 | + |
|
84 | + protected $strategy; |
|
85 | + |
|
86 | + /** |
|
87 | + * Blocked routes are dynamic routes selected to pass by the matcher. |
|
88 | + * |
|
89 | + * @var boolean |
|
90 | + */ |
|
91 | + |
|
92 | + protected $blocked = false; |
|
93 | + |
|
94 | + /** |
|
95 | + * The matcher that dispatched this route. |
|
96 | + * |
|
97 | + * @var Matcher $matcher |
|
98 | + */ |
|
99 | + |
|
100 | + protected $matcher; |
|
101 | + |
|
102 | + /** |
|
103 | + * The function used to create controllers from name. |
|
104 | + * |
|
105 | + * @var callable |
|
106 | + */ |
|
107 | + |
|
108 | + protected $controllerCreationFunction; |
|
109 | + |
|
110 | + /** |
|
111 | + * @param Collector $collector |
|
112 | + * @param string $method |
|
113 | + * @param string $pattern |
|
114 | + * @param callable $action |
|
115 | + */ |
|
116 | + |
|
117 | + public function __construct(Collector $collector, $method, $pattern, $action) |
|
118 | + { |
|
119 | + $this->collector = $collector; |
|
120 | + $this->method = $method; |
|
121 | + $this->pattern = $pattern; |
|
122 | + $this->action = $action; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Clone this route and set it into the collector. |
|
127 | + * |
|
128 | + * @return Route |
|
129 | + */ |
|
130 | + |
|
131 | + public function reset() |
|
132 | + { |
|
133 | + return $this->collector->set($this->method, $this->pattern, $this->action)->nth(0) |
|
134 | + ->setStrategy($this->strategy)->setParams($this->params) |
|
135 | + ->setDefaults($this->defaults)->setMetadataArray($this->metadata); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Remove this route from the collector. |
|
140 | + * |
|
141 | + * @return self |
|
142 | + */ |
|
143 | + |
|
144 | + public function forget() |
|
145 | + { |
|
146 | + $this->collector->forget($this->method, $this->pattern); |
|
147 | + return $this; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Execute the route action, if no strategy was provided the action |
|
152 | + * will be executed by the call_user_func PHP function. |
|
153 | + * |
|
154 | + * @param callable $container |
|
155 | + * @throws BadRouteException |
|
156 | + * @return mixed |
|
157 | + */ |
|
158 | + |
|
159 | + public function call(callable $container = null) |
|
160 | + { |
|
161 | + $this->action = $this->buildCallable($this->action, $container); |
|
162 | + |
|
163 | + if ($this->strategy === null) { |
|
164 | + return call_user_func_array($this->action, array_merge($this->defaults, $this->params)); |
|
165 | + } |
|
166 | + |
|
167 | + if (!is_object($this->strategy)) { |
|
168 | + if ($container === null) { |
|
169 | + $this->strategy = new $this->strategy; |
|
170 | + } else $this->strategy = $container($this->strategy); |
|
171 | + } |
|
172 | + |
|
173 | + return $this->callWithStrategy(); |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Seek for dynamic content in one callable. This allow to use parameters defined on pattern on callable |
|
178 | + * definition, eg. "get" "/{resource:string+}/{slug:slug+}" "{resource}::find". |
|
179 | + * |
|
180 | + * This will snakecase the resource parameter and deal with as a controller, then call the find method. |
|
181 | + * A request for "/articles/my-first-article" will execute find method of Articles controller with only |
|
182 | + * "my-first-article" as parameter. |
|
183 | + * |
|
184 | + * @param callable $callable |
|
185 | + * @param callable $container |
|
186 | + * |
|
187 | + * @return callable |
|
188 | + */ |
|
189 | + |
|
190 | + private function buildCallable($callable, $container) |
|
191 | + { |
|
192 | + if (is_string($callable) && strpos($callable, "::")) { |
|
193 | + $callable = explode("::", $callable); |
|
194 | + } |
|
195 | + |
|
196 | + if (is_array($callable)) { |
|
197 | + if (is_string($callable[0])) { |
|
198 | + $callable[0] = $this->parseCallableController($callable[0], $container); |
|
199 | + } |
|
200 | + |
|
201 | + $callable[1] = $this->parseCallablePlaceholders($callable[1]); |
|
202 | + } |
|
203 | + |
|
204 | + return $callable; |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * Get the controller object. |
|
209 | + * |
|
210 | + * @param string $controller |
|
211 | + * @param callable $container |
|
212 | + * |
|
213 | + * @return Object |
|
214 | + */ |
|
215 | + |
|
216 | + private function parseCallableController($controller, $container) |
|
217 | + { |
|
218 | + $controller = rtrim($this->namespace, "\\") . "\\" . $this->parseCallablePlaceholders($controller); |
|
219 | + |
|
220 | + if ($container === null) { |
|
221 | + return new $controller; |
|
222 | + } else return $container($controller); |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Parse and replace dynamic content on route action. |
|
227 | + * |
|
228 | + * @param string $fragment Part of callable |
|
229 | + * @return string |
|
230 | + */ |
|
231 | + |
|
232 | + private function parseCallablePlaceholders($fragment) |
|
233 | + { |
|
234 | + if (strpos($fragment, "{") !== false) { |
|
235 | + foreach ($this->params as $placeholder => $value) { |
|
236 | + if (strpos($fragment, "{" . $placeholder . "}") !== false) { |
|
237 | + $fragment = str_replace("{" . $placeholder . "}", ucwords(str_replace("-", " ", $value)), $fragment); |
|
238 | + } |
|
239 | + } |
|
240 | + } |
|
241 | + |
|
242 | + return $fragment; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Execute the route action with the given strategy. |
|
247 | + * |
|
248 | + * @throws BadRouteException |
|
249 | + * @return mixed |
|
250 | + */ |
|
251 | + |
|
252 | + private function callWithStrategy() |
|
253 | + { |
|
254 | + if ($this->strategy instanceof StrategyInterface) { |
|
255 | + if ($this->strategy instanceof MatcherAwareInterface) { |
|
256 | + $this->strategy->setMatcher($this->matcher); |
|
257 | + } |
|
258 | + |
|
259 | + return $this->strategy->call($this); |
|
260 | + } |
|
261 | + |
|
262 | + throw new BadRouteException(str_replace("%s", get_class($this->strategy), BadRouteException::BAD_STRATEGY)); |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * @return Collector |
|
267 | + */ |
|
268 | + |
|
269 | + public function getCollector() |
|
270 | + { |
|
271 | + return $this->collector; |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * @return string |
|
276 | + */ |
|
277 | + |
|
278 | + public function getMethod() |
|
279 | + { |
|
280 | + return $this->method; |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * @return string |
|
285 | + */ |
|
286 | + |
|
287 | + public function getPattern() |
|
288 | + { |
|
289 | + return $this->pattern; |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * @return string[] |
|
294 | + */ |
|
295 | + |
|
296 | + public function getSegments() |
|
297 | + { |
|
298 | + return explode("/", $this->pattern); |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * @return callable |
|
303 | + */ |
|
304 | + |
|
305 | + public function getAction() |
|
306 | + { |
|
307 | + return $this->action; |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * @return string |
|
312 | + */ |
|
313 | + |
|
314 | + public function getNamespace() |
|
315 | + { |
|
316 | + return $this->namespace; |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * @return string[] |
|
321 | + */ |
|
322 | + |
|
323 | + public function getParams() |
|
324 | + { |
|
325 | + return $this->params; |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * @param string $key |
|
330 | + * @return string |
|
331 | + */ |
|
332 | + |
|
333 | + public function getParam($key) |
|
334 | + { |
|
335 | + return $this->params[$key]; |
|
336 | + } |
|
337 | + |
|
338 | + /** |
|
339 | + * Return defaults and params merged in one array. |
|
340 | + * |
|
341 | + * @return array |
|
342 | + */ |
|
343 | + |
|
344 | + public function getMergedParams() |
|
345 | + { |
|
346 | + return array_merge($this->defaults, $this->params); |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * @return array |
|
351 | + */ |
|
352 | + |
|
353 | + public function getDefaults() |
|
354 | + { |
|
355 | + return $this->defaults; |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * @param string $key |
|
360 | + * @return mixed |
|
361 | + */ |
|
362 | + |
|
363 | + public function getDefault($key) |
|
364 | + { |
|
365 | + return $this->defaults[$key]; |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * @return array |
|
370 | + */ |
|
371 | + |
|
372 | + public function getMetadataArray() |
|
373 | + { |
|
374 | + return $this->metadata; |
|
375 | + } |
|
376 | + |
|
377 | + /** |
|
378 | + * @param string $key |
|
379 | + * @return mixed |
|
380 | + */ |
|
381 | + |
|
382 | + public function getMetadata($key) |
|
383 | + { |
|
384 | + return $this->metadata[$key]; |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * @return string|null |
|
389 | + */ |
|
390 | + |
|
391 | + public function getStrategy() |
|
392 | + { |
|
393 | + if ($this->strategy instanceof StrategyInterface) { |
|
394 | + return get_class($this->strategy); |
|
395 | + } |
|
396 | + |
|
397 | + return $this->strategy; |
|
398 | + } |
|
399 | + |
|
400 | + /** |
|
401 | + * @return StrategyInterface|string |
|
402 | + */ |
|
403 | + |
|
404 | + public function getRawStrategy() |
|
405 | + { |
|
406 | + return $this->strategy; |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * @return Matcher |
|
411 | + */ |
|
412 | + |
|
413 | + public function getMatcher() |
|
414 | + { |
|
415 | + return $this->matcher; |
|
416 | + } |
|
417 | + |
|
418 | + /** |
|
419 | + * Verify if a Route have already been blocked. |
|
420 | + * |
|
421 | + * @return boolean |
|
422 | + */ |
|
423 | + |
|
424 | + public function getBlock() |
|
425 | + { |
|
426 | + return $this->blocked; |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * Blocking a route indicate that that route have been selected and |
|
431 | + * parsed, now it will be given to the matcher. |
|
432 | + * |
|
433 | + * @param bool $blocked |
|
434 | + * @return self |
|
435 | + */ |
|
436 | + |
|
437 | + public function setBlock($blocked) |
|
438 | + { |
|
439 | + $this->blocked = $blocked; |
|
440 | + return $this; |
|
441 | + } |
|
442 | + |
|
443 | + /** |
|
444 | + * @param string $method |
|
445 | + * @return Route |
|
446 | + */ |
|
447 | + |
|
448 | + public function setMethod($method) |
|
449 | + { |
|
450 | + $this->forget(); |
|
451 | + $this->method = $method; |
|
452 | + return $this->reset(); |
|
453 | + } |
|
454 | + |
|
455 | + /** |
|
456 | + * @param string $pattern |
|
457 | + * @return Route |
|
458 | + */ |
|
459 | + |
|
460 | + public function setPattern($pattern) |
|
461 | + { |
|
462 | + $this->forget(); |
|
463 | + $this->pattern = $pattern; |
|
464 | + return $this->reset(); |
|
465 | + } |
|
466 | + |
|
467 | + /** |
|
468 | + * @param string $pattern |
|
469 | + * @return self |
|
470 | + */ |
|
471 | + |
|
472 | + public function setPatternWithoutReset($pattern) |
|
473 | + { |
|
474 | + $this->pattern = $pattern; |
|
475 | + return $this; |
|
476 | + } |
|
477 | + |
|
478 | + /** |
|
479 | + * @param string $action |
|
480 | + * @return self |
|
481 | + */ |
|
482 | + |
|
483 | + public function setAction($action) |
|
484 | + { |
|
485 | + $this->action = $action; |
|
486 | + return $this; |
|
487 | + } |
|
488 | + |
|
489 | + /** |
|
490 | + * @param string $namespace |
|
491 | + * @return self |
|
492 | + */ |
|
493 | + |
|
494 | + public function setNamespace($namespace) |
|
495 | + { |
|
496 | + $this->namespace = $namespace; |
|
497 | + return $this; |
|
498 | + } |
|
499 | + |
|
500 | + /** |
|
501 | + * @param string[] $params |
|
502 | + * @return self |
|
503 | + */ |
|
504 | + |
|
505 | + public function setParams(array $params) |
|
506 | + { |
|
507 | + $this->params = $params; |
|
508 | + return $this; |
|
509 | + } |
|
510 | + |
|
511 | + /** |
|
512 | + * @param string $key |
|
513 | + * @param string $value |
|
514 | + * |
|
515 | + * @return self |
|
516 | + */ |
|
517 | + |
|
518 | + public function setParam($key, $value) |
|
519 | + { |
|
520 | + $this->params[$key] = $value; |
|
521 | + return $this; |
|
522 | + } |
|
523 | + |
|
524 | + /** |
|
525 | + * @param mixed[] $defaults |
|
526 | + * @return self |
|
527 | + */ |
|
528 | + |
|
529 | + public function setDefaults(array $defaults) |
|
530 | + { |
|
531 | + $this->defaults = $defaults; |
|
532 | + return $this; |
|
533 | + } |
|
534 | + |
|
535 | + /** |
|
536 | + * @param string $key |
|
537 | + * @param mixed $value |
|
538 | + * |
|
539 | + * @return self |
|
540 | + */ |
|
541 | + |
|
542 | + public function setDefault($key, $value) |
|
543 | + { |
|
544 | + $this->defaults[$key] = $value; |
|
545 | + return $this; |
|
546 | + } |
|
547 | + |
|
548 | + /** |
|
549 | + * @param mixed[] $metadata |
|
550 | + * @return self |
|
551 | + */ |
|
552 | + |
|
553 | + public function setMetadataArray(array $metadata) |
|
554 | + { |
|
555 | + $this->metadata = $metadata; |
|
556 | + return $this; |
|
557 | + } |
|
558 | + |
|
559 | + /** |
|
560 | + * @param string $key |
|
561 | + * @param mixed $value |
|
562 | + * |
|
563 | + * @return $this |
|
564 | + */ |
|
565 | + |
|
566 | + public function setMetadata($key, $value) |
|
567 | + { |
|
568 | + $this->metadata[$key] = $value; |
|
569 | + return $this; |
|
570 | + } |
|
571 | + |
|
572 | + /** |
|
573 | + * @param null|string|StrategyInterface $strategy |
|
574 | + * @return self |
|
575 | + */ |
|
576 | + |
|
577 | + public function setStrategy($strategy) |
|
578 | + { |
|
579 | + $this->strategy = $strategy; |
|
580 | + return $this; |
|
581 | + } |
|
582 | + |
|
583 | + /** |
|
584 | + * @param Matcher $matcher |
|
585 | + * @return self |
|
586 | + */ |
|
587 | + |
|
588 | + public function setMatcher(Matcher $matcher) |
|
589 | + { |
|
590 | + $this->matcher = $matcher; |
|
591 | + return $this; |
|
592 | + } |
|
593 | + |
|
594 | + /** |
|
595 | + * Set a constraint to a token in the route pattern. |
|
596 | + * |
|
597 | + * @param string $token |
|
598 | + * @param string $regex |
|
599 | + * |
|
600 | + * @return self |
|
601 | + */ |
|
602 | + |
|
603 | + public function setConstraint($token, $regex) |
|
604 | + { |
|
605 | + $initPos = strpos($this->pattern, "{" . $token); |
|
606 | + |
|
607 | + if ($initPos !== false) { |
|
608 | + $endPos = strpos($this->pattern, "}", $initPos); |
|
609 | + $newPattern = substr_replace($this->pattern, "{" . "$token:$regex" . "}", $initPos, $endPos - $initPos + 1); |
|
610 | + $wildcards = $this->collector->getParser()->getWildcardTokens(); |
|
611 | + $newPattern = str_replace(array_keys($wildcards), $wildcards, $newPattern); |
|
612 | + $this->setPatternWithoutReset($newPattern); |
|
613 | + } |
|
614 | + |
|
615 | + return $this; |
|
616 | + } |
|
617 | + |
|
618 | + /** |
|
619 | + * @param string $key |
|
620 | + * @return bool |
|
621 | + */ |
|
622 | + |
|
623 | + public function hasParam($key) |
|
624 | + { |
|
625 | + return isset($this->params[$key]); |
|
626 | + } |
|
627 | + |
|
628 | + /** |
|
629 | + * @param string $key |
|
630 | + * @return bool |
|
631 | + */ |
|
632 | + |
|
633 | + public function hasDefault($key) |
|
634 | + { |
|
635 | + return isset($this->defaults[$key]); |
|
636 | + } |
|
637 | + |
|
638 | + /** |
|
639 | + * @param string $key |
|
640 | + * @return bool |
|
641 | + */ |
|
642 | + |
|
643 | + public function hasMetadata($key) |
|
644 | + { |
|
645 | + return isset($this->metadata[$key]); |
|
646 | + } |
|
647 | 647 | |
648 | 648 | } |
@@ -167,7 +167,9 @@ discard block |
||
167 | 167 | if (!is_object($this->strategy)) { |
168 | 168 | if ($container === null) { |
169 | 169 | $this->strategy = new $this->strategy; |
170 | - } else $this->strategy = $container($this->strategy); |
|
170 | + } else { |
|
171 | + $this->strategy = $container($this->strategy); |
|
172 | + } |
|
171 | 173 | } |
172 | 174 | |
173 | 175 | return $this->callWithStrategy(); |
@@ -219,7 +221,9 @@ discard block |
||
219 | 221 | |
220 | 222 | if ($container === null) { |
221 | 223 | return new $controller; |
222 | - } else return $container($controller); |
|
224 | + } else { |
|
225 | + return $container($controller); |
|
226 | + } |
|
223 | 227 | } |
224 | 228 | |
225 | 229 | /** |
@@ -22,69 +22,69 @@ |
||
22 | 22 | trait RequestAwareTrait |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * @var RequestInterface |
|
27 | - */ |
|
28 | - |
|
29 | - protected $request; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var ResponseInterface |
|
33 | - */ |
|
34 | - |
|
35 | - protected $response; |
|
36 | - |
|
37 | - /** |
|
38 | - * RequestResponseStrategy constructor. |
|
39 | - * |
|
40 | - * @param RequestInterface $request |
|
41 | - * @param ResponseInterface $response |
|
42 | - */ |
|
43 | - |
|
44 | - public function __construct(RequestInterface $request, ResponseInterface $response) |
|
45 | - { |
|
46 | - $this->request = $request; |
|
47 | - $this->response = $response; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * @return RequestInterface |
|
52 | - */ |
|
53 | - |
|
54 | - public function getRequest() |
|
55 | - { |
|
56 | - return $this->request; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * @param RequestInterface $request |
|
61 | - * @return self |
|
62 | - */ |
|
63 | - |
|
64 | - public function setRequest(RequestInterface $request) |
|
65 | - { |
|
66 | - $this->request = $request; |
|
67 | - return $this; |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * @return ResponseInterface |
|
72 | - */ |
|
73 | - |
|
74 | - public function getResponse() |
|
75 | - { |
|
76 | - return $this->response; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @param ResponseInterface $response |
|
81 | - * @return self |
|
82 | - */ |
|
83 | - |
|
84 | - public function setResponse(ResponseInterface $response) |
|
85 | - { |
|
86 | - $this->response = $response; |
|
87 | - return $this; |
|
88 | - } |
|
25 | + /** |
|
26 | + * @var RequestInterface |
|
27 | + */ |
|
28 | + |
|
29 | + protected $request; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var ResponseInterface |
|
33 | + */ |
|
34 | + |
|
35 | + protected $response; |
|
36 | + |
|
37 | + /** |
|
38 | + * RequestResponseStrategy constructor. |
|
39 | + * |
|
40 | + * @param RequestInterface $request |
|
41 | + * @param ResponseInterface $response |
|
42 | + */ |
|
43 | + |
|
44 | + public function __construct(RequestInterface $request, ResponseInterface $response) |
|
45 | + { |
|
46 | + $this->request = $request; |
|
47 | + $this->response = $response; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * @return RequestInterface |
|
52 | + */ |
|
53 | + |
|
54 | + public function getRequest() |
|
55 | + { |
|
56 | + return $this->request; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * @param RequestInterface $request |
|
61 | + * @return self |
|
62 | + */ |
|
63 | + |
|
64 | + public function setRequest(RequestInterface $request) |
|
65 | + { |
|
66 | + $this->request = $request; |
|
67 | + return $this; |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * @return ResponseInterface |
|
72 | + */ |
|
73 | + |
|
74 | + public function getResponse() |
|
75 | + { |
|
76 | + return $this->response; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @param ResponseInterface $response |
|
81 | + * @return self |
|
82 | + */ |
|
83 | + |
|
84 | + public function setResponse(ResponseInterface $response) |
|
85 | + { |
|
86 | + $this->response = $response; |
|
87 | + return $this; |
|
88 | + } |
|
89 | 89 | |
90 | 90 | } |
@@ -25,32 +25,32 @@ |
||
25 | 25 | class RequestJsonStrategy implements StrategyInterface |
26 | 26 | { |
27 | 27 | |
28 | - use RequestAwareTrait; |
|
29 | - |
|
30 | - /** |
|
31 | - * @inheritdoc |
|
32 | - * @throws RuntimeException |
|
33 | - * @return ResponseInterface |
|
34 | - */ |
|
35 | - |
|
36 | - public function call(Route $route) |
|
37 | - { |
|
38 | - try { |
|
39 | - $response = call_user_func($route->getAction(), $this->request, $route->getMergedParams()); |
|
40 | - |
|
41 | - if (is_array($response)) { |
|
42 | - $this->response->getBody()->write(json_encode($response)); |
|
43 | - $response = $this->response; |
|
44 | - } |
|
45 | - |
|
46 | - if ($response instanceof ResponseInterface) { |
|
47 | - return $response->withAddedHeader("content-type", "application/json"); |
|
48 | - } |
|
49 | - } catch (HttpExceptionAbstract $e) { |
|
50 | - return $e->getJsonResponse($this->response); |
|
51 | - } |
|
52 | - |
|
53 | - throw new RuntimeException("Unable to determine a json response from action returned value."); |
|
54 | - } |
|
28 | + use RequestAwareTrait; |
|
29 | + |
|
30 | + /** |
|
31 | + * @inheritdoc |
|
32 | + * @throws RuntimeException |
|
33 | + * @return ResponseInterface |
|
34 | + */ |
|
35 | + |
|
36 | + public function call(Route $route) |
|
37 | + { |
|
38 | + try { |
|
39 | + $response = call_user_func($route->getAction(), $this->request, $route->getMergedParams()); |
|
40 | + |
|
41 | + if (is_array($response)) { |
|
42 | + $this->response->getBody()->write(json_encode($response)); |
|
43 | + $response = $this->response; |
|
44 | + } |
|
45 | + |
|
46 | + if ($response instanceof ResponseInterface) { |
|
47 | + return $response->withAddedHeader("content-type", "application/json"); |
|
48 | + } |
|
49 | + } catch (HttpExceptionAbstract $e) { |
|
50 | + return $e->getJsonResponse($this->response); |
|
51 | + } |
|
52 | + |
|
53 | + throw new RuntimeException("Unable to determine a json response from action returned value."); |
|
54 | + } |
|
55 | 55 | |
56 | 56 | } |