1 | <?php |
||
14 | class RouteDispatcher implements RouteDispatcherInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var RouteCollection |
||
18 | */ |
||
19 | private $collection; |
||
20 | |||
21 | /** |
||
22 | * @var DispatcherInterface |
||
23 | */ |
||
24 | private $dispatcher; |
||
25 | |||
26 | /** |
||
27 | * @param DispatcherInterface $dispatcher |
||
28 | * @param RouteCollection $collection |
||
29 | */ |
||
30 | 4 | public function __construct(DispatcherInterface $dispatcher, RouteCollection $collection) |
|
35 | |||
36 | /** |
||
37 | * Dispatch the request |
||
38 | * |
||
39 | * @param ServerRequestInterface $request |
||
40 | * @throws MethodNotAllowedException |
||
41 | * @throws NotFoundHttpException |
||
42 | * @return mixed |
||
43 | */ |
||
44 | 4 | public function dispatch(ServerRequestInterface $request) |
|
45 | { |
||
46 | 4 | $method = $request->getMethod(); |
|
47 | 4 | $uri = $request->getUri()->getPath(); |
|
48 | |||
49 | 4 | $routeInfo = $this->createDispatcher()->dispatch($method, $uri); |
|
50 | |||
51 | 4 | switch ($routeInfo[0]) { |
|
52 | 4 | case Dispatcher::NOT_FOUND: |
|
53 | 1 | throw new NotFoundHttpException; |
|
54 | |||
55 | 3 | case Dispatcher::METHOD_NOT_ALLOWED: |
|
56 | 1 | throw new MethodNotAllowedException($routeInfo[1]); |
|
57 | |||
58 | 2 | case Dispatcher::FOUND: |
|
59 | 2 | $route = $routeInfo[1]; |
|
60 | 2 | $route->bind($routeInfo[2]); |
|
61 | |||
62 | return $this->dispatcher->dispatch($route, function ($response) { |
||
63 | return $response; |
||
64 | 2 | }); |
|
65 | } |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @return Dispatcher |
||
70 | */ |
||
71 | private function createDispatcher() |
||
79 | } |
||
80 |