@@ -61,23 +61,23 @@ discard block |
||
| 61 | 61 | |
| 62 | 62 | public function set($url , $role , $protocol = null): Router |
| 63 | 63 | { |
| 64 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
| 64 | + $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
| 65 | 65 | |
| 66 | - foreach($this->routers as $key => $value){ |
|
| 67 | - if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
| 66 | + foreach($this->routers as $key => $value){ |
|
| 67 | + if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
| 68 | 68 | throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured."); |
| 69 | 69 | } |
| 70 | - } |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - $route = [ |
|
| 73 | - 'url' => $this->prefix.$url, |
|
| 74 | - 'role' => $role, |
|
| 75 | - 'protocol' => $protocol, |
|
| 76 | - 'filters' => null, |
|
| 72 | + $route = [ |
|
| 73 | + 'url' => $this->prefix.$url, |
|
| 74 | + 'role' => $role, |
|
| 75 | + 'protocol' => $protocol, |
|
| 76 | + 'filters' => null, |
|
| 77 | 77 | 'group' => self::getInstance()->group |
| 78 | - ]; |
|
| 78 | + ]; |
|
| 79 | 79 | |
| 80 | - $this->routers[] = $route; |
|
| 80 | + $this->routers[] = $route; |
|
| 81 | 81 | |
| 82 | 82 | return self::getInstance(); |
| 83 | 83 | } |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | { |
| 141 | 141 | $this->byName($route_name); |
| 142 | 142 | |
| 143 | - $currentProtocol = $this->getProtocol(); |
|
| 143 | + $currentProtocol = $this->getProtocol(); |
|
| 144 | 144 | |
| 145 | 145 | foreach(array_reverse($this->routers) as $r => $route){ |
| 146 | 146 | |
@@ -150,17 +150,17 @@ discard block |
||
| 150 | 150 | |
| 151 | 151 | $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI']); |
| 152 | 152 | |
| 153 | - if($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)){ |
|
| 153 | + if($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)){ |
|
| 154 | 154 | continue; |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | $this->check_filtering($route); |
| 158 | 158 | |
| 159 | 159 | $this->Controller($route['role']); |
| 160 | - return true; |
|
| 161 | - } |
|
| 160 | + return true; |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | - throw new Exception('Page not found.',404); |
|
| 163 | + throw new Exception('Page not found.',404); |
|
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | public static function filter($filters): Router |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | 6 | |
| 7 | -class Router{ |
|
| 7 | +class Router { |
|
| 8 | 8 | use Helper, CheckTrait; |
| 9 | 9 | |
| 10 | 10 | private static $instance = null; |
@@ -59,12 +59,12 @@ discard block |
||
| 59 | 59 | return self::getInstance()->set($uri, $controll, $protocol); |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - public function set($url , $role , $protocol = null): Router |
|
| 62 | + public function set($url, $role, $protocol = null): Router |
|
| 63 | 63 | { |
| 64 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
| 64 | + $url = (substr($url, 0, 1) !== '/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
| 65 | 65 | |
| 66 | - foreach($this->routers as $key => $value){ |
|
| 67 | - if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
| 66 | + foreach ($this->routers as $key => $value) { |
|
| 67 | + if (md5($this->prefix.$value['url'].$value['protocol']) === md5($url.$protocol)) { |
|
| 68 | 68 | throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured."); |
| 69 | 69 | } |
| 70 | 70 | } |
@@ -82,9 +82,9 @@ discard block |
||
| 82 | 82 | return self::getInstance(); |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - public static function group(string $prefix,$callback): Router |
|
| 85 | + public static function group(string $prefix, $callback): Router |
|
| 86 | 86 | { |
| 87 | - self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix; |
|
| 87 | + self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix; |
|
| 88 | 88 | self::getInstance()->group = sha1(date('d/m/Y h:m:i')); |
| 89 | 89 | $callback(); |
| 90 | 90 | self::getInstance()->group = null; |
@@ -96,14 +96,14 @@ discard block |
||
| 96 | 96 | public static function name(string $name): Router |
| 97 | 97 | { |
| 98 | 98 | |
| 99 | - if(self::getInstance()->lastReturn){ |
|
| 99 | + if (self::getInstance()->lastReturn) { |
|
| 100 | 100 | throw new Exception("There is no reason to call a {$name} route group."); |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | $currentRoute = end(self::getInstance()->routers); |
| 104 | 104 | |
| 105 | - foreach(self::getInstance()->routers as $key => $value){ |
|
| 106 | - if(array_key_exists($name, self::getInstance()->routers)){ |
|
| 105 | + foreach (self::getInstance()->routers as $key => $value) { |
|
| 106 | + if (array_key_exists($name, self::getInstance()->routers)) { |
|
| 107 | 107 | throw new Exception("There is already a route with the name {$name} configured."); |
| 108 | 108 | } |
| 109 | 109 | } |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | |
| 120 | 120 | private function byName(?string $route_name) |
| 121 | 121 | { |
| 122 | - if(!is_null($route_name)){ |
|
| 122 | + if (!is_null($route_name)) { |
|
| 123 | 123 | $currentProtocol = $this->getProtocol(); |
| 124 | 124 | |
| 125 | 125 | $this->check_name($route_name); |
@@ -142,15 +142,15 @@ discard block |
||
| 142 | 142 | |
| 143 | 143 | $currentProtocol = $this->getProtocol(); |
| 144 | 144 | |
| 145 | - foreach(array_reverse($this->routers) as $r => $route){ |
|
| 145 | + foreach (array_reverse($this->routers) as $r => $route) { |
|
| 146 | 146 | |
| 147 | 147 | $this->hasProtocol($route, $currentProtocol); |
| 148 | 148 | |
| 149 | - $route_loop = $this->explodeRoute( (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url']); |
|
| 149 | + $route_loop = $this->explodeRoute((substr($route['url'], strlen($route['url'])-1, 1) === '/'), $route['url']); |
|
| 150 | 150 | |
| 151 | - $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI']); |
|
| 151 | + $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/'), $_SERVER['REQUEST_URI']); |
|
| 152 | 152 | |
| 153 | - if($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)){ |
|
| 153 | + if ($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)) { |
|
| 154 | 154 | continue; |
| 155 | 155 | } |
| 156 | 156 | |
@@ -160,18 +160,18 @@ discard block |
||
| 160 | 160 | return true; |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | - throw new Exception('Page not found.',404); |
|
| 163 | + throw new Exception('Page not found.', 404); |
|
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | public static function filter($filters): Router |
| 167 | 167 | { |
| 168 | - if(self::getInstance()->lastReturn !== null){ |
|
| 168 | + if (self::getInstance()->lastReturn !== null) { |
|
| 169 | 169 | $currentGroup = end(self::getInstance()->routers)['group']; |
| 170 | 170 | |
| 171 | 171 | foreach (self::getInstance()->routers as $key => $value) { |
| 172 | 172 | |
| 173 | - if($value['group'] === $currentGroup){ |
|
| 174 | - $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters); |
|
| 173 | + if ($value['group'] === $currentGroup) { |
|
| 174 | + $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters); |
|
| 175 | 175 | self::getInstance()->routers[$key] = $currentRoute; |
| 176 | 176 | } |
| 177 | 177 | |
@@ -180,24 +180,24 @@ discard block |
||
| 180 | 180 | return self::getInstance(); |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | - self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters); |
|
| 183 | + self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters); |
|
| 184 | 184 | return self::getInstance(); |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | public static function addFilter(array $route, $filter): array |
| 188 | 188 | { |
| 189 | - if(is_null($route['filters'])){ |
|
| 189 | + if (is_null($route['filters'])) { |
|
| 190 | 190 | $route['filters'] = $filter; |
| 191 | 191 | return $route; |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | $filters = (is_array($filter)) ? $filter : [0 => $filter]; |
| 195 | 195 | |
| 196 | - if(is_array($route['filters'])){ |
|
| 196 | + if (is_array($route['filters'])) { |
|
| 197 | 197 | foreach ($route['filters'] as $key => $value) { |
| 198 | 198 | $filters[] = $value; |
| 199 | 199 | } |
| 200 | - }else{ |
|
| 200 | + }else { |
|
| 201 | 201 | $filters[] = $route['filters']; |
| 202 | 202 | } |
| 203 | 203 | |
@@ -2,6 +2,6 @@ |
||
| 2 | 2 | |
| 3 | 3 | use HnrAzevedo\Router\Router; |
| 4 | 4 | |
| 5 | -Router::get('/','Application:index'); |
|
| 5 | +Router::get('/', 'Application:index'); |
|
| 6 | 6 | |
| 7 | -Router::get('/{controller}/{method}','{controller}:{method}'); |
|
| 8 | 7 | \ No newline at end of file |
| 8 | +Router::get('/{controller}/{method}', '{controller}:{method}'); |
|
| 9 | 9 | \ No newline at end of file |
@@ -4,27 +4,27 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | 6 | |
| 7 | -trait CheckTrait{ |
|
| 7 | +trait CheckTrait { |
|
| 8 | 8 | |
| 9 | 9 | protected function check_protocol(string $expected, string $current) |
| 10 | 10 | { |
| 11 | - if($expected !== $current){ |
|
| 12 | - throw new Exception('Page not found.',404); |
|
| 11 | + if ($expected !== $current) { |
|
| 12 | + throw new Exception('Page not found.', 404); |
|
| 13 | 13 | } |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | - protected function check_name(string $route_name){ |
|
| 17 | - if(!array_key_exists($route_name,$this->routers)){ |
|
| 18 | - throw new Exception('Page not found.',404); |
|
| 16 | + protected function check_name(string $route_name) { |
|
| 17 | + if (!array_key_exists($route_name, $this->routers)) { |
|
| 18 | + throw new Exception('Page not found.', 404); |
|
| 19 | 19 | } |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | protected function check_filtering(array $route) |
| 23 | 23 | { |
| 24 | - $filters = (is_array($route['filters'])) ? $route['filters'] : [ $route['filters'] ]; |
|
| 24 | + $filters = (is_array($route['filters'])) ? $route['filters'] : [$route['filters']]; |
|
| 25 | 25 | |
| 26 | - foreach($filters as $filter){ |
|
| 27 | - if(is_null($filter)){ |
|
| 26 | + foreach ($filters as $filter) { |
|
| 27 | + if (is_null($filter)) { |
|
| 28 | 28 | continue; |
| 29 | 29 | } |
| 30 | 30 | $this->filter->filtering($filter); |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | |
| 34 | 34 | protected function check_config() |
| 35 | 35 | { |
| 36 | - if(!defined('ROUTER_CONFIG')){ |
|
| 36 | + if (!defined('ROUTER_CONFIG')) { |
|
| 37 | 37 | throw new Exception("Information for loading routes has not been defined."); |
| 38 | 38 | } |
| 39 | 39 | } |
@@ -45,12 +45,12 @@ discard block |
||
| 45 | 45 | |
| 46 | 46 | protected function check_parameters(array $route_loop, array $route_request): bool |
| 47 | 47 | { |
| 48 | - foreach($route_loop as $rr => $param){ |
|
| 49 | - if( (substr($param,0,1) === '{') ){ |
|
| 50 | - $_GET[ substr($param,1,strlen($param)-2) ] = $route_request[$rr]; |
|
| 48 | + foreach ($route_loop as $rr => $param) { |
|
| 49 | + if ((substr($param, 0, 1) === '{')) { |
|
| 50 | + $_GET[substr($param, 1, strlen($param)-2)] = $route_request[$rr]; |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - if($this->check_parameter($param, $route_request[$rr])){ |
|
| 53 | + if ($this->check_parameter($param, $route_request[$rr])) { |
|
| 54 | 54 | return false; |
| 55 | 55 | } |
| 56 | 56 | } |
@@ -59,22 +59,22 @@ discard block |
||
| 59 | 59 | |
| 60 | 60 | protected function check_parameter(string $route_loop, string $route_request) |
| 61 | 61 | { |
| 62 | - return !( substr($route_loop,0,1) === '{' ) and $route_loop !== $route_request; |
|
| 62 | + return !(substr($route_loop, 0, 1) === '{') and $route_loop !== $route_request; |
|
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | protected function check_role() |
| 66 | 66 | { |
| 67 | - if(!array_key_exists('role', $this->getData()['POST'])){ |
|
| 67 | + if (!array_key_exists('role', $this->getData()['POST'])) { |
|
| 68 | 68 | throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.'); |
| 69 | 69 | } |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | protected function hasProtocol(array $route, string $currentProtocol) |
| 73 | 73 | { |
| 74 | - $protocols = ( is_array($route['protocol']) ) ? $route['protocol'] : [ $route['protocol'] ]; |
|
| 74 | + $protocols = (is_array($route['protocol'])) ? $route['protocol'] : [$route['protocol']]; |
|
| 75 | 75 | |
| 76 | - foreach($protocols as $protocol){ |
|
| 77 | - if($protocol !== $currentProtocol){ |
|
| 76 | + foreach ($protocols as $protocol) { |
|
| 77 | + if ($protocol !== $currentProtocol) { |
|
| 78 | 78 | continue; |
| 79 | 79 | } |
| 80 | 80 | } |