@@ -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, $walking, $protocol); |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - public function set($url ,$walking , string $protocol): Router |
|
| 62 | + public function set($url, $walking, string $protocol): 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 | } |
@@ -84,9 +84,9 @@ discard block |
||
| 84 | 84 | return self::getInstance(); |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - public static function group(string $prefix,$callback): Router |
|
| 87 | + public static function group(string $prefix, $callback): Router |
|
| 88 | 88 | { |
| 89 | - self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix; |
|
| 89 | + self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix; |
|
| 90 | 90 | self::getInstance()->group = sha1(date('d/m/Y h:m:i')); |
| 91 | 91 | $callback(); |
| 92 | 92 | self::getInstance()->group = null; |
@@ -97,14 +97,14 @@ discard block |
||
| 97 | 97 | |
| 98 | 98 | public static function name(string $name): Router |
| 99 | 99 | { |
| 100 | - if(self::getInstance()->lastReturn){ |
|
| 100 | + if (self::getInstance()->lastReturn) { |
|
| 101 | 101 | throw new Exception("There is no reason to call a {$name} route group."); |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | $currentRoute = end(self::getInstance()->routers); |
| 105 | 105 | |
| 106 | - foreach(self::getInstance()->routers as $key => $value){ |
|
| 107 | - if(array_key_exists($name, self::getInstance()->routers)){ |
|
| 106 | + foreach (self::getInstance()->routers as $key => $value) { |
|
| 107 | + if (array_key_exists($name, self::getInstance()->routers)) { |
|
| 108 | 108 | throw new Exception("There is already a route with the name {$name} configured."); |
| 109 | 109 | } |
| 110 | 110 | } |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | |
| 121 | 121 | private function byName(?string $route_name) |
| 122 | 122 | { |
| 123 | - if(!is_null($route_name)){ |
|
| 123 | + if (!is_null($route_name)) { |
|
| 124 | 124 | $currentProtocol = $this->getProtocol(); |
| 125 | 125 | |
| 126 | 126 | $this->check_name($route_name); |
@@ -142,19 +142,19 @@ 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 | - if(!$this->hasProtocol($route, $currentProtocol)){ |
|
| 147 | + if (!$this->hasProtocol($route, $currentProtocol)) { |
|
| 148 | 148 | continue; |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | - $route_loop = $this->explodeRoute( (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url']); |
|
| 151 | + $route_loop = $this->explodeRoute((substr($route['url'], strlen($route['url'])-1, 1) === '/'), $route['url']); |
|
| 152 | 152 | |
| 153 | 153 | $_SERVER['REQUEST_URI'] = (array_key_exists('REQUEST_URI', $_SERVER)) ? $_SERVER['REQUEST_URI'] : ''; |
| 154 | 154 | |
| 155 | - $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI']); |
|
| 155 | + $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/'), $_SERVER['REQUEST_URI']); |
|
| 156 | 156 | |
| 157 | - if($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)){ |
|
| 157 | + if ($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)) { |
|
| 158 | 158 | continue; |
| 159 | 159 | } |
| 160 | 160 | |
@@ -164,18 +164,18 @@ discard block |
||
| 164 | 164 | return true; |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - throw new Exception('Page not found.',404); |
|
| 167 | + throw new Exception('Page not found.', 404); |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | public static function filter($filters): Router |
| 171 | 171 | { |
| 172 | - if(self::getInstance()->lastReturn !== null){ |
|
| 172 | + if (self::getInstance()->lastReturn !== null) { |
|
| 173 | 173 | $currentGroup = end(self::getInstance()->routers)['group']; |
| 174 | 174 | |
| 175 | 175 | foreach (self::getInstance()->routers as $key => $value) { |
| 176 | 176 | |
| 177 | - if($value['group'] === $currentGroup){ |
|
| 178 | - $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters); |
|
| 177 | + if ($value['group'] === $currentGroup) { |
|
| 178 | + $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters); |
|
| 179 | 179 | self::getInstance()->routers[$key] = $currentRoute; |
| 180 | 180 | } |
| 181 | 181 | |
@@ -184,24 +184,24 @@ discard block |
||
| 184 | 184 | return self::getInstance(); |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | - self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters); |
|
| 187 | + self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters); |
|
| 188 | 188 | return self::getInstance(); |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | public static function addFilter(array $route, $filter): array |
| 192 | 192 | { |
| 193 | - if(is_null($route['filters'])){ |
|
| 193 | + if (is_null($route['filters'])) { |
|
| 194 | 194 | $route['filters'] = $filter; |
| 195 | 195 | return $route; |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | $filters = (is_array($filter)) ? $filter : [0 => $filter]; |
| 199 | 199 | |
| 200 | - if(is_array($route['filters'])){ |
|
| 200 | + if (is_array($route['filters'])) { |
|
| 201 | 201 | foreach ($route['filters'] as $key => $value) { |
| 202 | 202 | $filters[] = $value; |
| 203 | 203 | } |
| 204 | - }else{ |
|
| 204 | + }else { |
|
| 205 | 205 | $filters[] = $route['filters']; |
| 206 | 206 | } |
| 207 | 207 | |