|
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
|
|
|
foreach ($patterns as $key => $pattern) { |
|
48
|
|
|
if ($this->getMethod() === $this->verb[$key]) { |
|
49
|
|
|
if (preg_match('#' . $pattern . '#', $this->getRequest(), $router)) { |
|
50
|
|
|
throw new RuntimeException('Error '.$pattern.' ' . $this->getRequest()); |
|
51
|
|
|
} |
|
52
|
|
|
if (isset($router[0])) { |
|
53
|
|
|
if ($router[0] === $this->getRequest()) { |
|
54
|
|
|
$this->setClass($key, $router); |
|
55
|
|
|
} |
|
56
|
|
|
} else if ($this->getRequest() === "") { |
|
57
|
|
|
$this->setClass($key, $router); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
return true; |
|
63
|
|
|
} |
|
64
|
|
|
/** |
|
65
|
|
|
* @param $key |
|
66
|
|
|
* @param $router |
|
67
|
|
|
*/ |
|
68
|
|
|
private function setClass($key, $router): void |
|
69
|
|
|
{ |
|
70
|
|
|
$classMethod = explode("@", $this->controller[$key]); |
|
71
|
|
|
[$class, $method] = $classMethod; |
|
72
|
|
|
array_shift($router); |
|
73
|
|
|
if (preg_match_all('~{([^}]*)}~', $this->route[$key], $keys) === false) { |
|
74
|
|
|
throw new RuntimeException('The route ' . $this->route[$key] . ' not exist.'); |
|
75
|
|
|
} |
|
76
|
|
|
$data = array_combine($keys[1], $router); |
|
77
|
|
|
$this->classMethod($class, $method, $data, $key); |
|
78
|
|
|
} |
|
79
|
|
|
/** |
|
80
|
|
|
* @param $class |
|
81
|
|
|
* @param $method |
|
82
|
|
|
* @param $data |
|
83
|
|
|
* @param $key |
|
84
|
|
|
* @return bool|void |
|
85
|
|
|
*/ |
|
86
|
|
|
private function classMethod($class, $method, $data, $key) |
|
87
|
|
|
{ |
|
88
|
|
|
$argument = $data; |
|
89
|
|
|
unset($data); |
|
90
|
|
|
$data['argument'] = $argument; |
|
91
|
|
|
$class = $this->namespaceArray[$key] . "\\" . $class; |
|
92
|
|
|
if (class_exists($class)) { |
|
93
|
|
|
$Class = new $class; |
|
94
|
|
|
if (method_exists($Class, $method)) { |
|
95
|
|
|
$this->setNotFound(false); |
|
|
|
|
|
|
96
|
|
|
$data['query'] = $this->getQuery(); |
|
97
|
|
|
if ($this->middleware[$key]) { |
|
98
|
|
|
$Middleware = new Middleware(); |
|
99
|
|
|
if (!$Middleware->validate()) { |
|
100
|
|
|
$this->setResponse( |
|
101
|
|
|
401, |
|
102
|
|
|
"error", |
|
103
|
|
|
"this access is mandatory to inform the correct Baren Token" |
|
104
|
|
|
); |
|
105
|
|
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
$Class->$method($data, $this->type[$key]); |
|
109
|
|
|
return true; |
|
110
|
|
|
} |
|
111
|
|
|
$this->setResponse( |
|
112
|
|
|
405, |
|
113
|
|
|
"error", |
|
114
|
|
|
"the {$this->controller[$key]} method does not exist", |
|
115
|
|
|
dynamic: $this->controller[$key] |
|
116
|
|
|
); |
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
/** |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function controller(): bool |
|
124
|
|
|
{ |
|
125
|
|
|
$this->duplicates($this->getPatterns()); |
|
126
|
|
|
$this->patterns(); |
|
127
|
|
|
if (empty($this->getPatterns())) { |
|
128
|
|
|
return false; |
|
129
|
|
|
} |
|
130
|
|
|
return true; |
|
131
|
|
|
} |
|
132
|
|
|
/** |
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function getPatterns(): array |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->patterns; |
|
138
|
|
|
} |
|
139
|
|
|
/** |
|
140
|
|
|
* @return object |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function getResponse(): object |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->response; |
|
145
|
|
|
} |
|
146
|
|
|
/** |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function getMethod(): string |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->method; |
|
152
|
|
|
} |
|
153
|
|
|
/** |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function getRequest(): string |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->request; |
|
159
|
|
|
} |
|
160
|
|
|
/** |
|
161
|
|
|
* @return array|null |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function getQuery(): ?array |
|
164
|
|
|
{ |
|
165
|
|
|
return $this->query; |
|
166
|
|
|
} |
|
167
|
|
|
/** |
|
168
|
|
|
* @return string |
|
169
|
|
|
*/ |
|
170
|
|
|
protected function getNamespace(): string |
|
171
|
|
|
{ |
|
172
|
|
|
return $this->namespace; |
|
173
|
|
|
} |
|
174
|
|
|
/** |
|
175
|
|
|
* @return array |
|
176
|
|
|
*/ |
|
177
|
|
|
protected function getRoute(): array |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->route; |
|
180
|
|
|
} |
|
181
|
|
|
/** |
|
182
|
|
|
* @return bool |
|
183
|
|
|
*/ |
|
184
|
|
|
protected function isNotFound(): bool |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->notFound; |
|
187
|
|
|
} |
|
188
|
|
|
/** |
|
189
|
|
|
* @param int $code |
|
190
|
|
|
* @param string $type |
|
191
|
|
|
* @param string $message |
|
192
|
|
|
* @param object|null $data |
|
193
|
|
|
* @param string|null $dynamic |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function setResponse(int $code, string $type, string $message, ?object $data = null, ?string $dynamic = null): void |
|
196
|
|
|
{ |
|
197
|
|
|
$this->response = (object)[ |
|
198
|
|
|
"code" => $code, |
|
199
|
|
|
"type" => $type, |
|
200
|
|
|
"message" => $message, |
|
201
|
|
|
"data" => $data, |
|
202
|
|
|
"dynamic" => $dynamic |
|
203
|
|
|
]; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
} |