Skip to content

Commit c1ffd72

Browse files
committed
completed the route map implementation
1 parent 6723504 commit c1ffd72

File tree

12 files changed

+379
-449
lines changed

12 files changed

+379
-449
lines changed

.env

Whitespace-only changes.

Router/Route.php

+81-104
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ class Route extends Controller implements RouteInterface
8282
*
8383
* ------------------------------------------------------------------------
8484
*/
85-
public static function any (
86-
array|string $route,
87-
mixed $callback,
88-
string $method = '*',
85+
public static function any(
86+
array|string $route,
87+
mixed $callback,
88+
string $method = '*',
8989
): self {
9090
self::$any = [
91-
'route' => $route,
92-
'method' => $method,
93-
'callback' => $callback,
91+
'route' => $route,
92+
'method' => $method,
93+
'callback' => $callback,
9494
];
9595

9696
self::$route[] = $route;
@@ -105,11 +105,11 @@ public static function any (
105105
* @param string $method Request method
106106
* @param string|array $route Route parameter
107107
*/
108-
public static function map (string $method, string|array $route): self
108+
public static function map(string $method, string|array $route): self
109109
{
110110
self::$map = [
111-
'method' => $method,
112-
'route' => $route,
111+
'method' => $method,
112+
'route' => $route,
113113
];
114114
self::$route[] = $route;
115115
return new self();
@@ -121,18 +121,14 @@ public static function map (string $method, string|array $route): self
121121
*
122122
* @param string $name Set the name of the route
123123
*/
124-
public function name (string $name): self
124+
public function name(string $name): self
125125
{
126-
if (is_array(end(self::$route)))
127-
{
128-
for ($i = 0; $i < count(end(self::$route)); $i++)
129-
{
126+
if (is_array(end(self::$route))) {
127+
for ($i = 0; $i < count(end(self::$route)); $i++) {
130128
add_route_name("$name::$i", end(self::$route)[$i]);
131129
self::$routes["$name::$i"] = end(self::$route)[$i];
132130
}
133-
}
134-
else
135-
{
131+
} else {
136132
add_route_name($name, end(self::$route));
137133
self::$routes[$name] = end(self::$route);
138134
}
@@ -146,20 +142,17 @@ public function name (string $name): self
146142
* @param string $route
147143
* @param Closure $callback
148144
*/
149-
public function route (string $route, Closure $callback): self
145+
public function route(string $route, callable $callback): self
150146
{
151-
$route = rtrim('/', self::$map['route']) . '/' . ltrim('/', $route);
147+
$route = rtrim(self::$map['route'], '/') . '/' . ltrim($route, '/');
152148

153-
if (self::$map)
154-
{
149+
if (self::$map) {
155150
$this->mapRoute = [
156-
'route' => $route,
157-
'method' => self::$map['method'],
158-
'callback' => $callback,
151+
'route' => $route,
152+
'method' => '*',
153+
'callback' => $callback,
159154
];
160-
}
161-
else
162-
{
155+
} else {
163156
throw new Exception('There is no map to route.');
164157
}
165158
self::$route[] = $route;
@@ -172,10 +165,9 @@ public function route (string $route, Closure $callback): self
172165
*
173166
* @param mixed $callback
174167
*/
175-
public function action (mixed $callback): self
168+
public function action(mixed $callback): self
176169
{
177-
if (self::$map)
178-
{
170+
if (self::$map) {
179171
$this->action = $callback;
180172
}
181173
return $this;
@@ -190,8 +182,7 @@ public function action (mixed $callback): self
190182
*/
191183
public function use(string $controller): self
192184
{
193-
if (self::$map)
194-
{
185+
if (self::$map) {
195186
$this->use = $controller;
196187
}
197188
return $this;
@@ -203,10 +194,9 @@ public function use(string $controller): self
203194
*
204195
* @param string $file
205196
*/
206-
public function file (string $file): self
197+
public function file(string $file): self
207198
{
208-
if (self::$map)
209-
{
199+
if (self::$map) {
210200
$this->file = $file;
211201
}
212202
return $this;
@@ -221,13 +211,13 @@ public function file (string $file): self
221211
* @param Closure $closure The closure to handle invalid parameter types.
222212
* @return self Returns the current instance for method chaining.
223213
*/
224-
public function handleInvalidParameterType (Closure $closure): self
214+
public function handleInvalidParameterType(Closure $closure): self
225215
{
226216
$this->handleInvalidParameterType = $closure;
227217
return $this;
228218
}
229-
230-
public function caseSensitive (): self
219+
220+
public function caseSensitive(): self
231221
{
232222
$this->caseSensitive = true;
233223
return $this;
@@ -239,10 +229,9 @@ public function caseSensitive (): self
239229
* @param string ...$guards String parameters of registered guards.
240230
* @return self
241231
*/
242-
public function withGuard (string ...$guards): self
232+
public function withGuard(string ...$guards): self
243233
{
244-
if (self::$map || self::$method || self::$view)
245-
{
234+
if (self::$map || self::$method || self::$view) {
246235
$this->guards = $guards;
247236
}
248237
return $this;
@@ -263,11 +252,11 @@ public function withGuard (string ...$guards): self
263252
*
264253
* ---------------------------------------------------------------------------
265254
*/
266-
public static function view (array|string $route, string $view): self
255+
public static function view(array|string $route, string $view): self
267256
{
268257
self::$view = [
269-
'route' => $route,
270-
'view' => $view,
258+
'route' => $route,
259+
'view' => $view,
271260
];
272261

273262
self::$route[] = $route;
@@ -287,15 +276,15 @@ public static function view (array|string $route, string $view): self
287276
*
288277
* ---------------------------------------------------------------
289278
*/
290-
public static function redirect (
291-
string $route,
292-
string $new_url,
293-
int $code = 302,
279+
public static function redirect(
280+
string $route,
281+
string $new_url,
282+
int $code = 302,
294283
): self {
295284
self::$redirect = [
296-
'route' => $route,
297-
'new_url' => $new_url,
298-
'code' => $code,
285+
'route' => $route,
286+
'new_url' => $new_url,
287+
'code' => $code,
299288
];
300289

301290
self::$route[] = $route;
@@ -311,12 +300,12 @@ public static function redirect (
311300
*
312301
* --------------------------------------------------------------
313302
*/
314-
public static function get (array|string $route, $callback): self
303+
public static function get(array|string $route, $callback): self
315304
{
316305
self::$method = [
317-
'route' => $route,
318-
'method' => 'GET',
319-
'callback' => $callback,
306+
'route' => $route,
307+
'method' => 'GET',
308+
'callback' => $callback,
320309
];
321310

322311
self::$route[] = $route;
@@ -332,12 +321,12 @@ public static function get (array|string $route, $callback): self
332321
*
333322
* --------------------------------------------------------------
334323
*/
335-
public static function post (array|string $route, $callback): self
324+
public static function post(array|string $route, $callback): self
336325
{
337326
self::$method = [
338-
'route' => $route,
339-
'method' => 'POST',
340-
'callback' => $callback,
327+
'route' => $route,
328+
'method' => 'POST',
329+
'callback' => $callback,
341330
];
342331

343332
self::$route[] = $route;
@@ -353,12 +342,12 @@ public static function post (array|string $route, $callback): self
353342
*
354343
* --------------------------------------------------------------
355344
*/
356-
public static function put (array|string $route, $callback): self
345+
public static function put(array|string $route, $callback): self
357346
{
358347
self::$method = [
359-
'route' => $route,
360-
'method' => 'PUT',
361-
'callback' => $callback,
348+
'route' => $route,
349+
'method' => 'PUT',
350+
'callback' => $callback,
362351
];
363352

364353
self::$route[] = $route;
@@ -374,12 +363,12 @@ public static function put (array|string $route, $callback): self
374363
*
375364
* --------------------------------------------------------------
376365
*/
377-
public static function patch (array|string $route, $callback): self
366+
public static function patch(array|string $route, $callback): self
378367
{
379368
self::$method = [
380-
'route' => $route,
381-
'method' => 'PATCH',
382-
'callback' => $callback,
369+
'route' => $route,
370+
'method' => 'PATCH',
371+
'callback' => $callback,
383372
];
384373

385374
self::$route[] = $route;
@@ -395,86 +384,74 @@ public static function patch (array|string $route, $callback): self
395384
*
396385
* --------------------------------------------------------------
397386
*/
398-
public static function delete (array|string $route, $callback): self
387+
public static function delete(array|string $route, $callback): self
399388
{
400389
self::$method = [
401-
'route' => $route,
402-
'method' => 'DELETE',
403-
'callback' => $callback,
390+
'route' => $route,
391+
'method' => 'DELETE',
392+
'callback' => $callback,
404393
];
405394

406395
self::$route[] = $route;
407396
return new self();
408397
}
409398

410-
public function __destruct ()
399+
public function __destruct()
411400
{
412401
$route_index = end(self::$route);
413402
$route_index = is_array($route_index) ? $route_index[0] : $route_index;
414403

415-
$GLOBALS['__registered_routes'][$route_index][
416-
'caseSensitive'
417-
] = $this->caseSensitive;
404+
$GLOBALS['__registered_routes'][$route_index]['caseSensitive'] =
405+
$this->caseSensitive;
418406

419-
if (self::$map !== null)
420-
{
407+
if (self::$map !== null) {
421408
$GLOBALS['__registered_routes'][$route_index]['map'] = self::$map;
422409
}
423410

424-
if ($this->guards !== null)
425-
{
411+
if ($this->guards !== null) {
426412
$GLOBALS['__registered_routes'][$route_index]['guards'] =
427-
$this->guards;
413+
$this->guards;
428414
}
429415

430-
if (self::$redirect !== null)
431-
{
416+
if (self::$redirect !== null) {
432417
$GLOBALS['__registered_routes'][$route_index]['redirect'] =
433-
self::$redirect;
418+
self::$redirect;
434419
}
435420

436-
if ($this->action !== null)
437-
{
421+
if ($this->action !== null) {
438422
$GLOBALS['__registered_routes'][$route_index]['action'] =
439-
$this->action;
423+
$this->action;
440424
}
441425

442-
if ($this->mapRoute !== null)
443-
{
426+
if ($this->mapRoute !== null) {
444427
$GLOBALS['__registered_routes'][$route_index]['mapRoute'] =
445-
$this->mapRoute;
428+
$this->mapRoute;
446429
}
447430

448-
if (self::$any !== null)
449-
{
431+
if (self::$any !== null) {
450432
$GLOBALS['__registered_routes'][$route_index]['any'] = self::$any;
451433
}
452434

453-
if ($this->use !== null)
454-
{
435+
if ($this->use !== null) {
455436
$GLOBALS['__registered_routes'][$route_index]['use'] = $this->use;
456437
}
457438

458-
if ($this->file !== null)
459-
{
439+
if ($this->file !== null) {
460440
$GLOBALS['__registered_routes'][$route_index]['file'] = $this->file;
461441
}
462442

463-
if ($this->handleInvalidParameterType !== null)
464-
{
443+
if ($this->handleInvalidParameterType !== null) {
465444
$GLOBALS['__registered_routes'][$route_index][
466-
'handleInvalidParameterType'
445+
'handleInvalidParameterType'
467446
] = $this->handleInvalidParameterType;
468447
}
469448

470-
if (self::$method !== null)
471-
{
449+
if (self::$method !== null) {
472450
$GLOBALS['__registered_routes'][$route_index]['method'] =
473-
self::$method;
451+
self::$method;
474452
}
475453

476-
if (self::$view !== null)
477-
{
454+
if (self::$view !== null) {
478455
$GLOBALS['__registered_routes'][$route_index]['view'] = self::$view;
479456
}
480457
}

src/Config/jwt.config.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
use PhpSlides\Core\Foundation\Application;
66

77
return (new FileLoader())
8-
->safeLoad(Application::$configsDir . 'jwt.php')
9-
->getLoad() ?:
10-
[];
8+
->safeLoad(Application::$configsDir . 'jwt.php')
9+
->getLoad() ?:
10+
[];

0 commit comments

Comments
 (0)