@@ -19,8 +19,8 @@ discard block |
||
| 19 | 19 | |
| 20 | 20 | public static function globalMiddlewares(array $middlewares): RouterInterface |
| 21 | 21 | { |
| 22 | - foreach($middlewares as $middleware){ |
|
| 23 | - if(!class_exists($middleware)){ |
|
| 22 | + foreach ($middlewares as $middleware) { |
|
| 23 | + if (!class_exists($middleware)) { |
|
| 24 | 24 | throw new \RuntimeException("Middleware class {$middleware} not exists"); |
| 25 | 25 | } |
| 26 | 26 | } |
@@ -30,16 +30,16 @@ discard block |
||
| 30 | 30 | |
| 31 | 31 | public static function middleware($middlewares): RouterInterface |
| 32 | 32 | { |
| 33 | - $middlewares = (is_array($middlewares)) ? $middlewares : [ $middlewares ]; |
|
| 33 | + $middlewares = (is_array($middlewares)) ? $middlewares : [$middlewares]; |
|
| 34 | 34 | $route = self::getInstance()->inSave(); |
| 35 | - $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'],$middlewares) : $middlewares; |
|
| 36 | - self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->getRoutes())); |
|
| 35 | + $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'], $middlewares) : $middlewares; |
|
| 36 | + self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->getRoutes())); |
|
| 37 | 37 | return self::getInstance(); |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | private static function existMiddleware(string $name): void |
| 41 | 41 | { |
| 42 | - if(!class_exists($name) && !array_key_exists($name,self::$globalMiddlewares)){ |
|
| 42 | + if (!class_exists($name) && !array_key_exists($name, self::$globalMiddlewares)) { |
|
| 43 | 43 | throw new \RuntimeException("Middleware {$name} does not exist"); |
| 44 | 44 | } |
| 45 | 45 | } |
@@ -45,7 +45,7 @@ |
||
| 45 | 45 | |
| 46 | 46 | protected function setRoutes(array $routes): void |
| 47 | 47 | { |
| 48 | - $this->routes = $routes; |
|
| 48 | + $this->routes = $routes; |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | protected function getGroup(): ?string |
@@ -10,28 +10,28 @@ discard block |
||
| 10 | 10 | |
| 11 | 11 | protected function hasRouteName(string $name): void |
| 12 | 12 | { |
| 13 | - if(!isset($this->routesName[$name])){ |
|
| 13 | + if (!isset($this->routesName[$name])) { |
|
| 14 | 14 | throw new \RuntimeException("There is no route named with {$name}"); |
| 15 | 15 | } |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | 18 | protected function isInNameGroup(): void |
| 19 | 19 | { |
| 20 | - if(!is_null($this->getGroup())){ |
|
| 20 | + if (!is_null($this->getGroup())) { |
|
| 21 | 21 | throw new \RuntimeException("It is not allowed to assign names to groups"); |
| 22 | 22 | } |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | protected function isInPseudGroup(): void |
| 26 | 26 | { |
| 27 | - if(!is_null($this->getGroup())){ |
|
| 27 | + if (!is_null($this->getGroup())) { |
|
| 28 | 28 | throw new \RuntimeException("To assign actions before or after the execution of the route, use beforeGroup or afterGroup"); |
| 29 | 29 | } |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | protected function existRouteName(string $name): void |
| 33 | 33 | { |
| 34 | - if(isset($this->routesName[$name])){ |
|
| 34 | + if (isset($this->routesName[$name])) { |
|
| 35 | 35 | throw new \RuntimeException("There is already a route named with {$name}"); |
| 36 | 36 | } |
| 37 | 37 | } |
@@ -39,12 +39,12 @@ discard block |
||
| 39 | 39 | protected function checkMethod(array $route, $method): void |
| 40 | 40 | { |
| 41 | 41 | $hasMethod = false; |
| 42 | - foreach(explode('|',$route['method']) as $routeMethod){ |
|
| 43 | - if(@preg_match("/{$routeMethod}/",$method) !== 0 || $method === '*'){ |
|
| 42 | + foreach (explode('|', $route['method']) as $routeMethod) { |
|
| 43 | + if (@preg_match("/{$routeMethod}/", $method) !== 0 || $method === '*') { |
|
| 44 | 44 | $hasMethod = true; |
| 45 | 45 | } |
| 46 | 46 | } |
| 47 | - if(!$hasMethod){ |
|
| 47 | + if (!$hasMethod) { |
|
| 48 | 48 | throw new \Exception('This route is not released for the accessed method'); |
| 49 | 49 | } |
| 50 | 50 | |
@@ -2,7 +2,7 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace HnrAzevedo\Router; |
| 4 | 4 | |
| 5 | -trait CurrentTrait{ |
|
| 5 | +trait CurrentTrait { |
|
| 6 | 6 | use Helper; |
| 7 | 7 | |
| 8 | 8 | public static function current(): array |
@@ -11,8 +11,8 @@ discard block |
||
| 11 | 11 | public static function where(array $wheres): Router |
| 12 | 12 | { |
| 13 | 13 | $route = self::getInstance()->inSave(); |
| 14 | - $route['where'] = (is_array($route['where'])) ? array_merge($route['where'],$wheres) : $wheres; |
|
| 15 | - self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->getRoutes())); |
|
| 14 | + $route['where'] = (is_array($route['where'])) ? array_merge($route['where'], $wheres) : $wheres; |
|
| 15 | + self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->getRoutes())); |
|
| 16 | 16 | return self::getInstance(); |
| 17 | 17 | } |
| 18 | 18 | |
@@ -22,38 +22,38 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | $this->parameters = []; |
| 24 | 24 | |
| 25 | - $uriPath .= (substr($uriPath,strlen($uriPath)-1) !== '/') ? '/' : ''; |
|
| 25 | + $uriPath .= (substr($uriPath, strlen($uriPath)-1) !== '/') ? '/' : ''; |
|
| 26 | 26 | |
| 27 | - $routePath = explode('/',urldecode($route['uri']->getPath())); |
|
| 27 | + $routePath = explode('/', urldecode($route['uri']->getPath())); |
|
| 28 | 28 | unset($routePath[0]); |
| 29 | - $uriPath = explode('/',urldecode($uriPath)); |
|
| 29 | + $uriPath = explode('/', urldecode($uriPath)); |
|
| 30 | 30 | unset($uriPath[0]); |
| 31 | 31 | |
| 32 | 32 | $corretRoute = true; |
| 33 | - foreach($routePath as $r => $routeFrag){ |
|
| 33 | + foreach ($routePath as $r => $routeFrag) { |
|
| 34 | 34 | $where = is_array($route['where']) ? $route['where'] : []; |
| 35 | 35 | $routeFrag = $this->replaceParam($where, $routeFrag, $uriPath[$r]); |
| 36 | 36 | |
| 37 | - if($routeFrag !== $uriPath[$r]){ |
|
| 37 | + if ($routeFrag !== $uriPath[$r]) { |
|
| 38 | 38 | $corretRoute = false; |
| 39 | 39 | } |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - if(!$corretRoute){ |
|
| 42 | + if (!$corretRoute) { |
|
| 43 | 43 | throw new \Exception('continue'); |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | - $_REQUEST = array_merge($_REQUEST,$this->parameters); |
|
| 46 | + $_REQUEST = array_merge($_REQUEST, $this->parameters); |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | private function replaceParam(array $where, string $ref, string $value): string |
| 50 | 50 | { |
| 51 | - if(((substr($ref,0,1) === '{') && (substr($ref,strlen($ref)-1) === '}'))) { |
|
| 52 | - $this->parameters[str_replace(['{:','{','}'],'',$ref)] = $value; |
|
| 51 | + if (((substr($ref, 0, 1) === '{') && (substr($ref, strlen($ref)-1) === '}'))) { |
|
| 52 | + $this->parameters[str_replace(['{:', '{', '}'], '', $ref)] = $value; |
|
| 53 | 53 | |
| 54 | - $this->checkValueRequire($ref,$value); |
|
| 54 | + $this->checkValueRequire($ref, $value); |
|
| 55 | 55 | |
| 56 | - if(array_key_exists(str_replace(['{:','{','}'],'',$ref),$where)){ |
|
| 56 | + if (array_key_exists(str_replace(['{:', '{', '}'], '', $ref), $where)) { |
|
| 57 | 57 | $this->matchParam($where, $ref, $value); |
| 58 | 58 | } |
| 59 | 59 | |
@@ -64,25 +64,25 @@ discard block |
||
| 64 | 64 | |
| 65 | 65 | private function checkValueRequire(string $ref, string $value): void |
| 66 | 66 | { |
| 67 | - if(substr($ref,0,2) !== '{:' && strlen($value) === 0){ |
|
| 67 | + if (substr($ref, 0, 2) !== '{:' && strlen($value) === 0) { |
|
| 68 | 68 | throw new \Exception('continue'); |
| 69 | 69 | } |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | private function checkCount(string $routePath, string $uriPath): void |
| 73 | 73 | { |
| 74 | - $countRequest = substr_count($uriPath,'/') - substr_count($routePath,'{:'); |
|
| 75 | - $countRoute = substr_count($routePath,'/') - substr_count($routePath,'{:'); |
|
| 74 | + $countRequest = substr_count($uriPath, '/')-substr_count($routePath, '{:'); |
|
| 75 | + $countRoute = substr_count($routePath, '/')-substr_count($routePath, '{:'); |
|
| 76 | 76 | |
| 77 | - if($countRequest !== $countRoute){ |
|
| 77 | + if ($countRequest !== $countRoute) { |
|
| 78 | 78 | throw new \Exception('continue'); |
| 79 | 79 | } |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | private function matchParam(array $where, string $ref, string $value): void |
| 83 | 83 | { |
| 84 | - if(substr($ref,0,2) === '{' || $value !== ''){ |
|
| 85 | - if(!preg_match("/^{$where[str_replace(['{:','{','}'],'',$ref)]}$/",$value)){ |
|
| 84 | + if (substr($ref, 0, 2) === '{' || $value !== '') { |
|
| 85 | + if (!preg_match("/^{$where[str_replace(['{:', '{', '}'], '', $ref)]}$/", $value)) { |
|
| 86 | 86 | echo 2; |
| 87 | 87 | throw new \Exception('continue'); |
| 88 | 88 | } |
@@ -15,19 +15,19 @@ discard block |
||
| 15 | 15 | |
| 16 | 16 | protected function getState(string $state, bool $except = false) |
| 17 | 17 | { |
| 18 | - if($state === 'before'){ |
|
| 18 | + if ($state === 'before') { |
|
| 19 | 19 | return ($except) ? $this->beforeExcepts : $this->beforeAll; |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | - if($state === 'after'){ |
|
| 22 | + if ($state === 'after') { |
|
| 23 | 23 | return ($except) ? $this->afterExcepts : $this->afterAll; |
| 24 | 24 | } |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | protected function setState(string $state, $settable, bool $except = false): bool |
| 28 | 28 | { |
| 29 | - if($state === 'before'){ |
|
| 30 | - if($except){ |
|
| 29 | + if ($state === 'before') { |
|
| 30 | + if ($except) { |
|
| 31 | 31 | $this->beforeExcepts = $settable; |
| 32 | 32 | return true; |
| 33 | 33 | } |
@@ -36,8 +36,8 @@ discard block |
||
| 36 | 36 | return true; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - if($state === 'after'){ |
|
| 40 | - if($except){ |
|
| 39 | + if ($state === 'after') { |
|
| 40 | + if ($except) { |
|
| 41 | 41 | $this->afterExcepts = $settable; |
| 42 | 42 | return true; |
| 43 | 43 | } |
@@ -49,24 +49,24 @@ discard block |
||
| 49 | 49 | |
| 50 | 50 | public static function before($closure): RouterInterface |
| 51 | 51 | { |
| 52 | - return self::addInRoute('before',$closure); |
|
| 52 | + return self::addInRoute('before', $closure); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | public static function after($closure): RouterInterface |
| 56 | 56 | { |
| 57 | - return self::addInRoute('after',$closure); |
|
| 57 | + return self::addInRoute('after', $closure); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | public static function beforeAll($closure, $excepts): RouterInterface |
| 61 | 61 | { |
| 62 | - self::getInstance()->setState('before', (is_array($excepts)) ? $excepts : [ $excepts ] ,true); |
|
| 62 | + self::getInstance()->setState('before', (is_array($excepts)) ? $excepts : [$excepts], true); |
|
| 63 | 63 | self::getInstance()->setState('before', $closure, false); |
| 64 | 64 | return self::getInstance(); |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | public static function afterAll($closure, $excepts): RouterInterface |
| 68 | 68 | { |
| 69 | - self::getInstance()->setState('after', (is_array($excepts)) ? $excepts : [ $excepts ] ,true); |
|
| 69 | + self::getInstance()->setState('after', (is_array($excepts)) ? $excepts : [$excepts], true); |
|
| 70 | 70 | self::getInstance()->setState('after', $closure, false); |
| 71 | 71 | return self::getInstance(); |
| 72 | 72 | } |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | |
| 84 | 84 | protected function executeRouteAction(): bool |
| 85 | 85 | { |
| 86 | - if(is_callable($this->current()['action'])){ |
|
| 86 | + if (is_callable($this->current()['action'])) { |
|
| 87 | 87 | call_user_func_array($this->current()['action'], $_REQUEST); |
| 88 | 88 | return true; |
| 89 | 89 | } |
@@ -95,12 +95,12 @@ discard block |
||
| 95 | 95 | private static function addInRoutes(string $state, $closure, $excepts): RouterInterface |
| 96 | 96 | { |
| 97 | 97 | self::getInstance()->isInPseudGroup(); |
| 98 | - $excepts = (is_array($excepts)) ? $excepts : [ $excepts ]; |
|
| 98 | + $excepts = (is_array($excepts)) ? $excepts : [$excepts]; |
|
| 99 | 99 | $group = self::getInstance()->inSave()['group']; |
| 100 | 100 | |
| 101 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
| 102 | - if($route['group'] === $group && !in_array($r,$excepts)){ |
|
| 103 | - self::getInstance()->getRoutes()[$r][$state] = (is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
| 101 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
| 102 | + if ($route['group'] === $group && !in_array($r, $excepts)) { |
|
| 103 | + self::getInstance()->getRoutes()[$r][$state] = (is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
| 104 | 104 | } |
| 105 | 105 | } |
| 106 | 106 | |
@@ -110,14 +110,14 @@ discard block |
||
| 110 | 110 | private static function addInRoute(string $state, $closure): RouterInterface |
| 111 | 111 | { |
| 112 | 112 | $route = self::getInstance()->inSave(); |
| 113 | - $route[$state] = (!is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
| 114 | - self::updateRoute($route,array_key_last(self::getInstance()->getRoutes())); |
|
| 113 | + $route[$state] = (!is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
| 114 | + self::updateRoute($route, array_key_last(self::getInstance()->getRoutes())); |
|
| 115 | 115 | return self::getInstance(); |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | protected function executeBefore(): void |
| 119 | 119 | { |
| 120 | - if(!in_array($this->currentName(),$this->getState('before', true))){ |
|
| 120 | + if (!in_array($this->currentName(), $this->getState('before', true))) { |
|
| 121 | 121 | ($this->getState('before', false))(); |
| 122 | 122 | } |
| 123 | 123 | |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | |
| 127 | 127 | protected function executeAfter(): void |
| 128 | 128 | { |
| 129 | - if(!in_array($this->currentName(),$this->getState('after', true))){ |
|
| 129 | + if (!in_array($this->currentName(), $this->getState('after', true))) { |
|
| 130 | 130 | ($this->getState('after', false))(); |
| 131 | 131 | } |
| 132 | 132 | |
@@ -135,8 +135,8 @@ discard block |
||
| 135 | 135 | |
| 136 | 136 | private function executeState(string $stage): void |
| 137 | 137 | { |
| 138 | - foreach($this->current()[$stage] as $state){ |
|
| 139 | - if(is_callable($state)){ |
|
| 138 | + foreach ($this->current()[$stage] as $state) { |
|
| 139 | + if (is_callable($state)) { |
|
| 140 | 140 | $state(); |
| 141 | 141 | continue; |
| 142 | 142 | } |
@@ -147,31 +147,31 @@ discard block |
||
| 147 | 147 | |
| 148 | 148 | private function executeController(string $controllerMeth): void |
| 149 | 149 | { |
| 150 | - if(count(explode('@',$controllerMeth)) !== 2){ |
|
| 150 | + if (count(explode('@', $controllerMeth)) !== 2) { |
|
| 151 | 151 | $path = urldecode($this->current()['uri']->getPath()); |
| 152 | 152 | throw new \RuntimeException("Misconfigured route action ({$path})"); |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | - $controller = (string) explode('@',$controllerMeth)[0]; |
|
| 156 | - $method = (string) explode('@',$controllerMeth)[1]; |
|
| 155 | + $controller = (string) explode('@', $controllerMeth)[0]; |
|
| 156 | + $method = (string) explode('@', $controllerMeth)[1]; |
|
| 157 | 157 | |
| 158 | 158 | $this->checkControllerMeth($controllerMeth); |
| 159 | 159 | |
| 160 | - call_user_func_array([(new $controller()),$method], $_REQUEST); |
|
| 160 | + call_user_func_array([(new $controller()), $method], $_REQUEST); |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | private function checkControllerMeth(string $controllerMeth): void |
| 164 | 164 | { |
| 165 | - $routeURI = str_replace(['{:','{','}'],'',urldecode($this->current()['uri']->getPath())); |
|
| 165 | + $routeURI = str_replace(['{:', '{', '}'], '', urldecode($this->current()['uri']->getPath())); |
|
| 166 | 166 | |
| 167 | - $controller = (string) explode('@',$controllerMeth)[0]; |
|
| 168 | - $method = (string) explode('@',$controllerMeth)[1]; |
|
| 167 | + $controller = (string) explode('@', $controllerMeth)[0]; |
|
| 168 | + $method = (string) explode('@', $controllerMeth)[1]; |
|
| 169 | 169 | |
| 170 | - if(!class_exists($controller)){ |
|
| 170 | + if (!class_exists($controller)) { |
|
| 171 | 171 | throw new \RuntimeException("Controller not found in route URI {$routeURI}"); |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - if(!method_exists($controller, $method)){ |
|
| 174 | + if (!method_exists($controller, $method)) { |
|
| 175 | 175 | throw new \RuntimeException("Method {$method} not found in controller {$controller} in route URI {$routeURI}"); |
| 176 | 176 | } |
| 177 | 177 | |