|
2 | 2 |
|
3 | 3 | namespace League\Route; |
4 | 4 |
|
5 | | -use Closure; |
6 | 5 | use FastRoute\Dispatcher as FastRoute; |
7 | 6 | use FastRoute\Dispatcher\GroupCountBased as GroupCountBasedDispatcher; |
8 | | -use League\Container\ContainerAwareInterface; |
9 | | -use League\Container\ContainerInterface; |
10 | 7 | use League\Route\Http\Exception\MethodNotAllowedException; |
11 | 8 | use League\Route\Http\Exception\NotFoundException; |
12 | | -use League\Route\Strategy\RestfulStrategy; |
13 | | -use League\Route\Strategy\StrategyInterface; |
14 | | -use League\Route\Strategy\StrategyTrait; |
15 | | -use RuntimeException; |
| 9 | +use League\Route\Strategy\JsonStrategy; |
| 10 | +use League\Route\Strategy\StrategyAwareInterface; |
| 11 | +use League\Route\Strategy\StrategyAwareTrait; |
| 12 | +use Psr\Http\Message\ResponseInterface; |
| 13 | +use Psr\Http\Message\ServerRequestInterface; |
16 | 14 |
|
17 | | -class Dispatcher extends GroupCountBasedDispatcher |
| 15 | +class Dispatcher extends GroupCountBasedDispatcher implements StrategyAwareInterface |
18 | 16 | { |
19 | | - /** |
20 | | - * Route strategy functionality |
21 | | - */ |
22 | | - use StrategyTrait; |
23 | | - |
24 | | - /** |
25 | | - * @var \League\Container\ContainerInterface |
26 | | - */ |
27 | | - protected $container; |
28 | | - |
29 | | - /** |
30 | | - * @var array |
31 | | - */ |
32 | | - protected $routes; |
| 17 | + use StrategyAwareTrait; |
33 | 18 |
|
34 | 19 | /** |
35 | | - * Constructor |
| 20 | + * Match and dispatch a route matching the given http method and uri. |
36 | 21 | * |
37 | | - * @param \League\Container\ContainerInterface $container |
38 | | - * @param array $routes |
39 | | - * @param array $data |
40 | | - */ |
41 | | - public function __construct(ContainerInterface $container, array $routes, array $data) |
42 | | - { |
43 | | - $this->container = $container; |
44 | | - $this->routes = $routes; |
45 | | - |
46 | | - parent::__construct($data); |
47 | | - } |
48 | | - |
49 | | - /** |
50 | | - * Match and dispatch a route matching the given http method and uri |
| 22 | + * @param \Psr\Http\Message\ServerRequestInterface $request |
| 23 | + * @param \Psr\Http\Message\ResponseInterface $response |
51 | 24 | * |
52 | | - * @param string $method |
53 | | - * @param string $uri |
54 | | - * @return \Symfony\Component\HttpFoundation\Response |
| 25 | + * @return \Psr\Http\Message\ResponseInterface |
55 | 26 | */ |
56 | | - public function dispatch($method, $uri) |
| 27 | + public function handle(ServerRequestInterface $request, ResponseInterface $response) |
57 | 28 | { |
58 | | - $match = parent::dispatch($method, $uri); |
| 29 | + $match = $this->dispatch( |
| 30 | + $request->getMethod(), |
| 31 | + $request->getUri()->getPath() |
| 32 | + ); |
59 | 33 |
|
60 | 34 | if ($match[0] === FastRoute::NOT_FOUND) { |
61 | | - return $this->handleNotFound(); |
| 35 | + return $this->handleNotFound($response); |
62 | 36 | } |
63 | 37 |
|
64 | 38 | if ($match[0] === FastRoute::METHOD_NOT_ALLOWED) { |
65 | | - $allowed = (array) $match[1]; |
66 | | - return $this->handleNotAllowed($allowed); |
| 39 | + $allowed = (array) $match[1]; |
| 40 | + return $this->handleNotAllowed($response, $allowed); |
67 | 41 | } |
68 | 42 |
|
69 | | - $handler = (isset($this->routes[$match[1]]['callback'])) ? $this->routes[$match[1]]['callback'] : $match[1]; |
70 | | - $strategy = $this->routes[$match[1]]['strategy']; |
71 | | - $vars = (array) $match[2]; |
72 | | - |
73 | | - return $this->handleFound($handler, $strategy, $vars); |
| 43 | + return $this->handleFound($match[1], $request, $response, (array) $match[2]); |
74 | 44 | } |
75 | 45 |
|
76 | 46 | /** |
77 | | - * Handle dispatching of a found route |
| 47 | + * Handle dispatching of a found route. |
| 48 | + * |
| 49 | + * @param callable $route |
| 50 | + * @param \Psr\Http\Message\ServerRequestInterface $request |
| 51 | + * @param \Psr\Http\Message\ResponseInterface $response |
| 52 | + * @param array $vars |
78 | 53 | * |
79 | | - * @param string|\Closure $handler |
80 | | - * @param \League\Route\Strategy\StrategyInterface $strategy |
81 | | - * @param array $vars |
82 | | - * @return \Symfony\Component\HttpFoundation\Response |
83 | | - * @throws \RuntimeException |
| 54 | + * @return \Psr\Http\Message\ResponseInterface |
84 | 55 | */ |
85 | | - protected function handleFound($handler, StrategyInterface $strategy, array $vars) |
86 | | - { |
87 | | - if (is_null($this->getStrategy())) { |
88 | | - $this->setStrategy($strategy); |
89 | | - } |
90 | | - |
91 | | - $controller = null; |
92 | | - |
93 | | - // figure out what the controller is |
94 | | - if (($handler instanceof Closure) || is_callable($handler)) { |
95 | | - $controller = $handler; |
96 | | - } |
97 | | - |
98 | | - if (is_string($handler) && strpos($handler, '::') !== false) { |
99 | | - $controller = explode('::', $handler); |
100 | | - } |
101 | | - |
102 | | - // if controller method wasn't specified, throw exception. |
103 | | - if (! $controller) { |
104 | | - throw new RuntimeException('A class method must be provided as a controller. ClassName::methodName'); |
105 | | - } |
106 | | - |
107 | | - // dispatch via strategy |
108 | | - if ($strategy instanceof ContainerAwareInterface) { |
109 | | - $strategy->setContainer($this->container); |
110 | | - } |
111 | | - |
112 | | - return $strategy->dispatch($controller, $vars); |
| 56 | + protected function handleFound( |
| 57 | + callable $route, |
| 58 | + ServerRequestInterface $request, |
| 59 | + ResponseInterface $response, |
| 60 | + array $vars |
| 61 | + ) { |
| 62 | + return call_user_func_array($route, [$request, $response, $vars]); |
113 | 63 | } |
114 | 64 |
|
115 | 65 | /** |
116 | | - * Handle a not found route |
| 66 | + * Handle a not found route. |
117 | 67 | * |
118 | | - * @return \Symfony\Component\HttpFoundation\Response |
| 68 | + * @param \Psr\Http\Message\ResponseInterface $response |
| 69 | + * |
| 70 | + * @throws \League\Route\Http\Exception\NotFoundException if a response cannot be built |
| 71 | + * |
| 72 | + * @return \Psr\Http\Message\ResponseInterface |
119 | 73 | */ |
120 | | - protected function handleNotFound() |
| 74 | + protected function handleNotFound(ResponseInterface $response) |
121 | 75 | { |
122 | 76 | $exception = new NotFoundException; |
123 | 77 |
|
124 | | - if ($this->getStrategy() instanceof RestfulStrategy) { |
125 | | - return $exception->getJsonResponse(); |
| 78 | + if ($this->getStrategy() instanceof JsonStrategy) { |
| 79 | + return $exception->buildJsonResponse($response); |
126 | 80 | } |
127 | 81 |
|
128 | 82 | throw $exception; |
129 | 83 | } |
130 | 84 |
|
131 | 85 | /** |
132 | | - * Handles a not allowed route |
| 86 | + * Handles a not allowed route. |
| 87 | + * |
| 88 | + * @param \Psr\Http\Message\ResponseInterface $response |
| 89 | + * @param array $allowed |
| 90 | + * |
| 91 | + * @throws \League\Route\Http\Exception\MethodNotAllowedException if a response cannot be built |
133 | 92 | * |
134 | | - * @param array $allowed |
135 | | - * @return \Symfony\Component\HttpFoundation\Response |
| 93 | + * @return \Psr\Http\Message\ResponseInterface |
136 | 94 | */ |
137 | | - protected function handleNotAllowed(array $allowed) |
| 95 | + protected function handleNotAllowed(ResponseInterface $response, array $allowed) |
138 | 96 | { |
139 | 97 | $exception = new MethodNotAllowedException($allowed); |
140 | 98 |
|
141 | | - if ($this->getStrategy() instanceof RestfulStrategy) { |
142 | | - return $exception->getJsonResponse(); |
| 99 | + if ($this->getStrategy() instanceof JsonStrategy) { |
| 100 | + return $exception->buildJsonResponse($response); |
143 | 101 | } |
144 | 102 |
|
145 | 103 | throw $exception; |
|
0 commit comments