1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Erykai\Routes; |
4
|
|
|
|
5
|
|
|
use RuntimeException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Trait Controller Router |
9
|
|
|
*/ |
10
|
|
|
trait TraitRoute |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var object |
14
|
|
|
*/ |
15
|
|
|
private object $response; |
16
|
|
|
/** |
17
|
|
|
* @param array $array |
18
|
|
|
* @return object |
19
|
|
|
*/ |
20
|
|
|
private function duplicates(array $array): object |
21
|
|
|
{ |
22
|
|
|
if ($this->getRequest() !== "") { |
23
|
|
|
foreach ($array as $key => $item) { |
24
|
|
|
if ($this->verb[$key] !== $this->getMethod()) { |
25
|
|
|
unset( |
26
|
|
|
$this->controller[$key], |
27
|
|
|
$this->middleware[$key], |
28
|
|
|
$this->type[$key], |
29
|
|
|
$this->verb[$key], |
30
|
|
|
$this->route[$key], |
31
|
|
|
$this->patterns[$key] |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
/** |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
private function patterns(): bool |
42
|
|
|
{ |
43
|
|
|
$patterns = array_unique($this->getPatterns()); |
44
|
|
|
if ($this->getRequest() === "") { |
45
|
|
|
$patterns = (array)$patterns[0]; |
46
|
|
|
} |
47
|
|
|
$this->keyRouter($patterns); |
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
/** |
51
|
|
|
* @param $pattern |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
private function pregMatch($pattern): mixed |
55
|
|
|
{ |
56
|
|
|
if (preg_match('#' . $pattern . '#', $this->getRequest(), $router)) { |
57
|
|
|
throw new RuntimeException('Error ' . $pattern . ' ' . $this->getRequest()); |
58
|
|
|
} |
59
|
|
|
return $router; |
60
|
|
|
} |
61
|
|
|
/** |
62
|
|
|
* @param $patterns |
63
|
|
|
*/ |
64
|
|
|
private function keyRouter($patterns): void |
65
|
|
|
{ |
66
|
|
|
foreach ($patterns as $key => $pattern) { |
67
|
|
|
if ($this->getMethod() === $this->verb[$key]) { |
68
|
|
|
$router = $this->pregMatch($pattern); |
69
|
|
|
if (isset($router[0]) && $router[0] === $this->getRequest()) { |
70
|
|
|
$this->setClass($key, $router); |
71
|
|
|
} else if ($this->getRequest() === "") { |
72
|
|
|
$this->setClass($key, $router); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
/** |
79
|
|
|
* @param $key |
80
|
|
|
* @param $router |
81
|
|
|
*/ |
82
|
|
|
private function setClass($key, $router): void |
83
|
|
|
{ |
84
|
|
|
$classMethod = explode("@", $this->controller[$key]); |
85
|
|
|
[$class, $method] = $classMethod; |
86
|
|
|
array_shift($router); |
87
|
|
|
if (preg_match_all('~{([^}]*)}~', $this->route[$key], $keys) === false) { |
88
|
|
|
throw new RuntimeException('The route ' . $this->route[$key] . ' not exist.'); |
89
|
|
|
} |
90
|
|
|
$data = array_combine($keys[1], $router); |
91
|
|
|
$this->classMethod($class, $method, $data, $key); |
92
|
|
|
} |
93
|
|
|
/** |
94
|
|
|
* @param $class |
95
|
|
|
* @param $method |
96
|
|
|
* @param $data |
97
|
|
|
* @param $key |
98
|
|
|
* @return bool|void |
99
|
|
|
*/ |
100
|
|
|
private function classMethod($class, $method, $data, $key) |
101
|
|
|
{ |
102
|
|
|
$argument = $data; |
103
|
|
|
unset($data); |
104
|
|
|
$data['argument'] = $argument; |
105
|
|
|
$class = $this->namespaceArray[$key] . "\\" . $class; |
106
|
|
|
if (class_exists($class)) { |
107
|
|
|
$Class = new $class; |
108
|
|
|
if (method_exists($Class, $method)) { |
109
|
|
|
$this->setNotFound(false); |
|
|
|
|
110
|
|
|
$data['query'] = $this->getQuery(); |
111
|
|
|
if ($this->middleware[$key]) { |
112
|
|
|
$Middleware = new Middleware(); |
113
|
|
|
if (!$Middleware->validate()) { |
114
|
|
|
$this->setResponse( |
115
|
|
|
401, |
116
|
|
|
"error", |
117
|
|
|
"this access is mandatory to inform the correct Baren Token" |
118
|
|
|
); |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
$Class->$method($data, $this->type[$key]); |
123
|
|
|
return true; |
124
|
|
|
} |
125
|
|
|
$this->setResponse( |
126
|
|
|
405, |
127
|
|
|
"error", |
128
|
|
|
"the {$this->controller[$key]} method does not exist", |
129
|
|
|
dynamic: $this->controller[$key] |
130
|
|
|
); |
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
/** |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
protected function controller(): bool |
138
|
|
|
{ |
139
|
|
|
$this->duplicates($this->getPatterns()); |
140
|
|
|
$this->patterns(); |
141
|
|
|
if (empty($this->getPatterns())) { |
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
return true; |
145
|
|
|
} |
146
|
|
|
/** |
147
|
|
|
* @return array |
148
|
|
|
*/ |
149
|
|
|
protected function getPatterns(): array |
150
|
|
|
{ |
151
|
|
|
return $this->patterns; |
152
|
|
|
} |
153
|
|
|
/** |
154
|
|
|
* @return object |
155
|
|
|
*/ |
156
|
|
|
protected function getResponse(): object |
157
|
|
|
{ |
158
|
|
|
return $this->response; |
159
|
|
|
} |
160
|
|
|
/** |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
protected function getMethod(): string |
164
|
|
|
{ |
165
|
|
|
return $this->method; |
166
|
|
|
} |
167
|
|
|
/** |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
protected function getRequest(): string |
171
|
|
|
{ |
172
|
|
|
return $this->request; |
173
|
|
|
} |
174
|
|
|
/** |
175
|
|
|
* @return array|null |
176
|
|
|
*/ |
177
|
|
|
protected function getQuery(): ?array |
178
|
|
|
{ |
179
|
|
|
return $this->query; |
180
|
|
|
} |
181
|
|
|
/** |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
protected function getNamespace(): string |
185
|
|
|
{ |
186
|
|
|
return $this->namespace; |
187
|
|
|
} |
188
|
|
|
/** |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
protected function getRoute(): array |
192
|
|
|
{ |
193
|
|
|
return $this->route; |
194
|
|
|
} |
195
|
|
|
/** |
196
|
|
|
* @return bool |
197
|
|
|
*/ |
198
|
|
|
protected function isNotFound(): bool |
199
|
|
|
{ |
200
|
|
|
return $this->notFound; |
201
|
|
|
} |
202
|
|
|
/** |
203
|
|
|
* @param int $code |
204
|
|
|
* @param string $type |
205
|
|
|
* @param string $message |
206
|
|
|
* @param object|null $data |
207
|
|
|
* @param string|null $dynamic |
208
|
|
|
*/ |
209
|
|
|
protected function setResponse(int $code, string $type, string $message, ?object $data = null, ?string $dynamic = null): void |
210
|
|
|
{ |
211
|
|
|
$this->response = (object)[ |
212
|
|
|
"code" => $code, |
213
|
|
|
"type" => $type, |
214
|
|
|
"message" => $message, |
215
|
|
|
"data" => $data, |
216
|
|
|
"dynamic" => $dynamic |
217
|
|
|
]; |
218
|
|
|
} |
219
|
|
|
} |