|
1
|
|
|
<?php |
|
2
|
|
|
namespace Mezon\Router; |
|
3
|
|
|
|
|
4
|
|
|
trait RoutesSet |
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* List off routes for all supported request methods |
|
9
|
|
|
*/ |
|
10
|
|
|
private $routes = [ |
|
11
|
|
|
'GET' => [], |
|
12
|
|
|
'POST' => [], |
|
13
|
|
|
'PUT' => [], |
|
14
|
|
|
'DELETE' => [], |
|
15
|
|
|
'OPTION' => [] |
|
16
|
|
|
]; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Route names |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private $routeNames = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* This flag rises when we add route / * / |
|
27
|
|
|
* |
|
28
|
|
|
* @var bool |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $universalRouteWasAdded = false; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Validating that request method is supported within this router. |
|
34
|
|
|
* If it is not supported then Exception will be thrown |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $requestMethod |
|
37
|
|
|
* request method |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function validateSupportedRequestMethod(string $requestMethod): void |
|
40
|
|
|
{ |
|
41
|
|
|
if (isset($this->routes[$requestMethod]) === false) { |
|
42
|
|
|
throw (new \Exception('Unsupported request method')); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Method returns list of routes for the HTTP method. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $requestMethod |
|
50
|
|
|
* HTTP request method |
|
51
|
|
|
* @return array Routes |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function &getRoutesForMethod(string $requestMethod): array |
|
54
|
|
|
{ |
|
55
|
|
|
$this->validateSupportedRequestMethod($requestMethod); |
|
56
|
|
|
|
|
57
|
|
|
return $this->routes[$requestMethod]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Method returns a list of supported request methods |
|
62
|
|
|
* |
|
63
|
|
|
* @return array list of supported request methods |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getListOfSupportedRequestMethods(): array |
|
66
|
|
|
{ |
|
67
|
|
|
return [ |
|
68
|
|
|
'GET', |
|
69
|
|
|
'POST', |
|
70
|
|
|
'PUT', |
|
71
|
|
|
'DELETE', |
|
72
|
|
|
'OPTION' |
|
73
|
|
|
]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Method clears router data. |
|
78
|
|
|
*/ |
|
79
|
|
|
public function clear() |
|
80
|
|
|
{ |
|
81
|
|
|
$this->universalRouteWasAdded = false; |
|
82
|
|
|
|
|
83
|
|
|
$this->routeNames = []; |
|
84
|
|
|
|
|
85
|
|
|
foreach ($this->getListOfSupportedRequestMethods() as $requestMethod) { |
|
86
|
|
|
$this->routes[$requestMethod] = []; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Method returns true if the router exists |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $route |
|
94
|
|
|
* checking route |
|
95
|
|
|
* @return bool true if the router exists, false otherwise |
|
96
|
|
|
*/ |
|
97
|
|
|
public function routeExists(string $route): bool |
|
98
|
|
|
{ |
|
99
|
|
|
$route = trim($route, '/'); |
|
100
|
|
|
|
|
101
|
|
|
foreach ($this->getListOfSupportedRequestMethods() as $requestMethod) { |
|
102
|
|
|
if (isset($this->routes[$requestMethod][$route])) { |
|
103
|
|
|
return true; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Method rturns all available routes |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getAllRoutesTrace() |
|
114
|
|
|
{ |
|
115
|
|
|
$trace = []; |
|
116
|
|
|
|
|
117
|
|
|
foreach ($this->getListOfSupportedRequestMethods() as $requestMethod) { |
|
118
|
|
|
if (count($this->routes[$requestMethod]) > 0) { |
|
119
|
|
|
$trace[] = $requestMethod . ':' . implode(', ', array_keys($this->routes[$requestMethod])); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return implode('; ', $trace); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Additing route for GET request |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $route |
|
130
|
|
|
* route |
|
131
|
|
|
* @param object $object |
|
132
|
|
|
* callback object |
|
133
|
|
|
* @param string $method |
|
134
|
|
|
* callback method |
|
135
|
|
|
*/ |
|
136
|
|
|
public function addGetRoute(string $route, object $object, string $method): void |
|
137
|
|
|
{ |
|
138
|
|
|
$this->routes['GET'][trim($route, '/')] = [ |
|
139
|
|
|
$object, |
|
140
|
|
|
$method |
|
141
|
|
|
]; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Additing route for GET request |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $route |
|
148
|
|
|
* route |
|
149
|
|
|
* @param object $object |
|
150
|
|
|
* callback object |
|
151
|
|
|
* @param string $method |
|
152
|
|
|
* callback method |
|
153
|
|
|
*/ |
|
154
|
|
|
public function addPostRoute(string $route, object $object, string $method): void |
|
155
|
|
|
{ |
|
156
|
|
|
$this->routes['POST'][trim($route, '/')] = [ |
|
157
|
|
|
$object, |
|
158
|
|
|
$method |
|
159
|
|
|
]; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Method registers name of the route |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $routeName |
|
166
|
|
|
* route's name |
|
167
|
|
|
* @param string $route |
|
168
|
|
|
* route |
|
169
|
|
|
*/ |
|
170
|
|
|
protected function registerRouteName(string $routeName, string $route): void |
|
171
|
|
|
{ |
|
172
|
|
|
if ($routeName != '') { |
|
173
|
|
|
$this->routeNames[$routeName] = $route; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Validating that route name exists |
|
179
|
|
|
* |
|
180
|
|
|
* @param string $routeName |
|
181
|
|
|
* @return bool |
|
182
|
|
|
*/ |
|
183
|
|
|
protected function routeNameExists(string $routeName): bool |
|
184
|
|
|
{ |
|
185
|
|
|
return isset($this->routeNames[$routeName]); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Getting route by name |
|
190
|
|
|
* |
|
191
|
|
|
* @param string $routeName |
|
192
|
|
|
* route's name |
|
193
|
|
|
* @return string route |
|
194
|
|
|
*/ |
|
195
|
|
|
public function getRouteByName(string $routeName): string |
|
196
|
|
|
{ |
|
197
|
|
|
if ($this->routeNameExists($routeName) === false) { |
|
198
|
|
|
throw (new \Exception('Route with name ' . $routeName . ' does not exist')); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return $this->routeNames[$routeName]; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|