1 | <?php |
||
5 | class Route |
||
6 | { |
||
7 | /** |
||
8 | * Application Object |
||
9 | * |
||
10 | * @var \System\Application |
||
11 | */ |
||
12 | private $app; |
||
13 | |||
14 | /** |
||
15 | * Routes |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | private $routes = []; |
||
20 | |||
21 | /** |
||
22 | * Current route |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | public $current = []; |
||
27 | |||
28 | /** |
||
29 | * Current page |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | private $page; |
||
34 | |||
35 | /** |
||
36 | * Prefix |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $prefix; |
||
41 | |||
42 | /** |
||
43 | * Controller |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | private $basController; |
||
48 | |||
49 | /** |
||
50 | * Middleware |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | private $middleware = []; |
||
55 | |||
56 | /** |
||
57 | * Constructor |
||
58 | * |
||
59 | * @param \System\Application $app |
||
60 | */ |
||
61 | public function __construct(Application $app) |
||
65 | |||
66 | /** |
||
67 | * Add route to $routes after processing each parameter |
||
68 | * |
||
69 | * @param string $url |
||
70 | * @param string $action |
||
71 | * @param string|array $requestMethods |
||
72 | * @param string|array $middleware |
||
73 | * @return object $this |
||
74 | */ |
||
75 | private function add(string $url, string $action, $requestMethods, $middleware = []) |
||
92 | |||
93 | /** |
||
94 | * Add route GET to $routes |
||
95 | * |
||
96 | * @param string $url |
||
97 | * @param string $action |
||
98 | * @param string|array $middleware |
||
99 | * @return object $this |
||
100 | */ |
||
101 | public function get(string $url, string $action, $middleware = []) |
||
106 | |||
107 | /** |
||
108 | * Add route POST to $routes |
||
109 | * |
||
110 | * @param string $url |
||
111 | * @param string $action |
||
112 | * @param string|array $middleware |
||
113 | * @return object $this |
||
114 | */ |
||
115 | public function post(string $url, string $action, $middleware = []) |
||
120 | |||
121 | /** |
||
122 | * Add prefix at the first of $url if exists or not equal to '/' |
||
123 | * |
||
124 | * @param string $url |
||
125 | * @return string $url |
||
126 | */ |
||
127 | private function setPrefix($url) |
||
135 | |||
136 | /** |
||
137 | * Add basController at the first of $action if exists |
||
138 | * |
||
139 | * @param string $action |
||
140 | * @return string $action |
||
141 | */ |
||
142 | private function setAction($action) |
||
149 | |||
150 | /** |
||
151 | * Merge the given middleware if exists |
||
152 | * |
||
153 | * @param string $middleware |
||
154 | * @return array |
||
155 | */ |
||
156 | private function setMiddleware($middleware) |
||
163 | |||
164 | /** |
||
165 | * Clean the given path to the right controller and method |
||
166 | * |
||
167 | * @param string $action |
||
168 | * @return array $action |
||
169 | */ |
||
170 | private function getAction($action) |
||
178 | |||
179 | /** |
||
180 | * generate a pattern using regex |
||
181 | * |
||
182 | * @param string $url |
||
183 | * @return string $pattern |
||
184 | */ |
||
185 | private function generatePattern($url) |
||
193 | |||
194 | /** |
||
195 | * Check if the both methods are true |
||
196 | * |
||
197 | * @param string $pattern |
||
198 | * @param string $methods |
||
199 | * @return bool |
||
200 | */ |
||
201 | private function fullMatch($pattern, $methods) |
||
205 | |||
206 | /** |
||
207 | * Check if the url of the requesting page is matching the given pattern |
||
208 | * |
||
209 | * @property object $url |
||
210 | * @param string $pattern |
||
211 | * @return bool |
||
212 | */ |
||
213 | private function isMatchingPattern($pattern) |
||
219 | |||
220 | /** |
||
221 | * Get the rest parameter of the url as paramter for |
||
222 | * the method |
||
223 | * |
||
224 | * @property object $url |
||
225 | * @param string $pattern |
||
226 | * @return bool |
||
227 | */ |
||
228 | private function getArgumentsFor($pattern) |
||
238 | |||
239 | /** |
||
240 | * Group routes together |
||
241 | * |
||
242 | * @param array $groupOptions |
||
243 | * @param callable $callback |
||
244 | * @return object $this |
||
245 | */ |
||
246 | public function group(array $groupOptions, callable $callback) |
||
264 | |||
265 | /** |
||
266 | * Set a complate package |
||
267 | * |
||
268 | * @param string $url |
||
269 | * @param string $controller |
||
270 | * @param array $middlewares |
||
271 | * @return object $this |
||
272 | */ |
||
273 | public function package(string $url, string $controller, array $middlewares = []) |
||
284 | |||
285 | /** |
||
286 | * Loop over the routes and find the right one: |
||
287 | * found: break the loop and check if the request can continue by calling the middleware |
||
288 | * can be continue: call the right page |
||
289 | * can't be continue: ..... |
||
290 | * not found: call the 404 page |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | public function getProperRoute() |
||
318 | |||
319 | /** |
||
320 | * Get current route |
||
321 | * |
||
322 | * @return array $current |
||
323 | */ |
||
324 | public function getCurrent() |
||
328 | |||
329 | /** |
||
330 | * Get current page |
||
331 | * |
||
332 | * @return string $current |
||
333 | */ |
||
334 | public function getPage() |
||
341 | } |
||
342 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.