1 | <?php |
||
13 | class Router |
||
14 | { |
||
15 | /** @var \FastRoute\RouteCollector */ |
||
16 | protected $routeCollector; |
||
17 | |||
18 | /** @var array */ |
||
19 | protected $redirects = []; |
||
20 | |||
21 | 4 | public function __construct(RouteCollector $routeCollector) |
|
25 | |||
26 | 2 | public function get(string $url, string $controller): Router |
|
32 | |||
33 | public function put(string $url, string $controller): Router |
||
39 | |||
40 | public function post(string $url, string $controller): Router |
||
46 | |||
47 | public function patch(string $url, string $controller): Router |
||
53 | |||
54 | public function delete(string $url, string $controller): Router |
||
60 | |||
61 | 1 | public function redirect(string $url, string $targetUrl): Router |
|
67 | |||
68 | public function getRedirectForUrl(string $url): ?string |
||
72 | |||
73 | 1 | public function dispatch(Request $request): ?Response |
|
74 | { |
||
75 | 1 | $dispatcher = new GroupCountBased($this->routeCollector->getData()); |
|
76 | |||
77 | 1 | $routeInfo = $dispatcher->dispatch( |
|
78 | 1 | $request->getMethod(), |
|
79 | 1 | $request->getUri()->getPath() |
|
80 | ); |
||
81 | |||
82 | 1 | if ($routeInfo[0] !== Dispatcher::FOUND) { |
|
83 | return null; |
||
84 | } |
||
85 | |||
86 | 1 | $handler = $this->resolveHandler($routeInfo[1]); |
|
87 | 1 | $parameters = $routeInfo[2]; |
|
88 | |||
89 | return \call_user_func_array( |
||
90 | 1 | $handler, |
|
91 | 1 | array_merge($parameters, [$request]) |
|
92 | ); |
||
93 | } |
||
94 | |||
95 | 1 | protected function resolveHandler(array $callback): array |
|
109 | } |
||
110 |