@@ -14,13 +14,13 @@ discard block |
||
| 14 | 14 | use HnrAzevedo\Router\Router; |
| 15 | 15 | use Psr\Http\Server\MiddlewareInterface; |
| 16 | 16 | |
| 17 | -try{ |
|
| 17 | +try { |
|
| 18 | 18 | $serverRequest = (new Factory())->createServerRequest($_SERVER['REQUEST_METHOD'], new Uri($_SERVER['REQUEST_URI'])); |
| 19 | 19 | |
| 20 | - class App implements MiddlewareInterface{ |
|
| 20 | + class App implements MiddlewareInterface { |
|
| 21 | 21 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 22 | 22 | { |
| 23 | - if(empty($request->getAttribute('route'))) |
|
| 23 | + if (empty($request->getAttribute('route'))) |
|
| 24 | 24 | { |
| 25 | 25 | throw new Exception('Page not found', 404); |
| 26 | 26 | } |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | } |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - define('GLOBAL_MIDDLEWARES',[ |
|
| 34 | + define('GLOBAL_MIDDLEWARES', [ |
|
| 35 | 35 | Router::class, |
| 36 | 36 | App::class |
| 37 | 37 | ]); |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | |
| 68 | 68 | function runMiddlewares($serverRequest) |
| 69 | 69 | { |
| 70 | - nextExample(new class implements RequestHandlerInterface{ |
|
| 70 | + nextExample(new class implements RequestHandlerInterface { |
|
| 71 | 71 | public function handle(ServerRequestInterface $request): ResponseInterface |
| 72 | 72 | { |
| 73 | 73 | return (new Factory())->createResponse(200); |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | |
| 78 | 78 | runMiddlewares($serverRequest); |
| 79 | 79 | |
| 80 | -}catch(Exception $er){ |
|
| 80 | +}catch (Exception $er) { |
|
| 81 | 81 | |
| 82 | 82 | die("Code Error: {$er->getCode()}<br>Line: {$er->getLine()}<br>File: {$er->getFile()}<br>Message: {$er->getMessage()}."); |
| 83 | 83 | |
@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | |
| 8 | 8 | use HnrAzevedo\Router\Router; |
| 9 | 9 | |
| 10 | -try{ |
|
| 10 | +try { |
|
| 11 | 11 | |
| 12 | 12 | Router::defineHost('https://localhost'); |
| 13 | 13 | |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | */ |
| 29 | 29 | $action = Router::currentAction(); |
| 30 | 30 | |
| 31 | -}catch(Exception $er){ |
|
| 31 | +}catch (Exception $er) { |
|
| 32 | 32 | |
| 33 | 33 | die("Code Error: {$er->getCode()}<br>Line: {$er->getLine()}<br>File: {$er->getFile()}<br>Message: {$er->getMessage()}."); |
| 34 | 34 | |
@@ -7,26 +7,26 @@ discard block |
||
| 7 | 7 | /** |
| 8 | 8 | * Defines a route for the GET method |
| 9 | 9 | */ |
| 10 | -Router::get('/foo','\HnrAzevedo\Router\Examples\Controller@method'); |
|
| 10 | +Router::get('/foo', '\HnrAzevedo\Router\Examples\Controller@method'); |
|
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * Defines a route to the GET method with an anonymous function |
| 14 | 14 | */ |
| 15 | -Router::get('/bar', function(){ |
|
| 15 | +Router::get('/bar', function() { |
|
| 16 | 16 | // |
| 17 | 17 | }); |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | 20 | * Defines a route to the POST method with an anonymous function |
| 21 | 21 | */ |
| 22 | -Router::post('/bar', function(){ |
|
| 22 | +Router::post('/bar', function() { |
|
| 23 | 23 | // |
| 24 | 24 | }); |
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * Defining route for any method |
| 28 | 28 | */ |
| 29 | -Router::any('/any', function(){ |
|
| 29 | +Router::any('/any', function() { |
|
| 30 | 30 | // |
| 31 | 31 | }); |
| 32 | 32 | |
@@ -36,14 +36,14 @@ discard block |
||
| 36 | 36 | * @param string $route |
| 37 | 37 | * @param \Closure|string $action |
| 38 | 38 | */ |
| 39 | -Router::match('GET|post', '/get_post', function(){ |
|
| 39 | +Router::match('GET|post', '/get_post', function() { |
|
| 40 | 40 | // |
| 41 | 41 | }); |
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | 44 | * Defines the route name |
| 45 | 45 | */ |
| 46 | -Router::get('/named', function(){ |
|
| 46 | +Router::get('/named', function() { |
|
| 47 | 47 | // |
| 48 | 48 | })->name('baz'); |
| 49 | 49 | |
@@ -55,32 +55,32 @@ discard block |
||
| 55 | 55 | /** |
| 56 | 56 | * Run before all requests, regardless of error occurrences |
| 57 | 57 | */ |
| 58 | -Router::beforeAll(function(){ |
|
| 58 | +Router::beforeAll(function() { |
|
| 59 | 59 | // |
| 60 | 60 | }, null); |
| 61 | 61 | |
| 62 | 62 | /** |
| 63 | 63 | * Run after all requests, regardless of error occurrences |
| 64 | 64 | */ |
| 65 | -Router::afterAll(function(){ |
|
| 65 | +Router::afterAll(function() { |
|
| 66 | 66 | // |
| 67 | 67 | }, null); |
| 68 | 68 | |
| 69 | 69 | /** |
| 70 | 70 | * Executes after executing the triggered route action |
| 71 | 71 | */ |
| 72 | -Router::get('/after', function(){ |
|
| 72 | +Router::get('/after', function() { |
|
| 73 | 73 | // |
| 74 | -})->after(function(){ |
|
| 74 | +})->after(function() { |
|
| 75 | 75 | // |
| 76 | 76 | }); |
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | 79 | * Executes before executing the triggered route action |
| 80 | 80 | */ |
| 81 | -Router::get('/before', function(){ |
|
| 81 | +Router::get('/before', function() { |
|
| 82 | 82 | // |
| 83 | -})->before(function(){ |
|
| 83 | +})->before(function() { |
|
| 84 | 84 | // |
| 85 | 85 | }); |
| 86 | 86 | /** |
@@ -101,21 +101,21 @@ discard block |
||
| 101 | 101 | /** |
| 102 | 102 | * Example of route definition with parameters |
| 103 | 103 | */ |
| 104 | -Router::get('/passingParameters/{param}', function($param){ |
|
| 104 | +Router::get('/passingParameters/{param}', function($param) { |
|
| 105 | 105 | echo $param; |
| 106 | 106 | }); |
| 107 | 107 | |
| 108 | 108 | /** |
| 109 | 109 | * Example of setting an optional parameter |
| 110 | 110 | */ |
| 111 | -Router::get('/passingParameters/{:optionalParam}', function($optionalParam){ |
|
| 111 | +Router::get('/passingParameters/{:optionalParam}', function($optionalParam) { |
|
| 112 | 112 | echo $optionalParam; |
| 113 | 113 | }); |
| 114 | 114 | |
| 115 | 115 | /** |
| 116 | 116 | * Example of regular expression test for parameters |
| 117 | 117 | */ |
| 118 | -Router::get('/testingParameters/{param}', function($param){ |
|
| 118 | +Router::get('/testingParameters/{param}', function($param) { |
|
| 119 | 119 | echo $param; |
| 120 | 120 | })->where([ |
| 121 | 121 | 'param'=>'[0-9]{1,11}' |
@@ -131,8 +131,8 @@ discard block |
||
| 131 | 131 | * @param string $prefix |
| 132 | 132 | * @param \Closure $routeDefinitions |
| 133 | 133 | */ |
| 134 | -Router::group('/admin', function(){ |
|
| 135 | - Router::get('/users/{teste}', function($teste){ |
|
| 134 | +Router::group('/admin', function() { |
|
| 135 | + Router::get('/users/{teste}', function($teste) { |
|
| 136 | 136 | echo $teste; |
| 137 | 137 | })->where([ |
| 138 | 138 | 'teste'=>'[a-zA-Z]*' |
@@ -140,13 +140,13 @@ discard block |
||
| 140 | 140 | }); |
| 141 | 141 | |
| 142 | 142 | |
| 143 | -Router::group('/admin2', function(){ |
|
| 144 | - Router::get('/users/{teste}', function($teste){ |
|
| 143 | +Router::group('/admin2', function() { |
|
| 144 | + Router::get('/users/{teste}', function($teste) { |
|
| 145 | 145 | echo $teste; |
| 146 | 146 | })->name('teste'); |
| 147 | 147 | })->groupWhere([ |
| 148 | 148 | 'teste'=>'[a-zA-Z]*' |
| 149 | -],[]); |
|
| 149 | +], []); |
|
| 150 | 150 | |
| 151 | 151 | |
| 152 | 152 | |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | /** |
| 164 | 164 | * Defining route middleware - implements Psr\Http\Server\MiddlewareInterface |
| 165 | 165 | */ |
| 166 | -Router::get('/passingParameters/{:optionalParam}', function($optionalParam = null){ |
|
| 166 | +Router::get('/passingParameters/{:optionalParam}', function($optionalParam = null) { |
|
| 167 | 167 | echo $optionalParam; |
| 168 | 168 | })->middleware([ |
| 169 | 169 | HnrAzevedo\Router\Example\Middleware\Auth::class |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | /** |
| 173 | 173 | * Defining middleware by nickname |
| 174 | 174 | */ |
| 175 | -Router::get('/lasted', function(){ |
|
| 175 | +Router::get('/lasted', function() { |
|
| 176 | 176 | // |
| 177 | 177 | })->middleware([ |
| 178 | 178 | 'Lasted' |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | * Defining multiple middlewares |
| 183 | 183 | * NOTE: Importantly, the execution follows the same order of definition |
| 184 | 184 | */ |
| 185 | -Router::get('/middlewares', function(){ |
|
| 185 | +Router::get('/middlewares', function() { |
|
| 186 | 186 | // |
| 187 | 187 | })->middleware([ |
| 188 | 188 | HnrAzevedo\Router\Example\Middleware\Auth::class, |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace HnrAzevedo\Router; |
| 6 | 6 | |
@@ -12,28 +12,28 @@ discard block |
||
| 12 | 12 | |
| 13 | 13 | protected function hasRouteName(string $name): void |
| 14 | 14 | { |
| 15 | - if(!isset($this->routesName[$name])){ |
|
| 15 | + if (!isset($this->routesName[$name])) { |
|
| 16 | 16 | throw new \RuntimeException("There is no route named with {$name}"); |
| 17 | 17 | } |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | 20 | protected function isInNameGroup(): void |
| 21 | 21 | { |
| 22 | - if(!is_null($this->getGroup())){ |
|
| 22 | + if (!is_null($this->getGroup())) { |
|
| 23 | 23 | throw new \RuntimeException("It is not allowed to assign names to groups"); |
| 24 | 24 | } |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | protected function isInPseudGroup(): void |
| 28 | 28 | { |
| 29 | - if(!is_null($this->getGroup())){ |
|
| 29 | + if (!is_null($this->getGroup())) { |
|
| 30 | 30 | throw new \RuntimeException("To assign actions before or after the execution of the route, use beforeGroup or afterGroup"); |
| 31 | 31 | } |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | protected function existRouteName(string $name): void |
| 35 | 35 | { |
| 36 | - if(isset($this->routesName[$name])){ |
|
| 36 | + if (isset($this->routesName[$name])) { |
|
| 37 | 37 | throw new \RuntimeException("There is already a route named with {$name}"); |
| 38 | 38 | } |
| 39 | 39 | } |
@@ -41,12 +41,12 @@ discard block |
||
| 41 | 41 | protected function checkMethod(array $route, $method): void |
| 42 | 42 | { |
| 43 | 43 | $hasMethod = false; |
| 44 | - foreach(explode('|',$route['method']) as $routeMethod){ |
|
| 45 | - if(@preg_match("/{$routeMethod}/",$method) !== 0 || $method === '*'){ |
|
| 44 | + foreach (explode('|', $route['method']) as $routeMethod) { |
|
| 45 | + if (@preg_match("/{$routeMethod}/", $method) !== 0 || $method === '*') { |
|
| 46 | 46 | $hasMethod = true; |
| 47 | 47 | } |
| 48 | 48 | } |
| 49 | - if(!$hasMethod){ |
|
| 49 | + if (!$hasMethod) { |
|
| 50 | 50 | throw new \Exception('This route is not released for the accessed method'); |
| 51 | 51 | } |
| 52 | 52 | |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace HnrAzevedo\Router; |
| 6 | 6 | |
@@ -13,27 +13,27 @@ discard block |
||
| 13 | 13 | $staticRoutes = []; |
| 14 | 14 | $paramRoutes = []; |
| 15 | 15 | |
| 16 | - foreach($this->getRoutes() as $r => $route){ |
|
| 16 | + foreach ($this->getRoutes() as $r => $route) { |
|
| 17 | 17 | |
| 18 | 18 | $path = urldecode($route['uri']->getPath()); |
| 19 | 19 | |
| 20 | - if(strstr($path,'{')){ |
|
| 21 | - $paramRoutes[$this->getKeyArray(substr_count($path,'/') + substr_count($path,'{'),$paramRoutes)] = $route; |
|
| 20 | + if (strstr($path, '{')) { |
|
| 21 | + $paramRoutes[$this->getKeyArray(substr_count($path, '/')+substr_count($path, '{'), $paramRoutes)] = $route; |
|
| 22 | 22 | continue; |
| 23 | 23 | } |
| 24 | 24 | |
| 25 | - $staticRoutes[$this->getKeyArray(substr_count($path,'/'),$staticRoutes)] = $route; |
|
| 25 | + $staticRoutes[$this->getKeyArray(substr_count($path, '/'), $staticRoutes)] = $route; |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | rsort($paramRoutes); |
| 29 | 29 | rsort($staticRoutes); |
| 30 | 30 | |
| 31 | - $this->orderRoutes(array_merge($staticRoutes,$paramRoutes)); |
|
| 31 | + $this->orderRoutes(array_merge($staticRoutes, $paramRoutes)); |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | private function getKeyArray(int $index, array $array): int |
| 35 | 35 | { |
| 36 | - while(array_key_exists($index,$array)){ |
|
| 36 | + while (array_key_exists($index, $array)) { |
|
| 37 | 37 | $index++; |
| 38 | 38 | } |
| 39 | 39 | return $index; |
@@ -42,8 +42,8 @@ discard block |
||
| 42 | 42 | private function orderRoutes(array $routes):void |
| 43 | 43 | { |
| 44 | 44 | $kRoutes = $routes; |
| 45 | - foreach($routes as $r => $route){ |
|
| 46 | - if(array_key_exists('name',$route)){ |
|
| 45 | + foreach ($routes as $r => $route) { |
|
| 46 | + if (array_key_exists('name', $route)) { |
|
| 47 | 47 | unset($kRoutes[$r]); |
| 48 | 48 | $kRoutes["'{$route['name']}'"] = $route; |
| 49 | 49 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace HnrAzevedo\Router; |
| 6 | 6 | |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | |
| 55 | 55 | protected function setRoutes(array $routes): void |
| 56 | 56 | { |
| 57 | - $this->routes = $routes; |
|
| 57 | + $this->routes = $routes; |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | protected function getGroup(): ?string |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace HnrAzevedo\Router; |
| 6 | 6 | |
@@ -21,8 +21,8 @@ discard block |
||
| 21 | 21 | public static function groupWhere(array $wheres, array $excepts): Router |
| 22 | 22 | { |
| 23 | 23 | $group = self::getInstance()->inSave()['group']; |
| 24 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
| 25 | - if($route['group'] !== $group || in_array($route['name'], $excepts)){ |
|
| 24 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
| 25 | + if ($route['group'] !== $group || in_array($route['name'], $excepts)) { |
|
| 26 | 26 | continue; |
| 27 | 27 | } |
| 28 | 28 | |
@@ -47,30 +47,30 @@ discard block |
||
| 47 | 47 | unset($uriPath[0]); |
| 48 | 48 | |
| 49 | 49 | $corretRoute = true; |
| 50 | - foreach ($routePath as $r => $routeFrag){ |
|
| 50 | + foreach ($routePath as $r => $routeFrag) { |
|
| 51 | 51 | $where = is_array($route['where']) ? $route['where'] : []; |
| 52 | 52 | $routeFrag = $this->replaceParam($where, $routeFrag, $uriPath[$r]); |
| 53 | 53 | |
| 54 | - if($routeFrag !== $uriPath[$r]){ |
|
| 54 | + if ($routeFrag !== $uriPath[$r]) { |
|
| 55 | 55 | $corretRoute = false; |
| 56 | 56 | } |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | - if(!$corretRoute){ |
|
| 59 | + if (!$corretRoute) { |
|
| 60 | 60 | throw new \Exception('continue'); |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - $_REQUEST = array_merge($_REQUEST,$this->parameters); |
|
| 63 | + $_REQUEST = array_merge($_REQUEST, $this->parameters); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | private function replaceParam(array $where, string $ref, string $value): string |
| 67 | 67 | { |
| 68 | - if(((substr($ref,0,1) === '{') && (substr($ref,strlen($ref)-1) === '}'))) { |
|
| 69 | - $this->parameters[str_replace(['{:','{','}'],'',$ref)] = $value; |
|
| 68 | + if (((substr($ref, 0, 1) === '{') && (substr($ref, strlen($ref)-1) === '}'))) { |
|
| 69 | + $this->parameters[str_replace(['{:', '{', '}'], '', $ref)] = $value; |
|
| 70 | 70 | |
| 71 | - $this->checkValueRequire($ref,$value); |
|
| 71 | + $this->checkValueRequire($ref, $value); |
|
| 72 | 72 | |
| 73 | - if(array_key_exists(str_replace(['{:','{','}'],'',$ref),$where)){ |
|
| 73 | + if (array_key_exists(str_replace(['{:', '{', '}'], '', $ref), $where)) { |
|
| 74 | 74 | $this->matchParam($where, $ref, $value); |
| 75 | 75 | } |
| 76 | 76 | |
@@ -81,25 +81,25 @@ discard block |
||
| 81 | 81 | |
| 82 | 82 | private function checkValueRequire(string $ref, string $value): void |
| 83 | 83 | { |
| 84 | - if(substr($ref,0,2) !== '{:' && strlen($value) === 0){ |
|
| 84 | + if (substr($ref, 0, 2) !== '{:' && strlen($value) === 0) { |
|
| 85 | 85 | throw new \Exception('continue'); |
| 86 | 86 | } |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | private function checkCount(string $routePath, string $uriPath): void |
| 90 | 90 | { |
| 91 | - $countRequest = substr_count($uriPath,'/') - substr_count($routePath,'{:'); |
|
| 92 | - $countRoute = substr_count($routePath,'/') - substr_count($routePath,'{:'); |
|
| 91 | + $countRequest = substr_count($uriPath, '/')-substr_count($routePath, '{:'); |
|
| 92 | + $countRoute = substr_count($routePath, '/')-substr_count($routePath, '{:'); |
|
| 93 | 93 | |
| 94 | - if($countRequest !== $countRoute){ |
|
| 94 | + if ($countRequest !== $countRoute) { |
|
| 95 | 95 | throw new \Exception('continue'); |
| 96 | 96 | } |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | private function matchParam(array $where, string $ref, string $value): void |
| 100 | 100 | { |
| 101 | - if(substr($ref,0,2) === '{' || $value !== ''){ |
|
| 102 | - if(!preg_match("/^{$where[str_replace(['{:','{','}'],'',$ref)]}$/",$value)){ |
|
| 101 | + if (substr($ref, 0, 2) === '{' || $value !== '') { |
|
| 102 | + if (!preg_match("/^{$where[str_replace(['{:', '{', '}'], '', $ref)]}$/", $value)) { |
|
| 103 | 103 | throw new \Exception('continue'); |
| 104 | 104 | } |
| 105 | 105 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace HnrAzevedo\Router; |
| 6 | 6 | |
@@ -17,7 +17,7 @@ discard block |
||
| 17 | 17 | |
| 18 | 18 | protected function getState(string $state, bool $except = false) |
| 19 | 19 | { |
| 20 | - if($state === 'before'){ |
|
| 20 | + if ($state === 'before') { |
|
| 21 | 21 | return ($except) ? $this->beforeExcepts : $this->beforeAll; |
| 22 | 22 | } |
| 23 | 23 | |
@@ -26,8 +26,8 @@ discard block |
||
| 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,7 +36,7 @@ discard block |
||
| 36 | 36 | return true; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - if($except){ |
|
| 39 | + if ($except) { |
|
| 40 | 40 | $this->afterExcepts = $settable; |
| 41 | 41 | return true; |
| 42 | 42 | } |
@@ -47,24 +47,24 @@ discard block |
||
| 47 | 47 | |
| 48 | 48 | public static function before($closure): RouterInterface |
| 49 | 49 | { |
| 50 | - return self::addInRoute('before',$closure); |
|
| 50 | + return self::addInRoute('before', $closure); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | public static function after($closure): RouterInterface |
| 54 | 54 | { |
| 55 | - return self::addInRoute('after',$closure); |
|
| 55 | + return self::addInRoute('after', $closure); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | public static function beforeAll($closure, $excepts): RouterInterface |
| 59 | 59 | { |
| 60 | - self::getInstance()->setState('before', (is_array($excepts)) ? $excepts : [ $excepts ] ,true); |
|
| 60 | + self::getInstance()->setState('before', (is_array($excepts)) ? $excepts : [$excepts], true); |
|
| 61 | 61 | self::getInstance()->setState('before', $closure, false); |
| 62 | 62 | return self::getInstance(); |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | public static function afterAll($closure, $excepts): RouterInterface |
| 66 | 66 | { |
| 67 | - self::getInstance()->setState('after', (is_array($excepts)) ? $excepts : [ $excepts ] ,true); |
|
| 67 | + self::getInstance()->setState('after', (is_array($excepts)) ? $excepts : [$excepts], true); |
|
| 68 | 68 | self::getInstance()->setState('after', $closure, false); |
| 69 | 69 | return self::getInstance(); |
| 70 | 70 | } |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | |
| 82 | 82 | protected function executeRouteAction($action): bool |
| 83 | 83 | { |
| 84 | - if(is_callable($action)){ |
|
| 84 | + if (is_callable($action)) { |
|
| 85 | 85 | call_user_func_array($action, $_REQUEST); |
| 86 | 86 | return true; |
| 87 | 87 | } |
@@ -93,12 +93,12 @@ discard block |
||
| 93 | 93 | private static function addInRoutes(string $state, $closure, $excepts): RouterInterface |
| 94 | 94 | { |
| 95 | 95 | self::getInstance()->isInPseudGroup(); |
| 96 | - $excepts = (is_array($excepts)) ? $excepts : [ $excepts ]; |
|
| 96 | + $excepts = (is_array($excepts)) ? $excepts : [$excepts]; |
|
| 97 | 97 | $group = self::getInstance()->inSave()['group']; |
| 98 | 98 | |
| 99 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
| 100 | - if($route['group'] === $group && !in_array($r,$excepts)){ |
|
| 101 | - self::getInstance()->getRoutes()[$r][$state] = (is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
| 99 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
| 100 | + if ($route['group'] === $group && !in_array($r, $excepts)) { |
|
| 101 | + self::getInstance()->getRoutes()[$r][$state] = (is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
| 102 | 102 | } |
| 103 | 103 | } |
| 104 | 104 | |
@@ -108,14 +108,14 @@ discard block |
||
| 108 | 108 | private static function addInRoute(string $state, $closure): RouterInterface |
| 109 | 109 | { |
| 110 | 110 | $route = self::getInstance()->inSave(); |
| 111 | - $route[$state] = (!is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
| 112 | - self::updateRoute($route,array_key_last(self::getInstance()->getRoutes())); |
|
| 111 | + $route[$state] = (!is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
| 112 | + self::updateRoute($route, array_key_last(self::getInstance()->getRoutes())); |
|
| 113 | 113 | return self::getInstance(); |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | protected function executeBefore(): void |
| 117 | 117 | { |
| 118 | - if(!in_array($this->currentName(), (array) $this->getState('before', true))){ |
|
| 118 | + if (!in_array($this->currentName(), (array) $this->getState('before', true))) { |
|
| 119 | 119 | ($this->getState('before', false))(); |
| 120 | 120 | } |
| 121 | 121 | |
@@ -126,15 +126,15 @@ discard block |
||
| 126 | 126 | { |
| 127 | 127 | $this->executeState('after'); |
| 128 | 128 | |
| 129 | - if(!in_array($this->currentName(), (array) $this->getState('after', true))){ |
|
| 129 | + if (!in_array($this->currentName(), (array) $this->getState('after', true))) { |
|
| 130 | 130 | ($this->getState('after', false))(); |
| 131 | 131 | } |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | private function executeState(string $stage): void |
| 135 | 135 | { |
| 136 | - foreach($this->current()[$stage] as $state){ |
|
| 137 | - if(is_callable($state)){ |
|
| 136 | + foreach ($this->current()[$stage] as $state) { |
|
| 137 | + if (is_callable($state)) { |
|
| 138 | 138 | $state(); |
| 139 | 139 | continue; |
| 140 | 140 | } |
@@ -145,31 +145,31 @@ discard block |
||
| 145 | 145 | |
| 146 | 146 | private function executeController(string $controllerMeth): void |
| 147 | 147 | { |
| 148 | - if(count(explode('@',$controllerMeth)) !== 2){ |
|
| 148 | + if (count(explode('@', $controllerMeth)) !== 2) { |
|
| 149 | 149 | $path = urldecode($this->current()['uri']->getPath()); |
| 150 | 150 | throw new \RuntimeException("Misconfigured route action ({$path})"); |
| 151 | 151 | } |
| 152 | 152 | |
| 153 | - $controller = (string) explode('@',$controllerMeth)[0]; |
|
| 154 | - $method = (string) explode('@',$controllerMeth)[1]; |
|
| 153 | + $controller = (string) explode('@', $controllerMeth)[0]; |
|
| 154 | + $method = (string) explode('@', $controllerMeth)[1]; |
|
| 155 | 155 | |
| 156 | 156 | $this->checkControllerMeth($controllerMeth); |
| 157 | 157 | |
| 158 | - call_user_func_array([(new $controller()),$method], $_REQUEST); |
|
| 158 | + call_user_func_array([(new $controller()), $method], $_REQUEST); |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | private function checkControllerMeth(string $controllerMeth): void |
| 162 | 162 | { |
| 163 | - $routeURI = str_replace(['{:','{','}'],'',urldecode($this->current()['uri']->getPath())); |
|
| 163 | + $routeURI = str_replace(['{:', '{', '}'], '', urldecode($this->current()['uri']->getPath())); |
|
| 164 | 164 | |
| 165 | - $controller = (string) explode('@',$controllerMeth)[0]; |
|
| 166 | - $method = (string) explode('@',$controllerMeth)[1]; |
|
| 165 | + $controller = (string) explode('@', $controllerMeth)[0]; |
|
| 166 | + $method = (string) explode('@', $controllerMeth)[1]; |
|
| 167 | 167 | |
| 168 | - if(!class_exists($controller)){ |
|
| 168 | + if (!class_exists($controller)) { |
|
| 169 | 169 | throw new \RuntimeException("Controller not found in route URI {$routeURI}"); |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | - if(!method_exists($controller, $method)){ |
|
| 172 | + if (!method_exists($controller, $method)) { |
|
| 173 | 173 | throw new \RuntimeException("Method {$method} not found in controller {$controller} in route URI {$routeURI}"); |
| 174 | 174 | } |
| 175 | 175 | |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace HnrAzevedo\Router; |
| 6 | 6 | |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | public static function group(string $prefix, \Closure $closure): Router |
| 34 | 34 | { |
| 35 | 35 | $id = sha1(date('d/m/Y h:m:i')); |
| 36 | - while(array_key_exists($id, self::getInstance()->groupsName)){ |
|
| 36 | + while (array_key_exists($id, self::getInstance()->groupsName)) { |
|
| 37 | 37 | $id = sha1(date('d/m/Y h:m:i').$id); |
| 38 | 38 | } |
| 39 | 39 | |
@@ -55,15 +55,15 @@ discard block |
||
| 55 | 55 | self::getInstance()->loaded = true; |
| 56 | 56 | self::getInstance()->sortRoutes(); |
| 57 | 57 | |
| 58 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
| 58 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
| 59 | 59 | self::getInstance()->currentRoute = $route; |
| 60 | 60 | self::getInstance()->currentRoute['name'] = $r; |
| 61 | 61 | |
| 62 | - try{ |
|
| 62 | + try { |
|
| 63 | 63 | self::getInstance()->checkMethod($route, $_SERVER['REQUEST_METHOD']); |
| 64 | 64 | self::getInstance()->checkData($route, (new Uri($_SERVER['REQUEST_URI']))->getPath()); |
| 65 | 65 | return self::getInstance(); |
| 66 | - }catch(\Exception $er){ |
|
| 66 | + }catch (\Exception $er) { |
|
| 67 | 67 | continue; |
| 68 | 68 | } |
| 69 | 69 | } |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | |
| 78 | 78 | public static function run(): RouterInterface |
| 79 | 79 | { |
| 80 | - if(!self::getInstance()->loaded){ |
|
| 80 | + if (!self::getInstance()->loaded) { |
|
| 81 | 81 | self::getInstance()->load(); |
| 82 | 82 | } |
| 83 | 83 | |
@@ -87,9 +87,9 @@ discard block |
||
| 87 | 87 | |
| 88 | 88 | self::getInstance()->executeBefore(); |
| 89 | 89 | |
| 90 | - try{ |
|
| 90 | + try { |
|
| 91 | 91 | self::getInstance()->executeRouteAction(self::getInstance()->current()['action']); |
| 92 | - }catch(\Exception $er){ |
|
| 92 | + }catch (\Exception $er) { |
|
| 93 | 93 | self::getInstance()->error = $er; |
| 94 | 94 | } |
| 95 | 95 | |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | |
| 103 | 103 | private function checkError(): void |
| 104 | 104 | { |
| 105 | - if(isset($this->error)){ |
|
| 105 | + if (isset($this->error)) { |
|
| 106 | 106 | throw $this->error; |
| 107 | 107 | } |
| 108 | 108 | } |