@@ -48,15 +48,15 @@ discard block |
||
| 48 | 48 | { |
| 49 | 49 | $this->lastReturn = null; |
| 50 | 50 | |
| 51 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
| 51 | + $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
| 52 | 52 | |
| 53 | - $this->checkExistence($url,$protocol); |
|
| 53 | + $this->checkExistence($url,$protocol); |
|
| 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 | 'middlewares' => 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 | } |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | use HnrAzevedo\Http\ServerRequest; |
| 6 | 6 | |
| 7 | -class Router{ |
|
| 7 | +class Router { |
|
| 8 | 8 | use Helper, DefinitionsTrait, ExtraJobsTrait; |
| 9 | 9 | |
| 10 | 10 | private static Router $instance; |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | public static function create(): Router |
| 27 | 27 | { |
| 28 | - if(!self::getInstance()->instanced){ |
|
| 28 | + if (!self::getInstance()->instanced) { |
|
| 29 | 29 | self::getInstance()->instanced = true; |
| 30 | 30 | } |
| 31 | 31 | |
@@ -44,13 +44,13 @@ discard block |
||
| 44 | 44 | return self::getInstance(); |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | - public function set(string $url ,$walking , string $protocol): Router |
|
| 47 | + public function set(string $url, $walking, string $protocol): Router |
|
| 48 | 48 | { |
| 49 | 49 | $this->lastReturn = null; |
| 50 | 50 | |
| 51 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
| 51 | + $url = (substr($url, 0, 1) !== '/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
| 52 | 52 | |
| 53 | - $this->checkExistence($url,$protocol); |
|
| 53 | + $this->checkExistence($url, $protocol); |
|
| 54 | 54 | $this->checkTypeRole($walking); |
| 55 | 55 | |
| 56 | 56 | $route = [ |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | |
| 74 | 74 | public static function group(string $prefix, callable $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; |
@@ -93,11 +93,11 @@ discard block |
||
| 93 | 93 | |
| 94 | 94 | $currentRoute = end(self::getInstance()->routers); |
| 95 | 95 | |
| 96 | - if(in_array($name,self::getInstance()->beforeExcepts)){ |
|
| 96 | + if (in_array($name, self::getInstance()->beforeExcepts)) { |
|
| 97 | 97 | $currentRoute['beforeAll'] = null; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - if(in_array($name,self::getInstance()->afterExcepts)){ |
|
| 100 | + if (in_array($name, self::getInstance()->afterExcepts)) { |
|
| 101 | 101 | $currentRoute['afterAll'] = null; |
| 102 | 102 | } |
| 103 | 103 | |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | { |
| 118 | 118 | $instance = self::create()->getInstance(); |
| 119 | 119 | |
| 120 | - if(!$instance->loaded){ |
|
| 120 | + if (!$instance->loaded) { |
|
| 121 | 121 | self::load($routeName); |
| 122 | 122 | } |
| 123 | 123 | |
@@ -126,36 +126,36 @@ discard block |
||
| 126 | 126 | |
| 127 | 127 | public function middleware($middlewares): Router |
| 128 | 128 | { |
| 129 | - if($this->lastReturn !== null){ |
|
| 129 | + if ($this->lastReturn !== null) { |
|
| 130 | 130 | $currentGroup = end($this->routers)['group']; |
| 131 | 131 | |
| 132 | 132 | foreach ($this->routers as $key => $value) { |
| 133 | - if($value['group'] === $currentGroup){ |
|
| 134 | - $this->routers[$key] = $this->addMiddleware($this->routers[$key],$middlewares); |
|
| 133 | + if ($value['group'] === $currentGroup) { |
|
| 134 | + $this->routers[$key] = $this->addMiddleware($this->routers[$key], $middlewares); |
|
| 135 | 135 | } |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | return $this; |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | - $this->routers[count($this->routers)-1] = $this->addMiddleware(end($this->routers),$middlewares); |
|
| 141 | + $this->routers[count($this->routers)-1] = $this->addMiddleware(end($this->routers), $middlewares); |
|
| 142 | 142 | return $this; |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | public static function addMiddleware(array $route, $filter): array |
| 146 | 146 | { |
| 147 | - if(is_null($route['middlewares'])){ |
|
| 147 | + if (is_null($route['middlewares'])) { |
|
| 148 | 148 | $route['middlewares'] = $filter; |
| 149 | 149 | return $route; |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | $middlewares = (is_array($filter)) ? $filter : [0 => $filter]; |
| 153 | 153 | |
| 154 | - if(is_array($route['middlewares'])){ |
|
| 154 | + if (is_array($route['middlewares'])) { |
|
| 155 | 155 | foreach ($route['middlewares'] as $key => $value) { |
| 156 | 156 | $middlewares[] = $value; |
| 157 | 157 | } |
| 158 | - }else{ |
|
| 158 | + }else { |
|
| 159 | 159 | $middlewares[] = $route['middlewares']; |
| 160 | 160 | } |
| 161 | 161 | |
@@ -155,7 +155,7 @@ |
||
| 155 | 155 | foreach ($route['middlewares'] as $key => $value) { |
| 156 | 156 | $middlewares[] = $value; |
| 157 | 157 | } |
| 158 | - }else{ |
|
| 158 | + } else{ |
|
| 159 | 159 | $middlewares[] = $route['middlewares']; |
| 160 | 160 | } |
| 161 | 161 | |
@@ -4,15 +4,15 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | use Exception; |
| 6 | 6 | |
| 7 | -trait MiddlewareTrait{ |
|
| 7 | +trait MiddlewareTrait { |
|
| 8 | 8 | protected array $middlewares = []; |
| 9 | 9 | |
| 10 | 10 | protected function checkMiddleware(array $route) |
| 11 | 11 | { |
| 12 | - $middlewares = (is_array($route['middlewares'])) ? $route['middlewares'] : [ $route['middlewares'] ]; |
|
| 12 | + $middlewares = (is_array($route['middlewares'])) ? $route['middlewares'] : [$route['middlewares']]; |
|
| 13 | 13 | |
| 14 | - foreach($middlewares as $middleware){ |
|
| 15 | - if(is_null($middleware)){ |
|
| 14 | + foreach ($middlewares as $middleware) { |
|
| 15 | + if (is_null($middleware)) { |
|
| 16 | 16 | continue; |
| 17 | 17 | } |
| 18 | 18 | |
@@ -25,17 +25,17 @@ discard block |
||
| 25 | 25 | protected function middlewareHandle(string $m) |
| 26 | 26 | { |
| 27 | 27 | $middleware = $this->middlewareExists($m); |
| 28 | - $middleware->process($this->serverRequest,$this->request); |
|
| 28 | + $middleware->process($this->serverRequest, $this->request); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | protected function middlewareExists(string $m) |
| 32 | 32 | { |
| 33 | - if(class_exists(str_replace('::class','',$m))){ |
|
| 34 | - $m = str_replace('::class','',$m); |
|
| 33 | + if (class_exists(str_replace('::class', '', $m))) { |
|
| 34 | + $m = str_replace('::class', '', $m); |
|
| 35 | 35 | return new $m(); |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - if(array_key_exists($m,$this->middlewares)){ |
|
| 38 | + if (array_key_exists($m, $this->middlewares)) { |
|
| 39 | 39 | return new $this->middlewares[$m](); |
| 40 | 40 | } |
| 41 | 41 | |
@@ -2,12 +2,12 @@ |
||
| 2 | 2 | |
| 3 | 3 | namespace HnrAzevedo\Router; |
| 4 | 4 | |
| 5 | -trait DefinitionsTrait{ |
|
| 5 | +trait DefinitionsTrait { |
|
| 6 | 6 | private static Router $instance; |
| 7 | 7 | |
| 8 | 8 | public static function match(string $protocols, string $uri, $walking): Router |
| 9 | 9 | { |
| 10 | - foreach(explode('|',$protocols) as $protocol){ |
|
| 10 | + foreach (explode('|', $protocols) as $protocol) { |
|
| 11 | 11 | self::getInstance()->add($uri, $walking, $protocol); |
| 12 | 12 | } |
| 13 | 13 | return self::$instance; |