@@ -43,20 +43,20 @@ discard block |
||
| 43 | 43 | { |
| 44 | 44 | $this->lastReturn = null; |
| 45 | 45 | |
| 46 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
| 46 | + $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
| 47 | 47 | |
| 48 | - foreach($this->routers as $key => $value){ |
|
| 49 | - if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
| 48 | + foreach($this->routers as $key => $value){ |
|
| 49 | + if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
| 50 | 50 | throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured."); |
| 51 | 51 | } |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | $this->checkTypeRole($walking); |
| 55 | 55 | |
| 56 | - $route = [ |
|
| 57 | - 'url' => $this->prefix.$url, |
|
| 58 | - 'role' => $walking, |
|
| 59 | - 'protocol' => $protocol, |
|
| 56 | + $route = [ |
|
| 57 | + 'url' => $this->prefix.$url, |
|
| 58 | + 'role' => $walking, |
|
| 59 | + 'protocol' => $protocol, |
|
| 60 | 60 | 'filters' => null, |
| 61 | 61 | 'where' => null, |
| 62 | 62 | 'before' => null, |
@@ -64,9 +64,9 @@ discard block |
||
| 64 | 64 | 'after' => null, |
| 65 | 65 | 'afterAll' => $this->afterAll, |
| 66 | 66 | 'group' => self::getInstance()->group |
| 67 | - ]; |
|
| 67 | + ]; |
|
| 68 | 68 | |
| 69 | - $this->routers[] = $route; |
|
| 69 | + $this->routers[] = $route; |
|
| 70 | 70 | |
| 71 | 71 | return self::getInstance(); |
| 72 | 72 | } |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | return $instance; |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | - $currentProtocol = $instance->getProtocol(); |
|
| 151 | + $currentProtocol = $instance->getProtocol(); |
|
| 152 | 152 | |
| 153 | 153 | foreach(array_reverse($instance->routers) as $r => $route){ |
| 154 | 154 | |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | $instance->currentRoute = null; |
| 181 | - throw new Exception('Page not found.',404); |
|
| 181 | + throw new Exception('Page not found.',404); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | public static function dispatch(?string $routeName = null): bool |
@@ -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, CheckWhere, DefinitionsTrait, ExtraJobsTrait; |
| 9 | 9 | |
| 10 | 10 | private static $instance = null; |
@@ -26,7 +26,7 @@ discard block |
||
| 26 | 26 | |
| 27 | 27 | public static function create(): Router |
| 28 | 28 | { |
| 29 | - if(!self::getInstance()->instanced){ |
|
| 29 | + if (!self::getInstance()->instanced) { |
|
| 30 | 30 | self::getInstance()->instanced = true; |
| 31 | 31 | } |
| 32 | 32 | |
@@ -39,14 +39,14 @@ discard block |
||
| 39 | 39 | return self::$instance; |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - public function set($url ,$walking , string $protocol): Router |
|
| 42 | + public function set($url, $walking, string $protocol): Router |
|
| 43 | 43 | { |
| 44 | 44 | $this->lastReturn = null; |
| 45 | 45 | |
| 46 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
| 46 | + $url = (substr($url, 0, 1) !== '/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
| 47 | 47 | |
| 48 | - foreach($this->routers as $key => $value){ |
|
| 49 | - if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
| 48 | + foreach ($this->routers as $key => $value) { |
|
| 49 | + if (md5($this->prefix.$value['url'].$value['protocol']) === md5($url.$protocol)) { |
|
| 50 | 50 | throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured."); |
| 51 | 51 | } |
| 52 | 52 | } |
@@ -71,9 +71,9 @@ discard block |
||
| 71 | 71 | return self::getInstance(); |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - public static function group(string $prefix,$callback): Router |
|
| 74 | + public static function group(string $prefix, $callback): Router |
|
| 75 | 75 | { |
| 76 | - self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix; |
|
| 76 | + self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix; |
|
| 77 | 77 | self::getInstance()->group = sha1(date('d/m/Y h:m:i')); |
| 78 | 78 | $callback(); |
| 79 | 79 | self::getInstance()->group = null; |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | |
| 85 | 85 | public static function where(): Router |
| 86 | 86 | { |
| 87 | - if(self::getInstance()->lastReturn){ |
|
| 87 | + if (self::getInstance()->lastReturn) { |
|
| 88 | 88 | throw new Exception("It is not possible to define parameter tests for groups of routes."); |
| 89 | 89 | } |
| 90 | 90 | |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | |
| 96 | 96 | public static function name(string $name): Router |
| 97 | 97 | { |
| 98 | - if(self::getInstance()->lastReturn){ |
|
| 98 | + if (self::getInstance()->lastReturn) { |
|
| 99 | 99 | throw new Exception("There is no reason to call a {$name} route group."); |
| 100 | 100 | } |
| 101 | 101 | |
@@ -103,11 +103,11 @@ discard block |
||
| 103 | 103 | |
| 104 | 104 | self::getInstance()->hasRouteName($name); |
| 105 | 105 | |
| 106 | - if(in_array($name,self::getInstance()->beforeExcepts)){ |
|
| 106 | + if (in_array($name, self::getInstance()->beforeExcepts)) { |
|
| 107 | 107 | $currentRoute['beforeAll'] = null; |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - if(in_array($name,self::getInstance()->afterExcepts)){ |
|
| 110 | + if (in_array($name, self::getInstance()->afterExcepts)) { |
|
| 111 | 111 | $currentRoute['afterAll'] = null; |
| 112 | 112 | } |
| 113 | 113 | |
@@ -120,15 +120,15 @@ discard block |
||
| 120 | 120 | |
| 121 | 121 | private function byName(?string $routName): bool |
| 122 | 122 | { |
| 123 | - if(!is_null($routName)){ |
|
| 123 | + if (!is_null($routName)) { |
|
| 124 | 124 | $currentProtocol = $this->getProtocol(); |
| 125 | 125 | |
| 126 | 126 | $this->checkName($routName); |
| 127 | 127 | |
| 128 | 128 | $route = $this->routers[$routName]; |
| 129 | 129 | |
| 130 | - if(!$this->checkProtocol($route['protocol'], $currentProtocol)){ |
|
| 131 | - throw new Exception('Page not found.',404); |
|
| 130 | + if (!$this->checkProtocol($route['protocol'], $currentProtocol)) { |
|
| 131 | + throw new Exception('Page not found.', 404); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | $this->currentRoute = $route; |
@@ -144,18 +144,18 @@ discard block |
||
| 144 | 144 | { |
| 145 | 145 | $instance = self::create()->getInstance(); |
| 146 | 146 | |
| 147 | - if($instance->byName($routeName)){ |
|
| 147 | + if ($instance->byName($routeName)) { |
|
| 148 | 148 | return $instance; |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | $currentProtocol = $instance->getProtocol(); |
| 152 | 152 | |
| 153 | - foreach(array_reverse($instance->routers) as $r => $route){ |
|
| 153 | + foreach (array_reverse($instance->routers) as $r => $route) { |
|
| 154 | 154 | |
| 155 | 155 | $instance->currentRoute = $route; |
| 156 | 156 | $instance->currentRoute['name'] = $r; |
| 157 | 157 | |
| 158 | - if(!$instance->checkProtocol($route['protocol'], $currentProtocol)){ |
|
| 158 | + if (!$instance->checkProtocol($route['protocol'], $currentProtocol)) { |
|
| 159 | 159 | continue; |
| 160 | 160 | } |
| 161 | 161 | |
@@ -165,11 +165,11 @@ discard block |
||
| 165 | 165 | |
| 166 | 166 | |
| 167 | 167 | $routs = $instance->explodeRoutes( |
| 168 | - (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url'], |
|
| 169 | - (substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI'] |
|
| 168 | + (substr($route['url'], strlen($route['url'])-1, 1) === '/'), $route['url'], |
|
| 169 | + (substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/'), $_SERVER['REQUEST_URI'] |
|
| 170 | 170 | ); |
| 171 | 171 | |
| 172 | - if(!$instance->checkToHiking($route, $routs['routeRequest'], $routs['routeLoop'])){ |
|
| 172 | + if (!$instance->checkToHiking($route, $routs['routeRequest'], $routs['routeLoop'])) { |
|
| 173 | 173 | continue; |
| 174 | 174 | } |
| 175 | 175 | |
@@ -178,14 +178,14 @@ discard block |
||
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | $instance->currentRoute = null; |
| 181 | - throw new Exception('Page not found.',404); |
|
| 181 | + throw new Exception('Page not found.', 404); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | public static function dispatch(?string $routeName = null): bool |
| 185 | 185 | { |
| 186 | 186 | $instance = self::create()->getInstance(); |
| 187 | 187 | |
| 188 | - if(!$instance->loaded){ |
|
| 188 | + if (!$instance->loaded) { |
|
| 189 | 189 | self::load($routeName); |
| 190 | 190 | } |
| 191 | 191 | |
@@ -197,13 +197,13 @@ discard block |
||
| 197 | 197 | |
| 198 | 198 | public function filter($filters): Router |
| 199 | 199 | { |
| 200 | - if($this->lastReturn !== null){ |
|
| 200 | + if ($this->lastReturn !== null) { |
|
| 201 | 201 | $currentGroup = end($this->routers)['group']; |
| 202 | 202 | |
| 203 | 203 | foreach ($this->routers as $key => $value) { |
| 204 | 204 | |
| 205 | - if($value['group'] === $currentGroup){ |
|
| 206 | - $currentRoute = $this->addFilter($this->routers[$key],$filters); |
|
| 205 | + if ($value['group'] === $currentGroup) { |
|
| 206 | + $currentRoute = $this->addFilter($this->routers[$key], $filters); |
|
| 207 | 207 | $this->routers[$key] = $currentRoute; |
| 208 | 208 | } |
| 209 | 209 | |
@@ -212,24 +212,24 @@ discard block |
||
| 212 | 212 | return $this; |
| 213 | 213 | } |
| 214 | 214 | |
| 215 | - $this->routers[count($this->routers)-1] = $this->addFilter(end($this->routers),$filters); |
|
| 215 | + $this->routers[count($this->routers)-1] = $this->addFilter(end($this->routers), $filters); |
|
| 216 | 216 | return $this; |
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | public static function addFilter(array $route, $filter): array |
| 220 | 220 | { |
| 221 | - if(is_null($route['filters'])){ |
|
| 221 | + if (is_null($route['filters'])) { |
|
| 222 | 222 | $route['filters'] = $filter; |
| 223 | 223 | return $route; |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | $filters = (is_array($filter)) ? $filter : [0 => $filter]; |
| 227 | 227 | |
| 228 | - if(is_array($route['filters'])){ |
|
| 228 | + if (is_array($route['filters'])) { |
|
| 229 | 229 | foreach ($route['filters'] as $key => $value) { |
| 230 | 230 | $filters[] = $value; |
| 231 | 231 | } |
| 232 | - }else{ |
|
| 232 | + }else { |
|
| 233 | 233 | $filters[] = $route['filters']; |
| 234 | 234 | } |
| 235 | 235 | |