|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
namespace Mezon\Router; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Trait StaticRoutes |
|
7
|
|
|
* |
|
8
|
|
|
* @package Router |
|
9
|
|
|
* @author Dodonov A.A. |
|
10
|
|
|
* @version v.1.0 (2021/09/27) |
|
11
|
|
|
* @copyright Copyright (c) 2021, aeon.org |
|
12
|
|
|
*/ |
|
13
|
|
|
trait StaticRoutes |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* List of static routes for all supported request methods |
|
18
|
|
|
* |
|
19
|
|
|
* @var array<string, array<string, array{0: string, 1:string}|callable|string>> |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $staticRoutes = [ |
|
22
|
|
|
'GET' => [], |
|
23
|
|
|
'POST' => [], |
|
24
|
|
|
'PUT' => [], |
|
25
|
|
|
'DELETE' => [], |
|
26
|
|
|
'OPTION' => [], |
|
27
|
|
|
'PATCH' => [] |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Method returns static routes handlers for the specified request methods |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $requestMethod |
|
34
|
|
|
* request method |
|
35
|
|
|
* @return array<string, array{0: string, 1:string}|callable|string> handlers |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function getStaticRoutes(string $requestMethod): array |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
$requestMethod = (string) $_SERVER['REQUEST_METHOD']; |
|
40
|
|
|
|
|
41
|
|
|
if (! isset($this->staticRoutes[$requestMethod])) { |
|
42
|
|
|
throw (new \Exception('Unsupported request method : ' . $requestMethod, - 1)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $this->staticRoutes[$requestMethod]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Method returns route handler |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $route |
|
52
|
|
|
* routes |
|
53
|
|
|
* @return array{0: string, 1: string}|callable|false|string route handler |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function getStaticRouteProcessor(string $route) |
|
56
|
|
|
{ |
|
57
|
|
|
$processors = $this->getStaticRoutes((string) $_SERVER['REQUEST_METHOD']); |
|
58
|
|
|
|
|
59
|
|
|
if (isset($processors[$route])) { |
|
60
|
|
|
$this->calledRoute = $route; |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
return $processors[$route]; |
|
63
|
|
|
} else { |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Method returns route handler |
|
70
|
|
|
* |
|
71
|
|
|
* @return array{0: string, 1: string}|callable|false|string route handler |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function getUniversalRouteProcessor() |
|
74
|
|
|
{ |
|
75
|
|
|
$processors = $this->getStaticRoutes((string) $_SERVER['REQUEST_METHOD']); |
|
76
|
|
|
|
|
77
|
|
|
if (isset($processors['*'])) { |
|
78
|
|
|
$this->calledRoute = '*'; |
|
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
return $processors['*']; |
|
81
|
|
|
} else { |
|
82
|
|
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Method searches route processor |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $route |
|
91
|
|
|
* route |
|
92
|
|
|
* @return mixed|false result of the router processor |
|
93
|
|
|
*/ |
|
94
|
|
|
public function findStaticRouteProcessor(string $route) |
|
95
|
|
|
{ |
|
96
|
|
|
$processor = $this->getStaticRouteProcessor($route); |
|
97
|
|
|
|
|
98
|
|
|
if ($processor === false) { |
|
99
|
|
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this->executeHandler($processor, $route); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Method searches universal route processor |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $route |
|
109
|
|
|
* route |
|
110
|
|
|
* @return mixed|false result of the router processor |
|
111
|
|
|
*/ |
|
112
|
|
|
public function findUniversalRouteProcessor(string $route) |
|
113
|
|
|
{ |
|
114
|
|
|
$processor = $this->getUniversalRouteProcessor(); |
|
115
|
|
|
|
|
116
|
|
|
if ($processor === false) { |
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $this->executeHandler($processor, $route); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.