1 | <?php |
||
52 | class Router implements SingletonInterface |
||
53 | { |
||
54 | /** @var RouteCollector FastRoute, null if usingCache is set */ |
||
55 | private $routeCollector = null; |
||
56 | |||
57 | /** @var array List of middlewares called using the middleware() method. */ |
||
58 | private $currentMiddlewares = []; |
||
59 | |||
60 | /** @var string List of group prefixes called using the group() method. */ |
||
61 | private $currentGroupPrefix; |
||
62 | |||
63 | /** @var Context The current context */ |
||
64 | private $context; |
||
65 | |||
66 | /** |
||
67 | * Router constructor. |
||
68 | * |
||
69 | * @param Context $context |
||
70 | */ |
||
71 | public function __construct(Context $context) |
||
80 | |||
81 | /** |
||
82 | * Load every PHP Routes files under the directory |
||
83 | * |
||
84 | * @param string $path |
||
85 | */ |
||
86 | public function loadPath(string $path) : void |
||
105 | |||
106 | /** |
||
107 | * Encapsulate all the routes that are added from $func(Router) with this middleware. |
||
108 | * |
||
109 | * If the return value of the middleware is false, throws a RouteMiddlewareFailedException. |
||
110 | * |
||
111 | * @param string $middleware_class The middleware to use |
||
112 | * @param string $method_name The method of the singleton to call |
||
113 | * @param callable $func |
||
114 | */ |
||
115 | public function middleware(string $middleware_class, string $method_name, callable $func) : void |
||
127 | |||
128 | /** |
||
129 | * Adds a prefix in front of all the encapsulated routes. |
||
130 | * |
||
131 | * @param string $prefix The prefix of the group. |
||
132 | * @param callable $func |
||
133 | */ |
||
134 | public function group(string $prefix, callable $func) : void |
||
143 | |||
144 | /** |
||
145 | * Dispatch the request to the router. |
||
146 | * |
||
147 | * @return Route |
||
148 | * @throws MethodNotAllowedException if the request method is not supported, but others are for this route. |
||
149 | * @throws RouteNotFoundException if the requested route did not match any routes. |
||
150 | */ |
||
151 | public function dispatch() : Route |
||
186 | |||
187 | /** |
||
188 | * Add a new route. |
||
189 | * |
||
190 | * @param string|string[] $http_method The HTTP method, example: GET, HEAD, POST, PATCH, PUT, DELETE, CLI, etc. Can be an array of values. |
||
191 | * @param string $route The route |
||
192 | * @param string $controller_class The Controller's class |
||
193 | * @param array $parameters The parameters to pass |
||
194 | */ |
||
195 | public function addRoute($http_method, string $route, string $controller_class, array $parameters = []) : void |
||
205 | |||
206 | /** |
||
207 | * Add a new route with GET as HTTP method. |
||
208 | * |
||
209 | * @param string $route The route |
||
210 | * @param string $controller_class The Controller's class |
||
211 | * @param array $parameters The parameters to pass |
||
212 | */ |
||
213 | public function get(string $route, string $controller_class, array $parameters = []) : void |
||
217 | |||
218 | /** |
||
219 | * Add a new route with HEAD as HTTP method. |
||
220 | * |
||
221 | * @param string $route The route |
||
222 | * @param string $controller_class The Controller's class |
||
223 | * @param array $parameters The parameters to pass |
||
224 | */ |
||
225 | public function head(string $route, string $controller_class, array $parameters = []) : void |
||
229 | |||
230 | /** |
||
231 | * Add a new route with POST as HTTP method. |
||
232 | * |
||
233 | * @param string $route The route |
||
234 | * @param string $controller_class The Controller's class |
||
235 | * @param array $parameters The parameters to pass |
||
236 | */ |
||
237 | public function post(string $route, string $controller_class, array $parameters = []) : void |
||
241 | |||
242 | /** |
||
243 | * Add a new route with PUT as HTTP method. |
||
244 | * |
||
245 | * @param string $route The route |
||
246 | * @param string $controller_class The Controller's class |
||
247 | * @param array $parameters The parameters to pass |
||
248 | */ |
||
249 | public function put(string $route, string $controller_class, array $parameters = []) : void |
||
253 | |||
254 | /** |
||
255 | * Add a new route with PATCH as HTTP method. |
||
256 | * |
||
257 | * @param string $route The route |
||
258 | * @param string $controller_class The Controller's class |
||
259 | * @param array $parameters The parameters to pass |
||
260 | */ |
||
261 | public function patch(string $route, string $controller_class, array $parameters = []) : void |
||
265 | |||
266 | /** |
||
267 | * Add a new route with DELETE as HTTP method. |
||
268 | * |
||
269 | * @param string $route The route |
||
270 | * @param string $controller_class The Controller's class |
||
271 | * @param array $parameters The parameters to pass |
||
272 | */ |
||
273 | public function delete(string $route, string $controller_class, array $parameters = []) : void |
||
277 | |||
278 | /** |
||
279 | * Add a new route with CLI as method. |
||
280 | * |
||
281 | * @param string $route The route |
||
282 | * @param string $controller_class The Controller's class |
||
283 | * @param array $parameters The parameters to pass |
||
284 | */ |
||
285 | public function cli(string $route, string $controller_class, array $parameters = []) : void |
||
289 | |||
290 | /** |
||
291 | * @return Dispatcher\GroupCountBased |
||
292 | */ |
||
293 | private function getDispatcher() : Dispatcher\GroupCountBased |
||
297 | |||
298 | /** |
||
299 | * Returns a parsable URI |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | private function detectUri() : string |
||
324 | |||
325 | /** |
||
326 | * Return the base URI for a request |
||
327 | * |
||
328 | * @return string |
||
329 | */ |
||
330 | private function getBaseUri() : string |
||
346 | |||
347 | /** |
||
348 | * Throws an exception or return void. |
||
349 | * |
||
350 | * @param array $middlewares |
||
351 | * @param Route $route |
||
352 | * |
||
353 | * @return void |
||
354 | * @throws RouteMiddlewareFailedException if a route middleware returned false. |
||
355 | * @throws InvalidMiddlewareException if a middleware is invalid. |
||
356 | */ |
||
357 | private function handleMiddlewares(array $middlewares, Route $route) : void |
||
377 | } |
||
378 |