1 | <?php |
||
17 | class Router extends Dispatcher |
||
18 | { |
||
19 | const DEFAULT_SCHEME = 'http'; |
||
20 | const HTTP_PORT = 80; |
||
21 | const HTTPS_PORT = 443; |
||
22 | |||
23 | private $computed = false; |
||
24 | private $host = ''; |
||
25 | private $rawRoutes = []; |
||
26 | private $routesNames = []; |
||
27 | private $routeCollector; |
||
28 | private $scheme = self::DEFAULT_SCHEME; |
||
29 | |||
30 | /** |
||
31 | * Creates an instance of Router. |
||
32 | * |
||
33 | * Required to disable FastRoute\Dispatcher\GroupCountBased constructor. |
||
34 | */ |
||
35 | public function __construct() |
||
38 | |||
39 | /** |
||
40 | * Adds a new route to the collection. |
||
41 | * |
||
42 | * We highly recommend you to use ::beginRoute() instead. |
||
43 | * {@see ::beginRoute()} |
||
44 | * |
||
45 | * @param Route $route |
||
46 | * @return self |
||
47 | */ |
||
48 | public function addRoute(Route $route): void |
||
57 | |||
58 | /** |
||
59 | * This is an helper that provides you a smooth syntax to add new route. Example: |
||
60 | * |
||
61 | * $router |
||
62 | * ->beginRoute('hello_world') |
||
63 | * ->setPattern('/hello/world') |
||
64 | * ->setHandler(function () { |
||
65 | * return 'Hello, world!'; |
||
66 | * }) |
||
67 | * ->end() |
||
68 | * ; |
||
69 | * |
||
70 | * This syntax avoids you to create a new intance of Route, hydrating it and |
||
71 | * then adding it to Router. |
||
72 | * |
||
73 | * @param string|null $name |
||
74 | * @return Route |
||
75 | */ |
||
76 | public function beginRoute(string $name = null): Route |
||
80 | |||
81 | /** |
||
82 | * Generates and returns the full URL (with scheme and host) with provided URI. |
||
83 | * |
||
84 | * Notes that this method required at least the host to be setted. |
||
85 | * |
||
86 | * @param string $uri |
||
87 | * @return string |
||
88 | */ |
||
89 | public function url(string $uri): string |
||
99 | |||
100 | /** |
||
101 | * Returns the current scheme. |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | public function scheme(): string |
||
109 | |||
110 | /** |
||
111 | * Sets the new scheme to use. Calling this method without parameter will reset |
||
112 | * it to 'http'. |
||
113 | * |
||
114 | * @param string|null $scheme |
||
115 | */ |
||
116 | public function setScheme(string $scheme = null): void |
||
120 | |||
121 | /** |
||
122 | * Returns the setted host. |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | public function host(): string |
||
130 | |||
131 | /** |
||
132 | * Sets new host to Router. Calling this method without parameter will reset |
||
133 | * the host to empty string. |
||
134 | * |
||
135 | * @param string|null $host |
||
136 | * @return self |
||
137 | */ |
||
138 | public function setHost(string $host = null): void |
||
142 | |||
143 | /** |
||
144 | * Uses the provided request to guess the host. This method also set the |
||
145 | * |
||
146 | * @param Request $request |
||
147 | * @return self |
||
148 | */ |
||
149 | public function guessHost(Request $request): void |
||
157 | |||
158 | /** |
||
159 | * Generates URI associated to provided route name. |
||
160 | * |
||
161 | * @param string $name The URI route name we want to generate |
||
162 | * @param array $params Parameters to replace in pattern |
||
163 | * @return string |
||
164 | * @throws \InvalidArgumentException if provided route name is unknown |
||
165 | */ |
||
166 | public function uri(string $name, array $params = []): string |
||
193 | |||
194 | /** |
||
195 | * Matches the given HTTP method and URI to the route collection and returns |
||
196 | * the callback with the array of arguments to use. |
||
197 | * |
||
198 | * @param string $method |
||
199 | * @param string $uri |
||
200 | * @return array |
||
201 | */ |
||
202 | public function match(string $method, string $uri): array |
||
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | * Overrides GroupCountBased::dispatch() to ensure that dispatcher always deals with up-to-date |
||
225 | * route collection. |
||
226 | */ |
||
227 | public function dispatch($method, $uri): array |
||
233 | |||
234 | /** |
||
235 | * Will always return the right RouteCollector and knows when to recompute it. |
||
236 | * |
||
237 | * @return RouteCollector |
||
238 | */ |
||
239 | private function routeCollector(): RouteCollector |
||
253 | } |
||
254 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.