@@ -79,23 +79,23 @@ discard block |
||
79 | 79 | |
80 | 80 | public function set($url , $role , $protocol = null): Router |
81 | 81 | { |
82 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
82 | + $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
83 | 83 | |
84 | - foreach($this->routers as $key => $value){ |
|
85 | - if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
84 | + foreach($this->routers as $key => $value){ |
|
85 | + if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
86 | 86 | throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured."); |
87 | 87 | } |
88 | - } |
|
88 | + } |
|
89 | 89 | |
90 | - $route = [ |
|
91 | - 'url' => $this->prefix.$url, |
|
92 | - 'role' => $role, |
|
93 | - 'protocol' => $protocol, |
|
94 | - 'filters' => null, |
|
90 | + $route = [ |
|
91 | + 'url' => $this->prefix.$url, |
|
92 | + 'role' => $role, |
|
93 | + 'protocol' => $protocol, |
|
94 | + 'filters' => null, |
|
95 | 95 | 'group' => self::getInstance()->group |
96 | - ]; |
|
96 | + ]; |
|
97 | 97 | |
98 | - $this->routers[] = $route; |
|
98 | + $this->routers[] = $route; |
|
99 | 99 | |
100 | 100 | return self::getInstance(); |
101 | 101 | } |
@@ -137,33 +137,33 @@ discard block |
||
137 | 137 | |
138 | 138 | public function dispatch(?string $route_name = null): bool |
139 | 139 | { |
140 | - $currentProtocol = $this->getProtocol(); |
|
140 | + $currentProtocol = $this->getProtocol(); |
|
141 | 141 | $context = []; |
142 | 142 | |
143 | - if(!empty($route_name)){ |
|
143 | + if(!empty($route_name)){ |
|
144 | 144 | |
145 | - if(!array_key_exists($route_name,$this->routers)){ |
|
145 | + if(!array_key_exists($route_name,$this->routers)){ |
|
146 | 146 | throw new Exception('Page not found.'.$route_name,404); |
147 | 147 | } |
148 | 148 | |
149 | - $route = $this->routers[$route_name]; |
|
149 | + $route = $this->routers[$route_name]; |
|
150 | 150 | |
151 | - if($route['protocol']!==$currentProtocol){ |
|
151 | + if($route['protocol']!==$currentProtocol){ |
|
152 | 152 | throw new Exception('Page not found.'.$route_name,404); |
153 | 153 | } |
154 | 154 | |
155 | - if(!empty($route['filters'])){ |
|
156 | - if(is_array($route['filters'])){ |
|
157 | - foreach($route['filters'] as $filter){ |
|
158 | - $this->filter->filtering($filter); |
|
159 | - } |
|
160 | - }else{ |
|
161 | - $this->filter->filtering($route['filters']); |
|
162 | - } |
|
163 | - } |
|
155 | + if(!empty($route['filters'])){ |
|
156 | + if(is_array($route['filters'])){ |
|
157 | + foreach($route['filters'] as $filter){ |
|
158 | + $this->filter->filtering($filter); |
|
159 | + } |
|
160 | + }else{ |
|
161 | + $this->filter->filtering($route['filters']); |
|
162 | + } |
|
163 | + } |
|
164 | 164 | |
165 | 165 | $this->Controller($route['role']); |
166 | - return true; |
|
166 | + return true; |
|
167 | 167 | } |
168 | 168 | |
169 | 169 | foreach(array_reverse($this->routers) as $r => $route){ |
@@ -173,13 +173,13 @@ discard block |
||
173 | 173 | continue; |
174 | 174 | } |
175 | 175 | } |
176 | - }else{ |
|
177 | - if($route['protocol'] !== $currentProtocol){ |
|
176 | + }else{ |
|
177 | + if($route['protocol'] !== $currentProtocol){ |
|
178 | 178 | continue; |
179 | 179 | } |
180 | - } |
|
180 | + } |
|
181 | 181 | |
182 | - $route_loop = explode( |
|
182 | + $route_loop = explode( |
|
183 | 183 | '/', |
184 | 184 | (substr($route['url'],strlen($route['url'])-1,1) === '/') |
185 | 185 | ? substr($route['url'], 0, -1) |
@@ -188,45 +188,45 @@ discard block |
||
188 | 188 | |
189 | 189 | /* ONLY FOR DEBUG CONDITION */ |
190 | 190 | $route_request = $route_loop; |
191 | - /*$route_request = explode( |
|
191 | + /*$route_request = explode( |
|
192 | 192 | '/', |
193 | 193 | (substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') |
194 | 194 | ? substr($_SERVER['REQUEST_URI'], 0, -1) |
195 | 195 | : $_SERVER['REQUEST_URI'] |
196 | 196 | );*/ |
197 | 197 | |
198 | - if(count($route_loop) !== count($route_request)){ |
|
198 | + if(count($route_loop) !== count($route_request)){ |
|
199 | 199 | continue; |
200 | 200 | } |
201 | 201 | |
202 | - for($rr = 0; $rr < count($route_loop); $rr++){ |
|
203 | - $param = (substr($route_loop[$rr],0,1)==='{'); |
|
202 | + for($rr = 0; $rr < count($route_loop); $rr++){ |
|
203 | + $param = (substr($route_loop[$rr],0,1)==='{'); |
|
204 | 204 | |
205 | - if($param){ |
|
205 | + if($param){ |
|
206 | 206 | $param_name = substr($route_loop[$rr],1,strlen($route_loop[$rr])-2); |
207 | - $data[$param_name] = $route_request[$rr]; |
|
208 | - } |
|
207 | + $data[$param_name] = $route_request[$rr]; |
|
208 | + } |
|
209 | 209 | |
210 | - if(!$param and $route_loop[$rr] !== $route_request[$rr]){ |
|
210 | + if(!$param and $route_loop[$rr] !== $route_request[$rr]){ |
|
211 | 211 | continue 2; |
212 | 212 | } |
213 | - } |
|
214 | - |
|
215 | - if(!empty($route['filters'])){ |
|
216 | - if(is_array($route['filters'])){ |
|
217 | - foreach($route['filters'] as $filter){ |
|
218 | - $this->filter->filtering($filter); |
|
219 | - } |
|
220 | - }else{ |
|
221 | - $this->filter->filtering($route['filters']); |
|
222 | - } |
|
223 | - } |
|
213 | + } |
|
214 | + |
|
215 | + if(!empty($route['filters'])){ |
|
216 | + if(is_array($route['filters'])){ |
|
217 | + foreach($route['filters'] as $filter){ |
|
218 | + $this->filter->filtering($filter); |
|
219 | + } |
|
220 | + }else{ |
|
221 | + $this->filter->filtering($route['filters']); |
|
222 | + } |
|
223 | + } |
|
224 | 224 | |
225 | 225 | $this->Controller($route['role']); |
226 | - return true; |
|
227 | - } |
|
226 | + return true; |
|
227 | + } |
|
228 | 228 | |
229 | - throw new Exception('Page not found.',404); |
|
229 | + throw new Exception('Page not found.',404); |
|
230 | 230 | } |
231 | 231 | |
232 | 232 | public static function filter($filters): Router |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | } |
315 | 315 | |
316 | 316 | public function ControllerForm($controller, string $method, array $values){ |
317 | - if(Validator::execute($values)){ |
|
317 | + if(Validator::execute($values)){ |
|
318 | 318 | if(!array_key_exists('role',$this->getData()['POST'])){ |
319 | 319 | throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.'); |
320 | 320 | } |