1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\Route; |
4
|
|
|
|
5
|
|
|
use FastRoute\Dispatcher as FastRoute; |
6
|
|
|
use FastRoute\Dispatcher\GroupCountBased as GroupCountBasedDispatcher; |
7
|
|
|
use League\Route\Http\Exception\MethodNotAllowedException; |
8
|
|
|
use League\Route\Http\Exception\NotFoundException; |
9
|
|
|
use League\Route\Middleware\ExecutionChain; |
10
|
|
|
use League\Route\Strategy\JsonStrategy; |
11
|
|
|
use League\Route\Strategy\StrategyAwareInterface; |
12
|
|
|
use League\Route\Strategy\StrategyAwareTrait; |
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
15
|
|
|
|
16
|
|
|
class Dispatcher extends GroupCountBasedDispatcher implements StrategyAwareInterface |
17
|
|
|
{ |
18
|
|
|
use StrategyAwareTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Match and dispatch a route matching the given http method and |
22
|
|
|
* uri, retruning an execution chain. |
23
|
|
|
* |
24
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request |
25
|
|
|
* |
26
|
|
|
* @return \League\Route\Middleware\ExecutionChain |
27
|
|
|
*/ |
28
|
|
|
public function handle(ServerRequestInterface $request) |
29
|
|
|
{ |
30
|
|
|
$match = $this->dispatch( |
31
|
|
|
$request->getMethod(), |
32
|
|
|
$request->getUri()->getPath() |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
if ($match[0] === FastRoute::NOT_FOUND) { |
36
|
|
|
return $this->handleNotFound(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ($match[0] === FastRoute::METHOD_NOT_ALLOWED) { |
40
|
|
|
$allowed = (array) $match[1]; |
41
|
|
|
return $this->handleNotAllowed($allowed); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $this->handleFound($match[1], (array) $match[2]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Handle dispatching of a found route. |
49
|
|
|
* |
50
|
|
|
* @param callable $route |
51
|
|
|
* @param array $vars |
52
|
|
|
* |
53
|
|
|
* @return \League\Route\Middleware\ExecutionChain |
54
|
|
|
*/ |
55
|
|
|
protected function handleFound(callable $route, array $vars) |
56
|
|
|
{ |
57
|
|
|
return call_user_func_array($route, [$vars]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Handle a not found route. |
62
|
|
|
* |
63
|
|
|
* @return \League\Route\Middleware\ExecutionChain |
64
|
|
|
*/ |
65
|
|
|
protected function handleNotFound() |
66
|
|
|
{ |
67
|
|
|
$middleware = function (ServerRequestInterface $request, ResponseInterface $response) { |
68
|
|
|
$exception = new NotFoundException; |
69
|
|
|
|
70
|
|
|
if ($this->getStrategy() instanceof JsonStrategy) { |
71
|
|
|
return $exception->buildJsonResponse($response); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
throw $exception; |
75
|
|
|
}; |
76
|
|
|
|
77
|
|
|
return (new ExecutionChain)->middleware($middleware); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Handles a not allowed route. |
82
|
|
|
* |
83
|
|
|
* @param array $allowed |
84
|
|
|
* |
85
|
|
|
* @return \League\Route\Middleware\ExecutionChain |
86
|
|
|
*/ |
87
|
|
|
protected function handleNotAllowed(array $allowed) |
88
|
|
|
{ |
89
|
|
|
$middleware = function (ServerRequestInterface $request, ResponseInterface $response) use ($allowed) { |
90
|
|
|
$exception = new MethodNotAllowedException($allowed); |
91
|
|
|
|
92
|
|
|
if ($this->getStrategy() instanceof JsonStrategy) { |
93
|
|
|
return $exception->buildJsonResponse($response); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
throw $exception; |
97
|
|
|
}; |
98
|
|
|
|
99
|
|
|
return (new ExecutionChain)->middleware($middleware); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|