1 | <?php |
||
17 | final class Router |
||
18 | { |
||
19 | /** |
||
20 | * routes array to be registered. |
||
21 | * Each route item is an array has items respectively : Request Method, Request Uri, Controller/Action, Return Type. |
||
22 | * Route item can be defined using array key as an alias key. |
||
23 | * @var array |
||
24 | */ |
||
25 | private $routes = []; |
||
26 | |||
27 | /** |
||
28 | * HTTP request Method |
||
29 | * @var string |
||
30 | */ |
||
31 | private $method = null; |
||
32 | |||
33 | /** |
||
34 | * Request Uri |
||
35 | * @var string |
||
36 | */ |
||
37 | private $requestedPath = null; |
||
38 | |||
39 | /** |
||
40 | * Default return type if not noted in the $routes |
||
41 | * @var string |
||
42 | */ |
||
43 | private $defaultReturnType = null; |
||
44 | |||
45 | /** |
||
46 | * Router constructor. |
||
47 | * Create new router. |
||
48 | * |
||
49 | * @param array $routes |
||
50 | * @param string $defaultReturnType |
||
51 | * @param string $method |
||
52 | * @param string $requestedPath |
||
53 | * @param string $folder |
||
54 | */ |
||
55 | 8 | public function __construct(array $routes, string $defaultReturnType, string $method, string $requestedPath, string $folder='') |
|
62 | |||
63 | /** |
||
64 | * Remove subfolder from requestedPath if defined |
||
65 | * @param $requestPath |
||
66 | * @param $folder |
||
67 | * @return string |
||
68 | */ |
||
69 | 8 | private function extractFolder($requestPath, $folder) |
|
79 | |||
80 | /** |
||
81 | * Dispatch against the provided HTTP method verb and URI. |
||
82 | * @return array |
||
83 | */ |
||
84 | 3 | private function dispatcher() |
|
99 | |||
100 | /** |
||
101 | * Define Closures for all routes that returns controller info to be used. |
||
102 | * @param FastRoute\RouteCollector $route |
||
103 | */ |
||
104 | 3 | private function addRoutes(FastRoute\RouteCollector $route) |
|
116 | |||
117 | /** |
||
118 | * Get aliases and request uri from $routes |
||
119 | * @return array |
||
120 | */ |
||
121 | 4 | private function getAliases() |
|
131 | |||
132 | /** |
||
133 | * Get router data that includes route info and aliases |
||
134 | */ |
||
135 | 3 | public function getRoute() |
|
145 | |||
146 | /** |
||
147 | * Get route info for requested uri |
||
148 | * @param array $routeInfo |
||
149 | * @return array $routerData |
||
150 | */ |
||
151 | 3 | private function runDispatcher(array $routeInfo) |
|
167 | |||
168 | /** |
||
169 | * Get routeData according to dispatcher's results |
||
170 | * @param array $routeInfo |
||
171 | * @return array |
||
172 | */ |
||
173 | 3 | private function getRouteData(array $routeInfo) |
|
188 | } |