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