@@ -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 | |
@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | { |
| 20 | 20 | $this->hasRouteName($name); |
| 21 | 21 | |
| 22 | - if(!$this->loaded()){ |
|
| 22 | + if (!$this->loaded()) { |
|
| 23 | 23 | $this->loadIn($name); |
| 24 | 24 | } |
| 25 | 25 | |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | |
| 29 | 29 | public function hasCurrentRoute(): void |
| 30 | 30 | { |
| 31 | - if(!isset($this->currentRoute)){ |
|
| 31 | + if (!isset($this->currentRoute)) { |
|
| 32 | 32 | throw new \RuntimeException('Route not yet loaded'); |
| 33 | 33 | } |
| 34 | 34 | } |
@@ -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 | |
@@ -20,8 +20,8 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | public static function globalMiddlewares(array $middlewares): RouterInterface |
| 22 | 22 | { |
| 23 | - foreach($middlewares as $middleware){ |
|
| 24 | - if(!class_exists($middleware)){ |
|
| 23 | + foreach ($middlewares as $middleware) { |
|
| 24 | + if (!class_exists($middleware)) { |
|
| 25 | 25 | throw new \RuntimeException("Middleware class {$middleware} not exists"); |
| 26 | 26 | } |
| 27 | 27 | } |
@@ -37,16 +37,16 @@ discard block |
||
| 37 | 37 | public static function middleware(array $middlewares): RouterInterface |
| 38 | 38 | { |
| 39 | 39 | $route = self::getInstance()->inSave(); |
| 40 | - $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'],$middlewares) : $middlewares; |
|
| 41 | - self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->getRoutes())); |
|
| 40 | + $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'], $middlewares) : $middlewares; |
|
| 41 | + self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->getRoutes())); |
|
| 42 | 42 | return self::getInstance(); |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | public static function groupMiddlewares(array $middlewares, array $excepts): RouterInterface |
| 46 | 46 | { |
| 47 | 47 | $group = self::getInstance()->inSave()['group']; |
| 48 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
| 49 | - if($route['group'] !== $group || in_array($route['name'], $excepts)){ |
|
| 48 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
| 49 | + if ($route['group'] !== $group || in_array($route['name'], $excepts)) { |
|
| 50 | 50 | continue; |
| 51 | 51 | } |
| 52 | 52 | |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | |
| 59 | 59 | private static function existMiddleware(string $name): void |
| 60 | 60 | { |
| 61 | - if(!class_exists($name) && !array_key_exists($name,self::$globalMiddlewares)){ |
|
| 61 | + if (!class_exists($name) && !array_key_exists($name, self::$globalMiddlewares)) { |
|
| 62 | 62 | throw new \RuntimeException("Middleware {$name} does not exist"); |
| 63 | 63 | } |
| 64 | 64 | } |
@@ -67,9 +67,9 @@ discard block |
||
| 67 | 67 | { |
| 68 | 68 | $factory = new Factory(); |
| 69 | 69 | |
| 70 | - $this->serverRequest = (!isset($this->serverRequest)) ? $factory->createServerRequest($_SERVER['REQUEST_METHOD'], $this->current()['uri'],['route' => $this->current()]) : $this->serverRequest; |
|
| 70 | + $this->serverRequest = (!isset($this->serverRequest)) ? $factory->createServerRequest($_SERVER['REQUEST_METHOD'], $this->current()['uri'], ['route' => $this->current()]) : $this->serverRequest; |
|
| 71 | 71 | |
| 72 | - foreach ($this->current()['middlewares'] as $middleware){ |
|
| 72 | + foreach ($this->current()['middlewares'] as $middleware) { |
|
| 73 | 73 | $this->currentMiddlewares[] = (class_exists($middleware)) ? new $middleware() : new $this->globalMiddlewares[$middleware](); |
| 74 | 74 | } |
| 75 | 75 | |
@@ -84,20 +84,20 @@ discard block |
||
| 84 | 84 | |
| 85 | 85 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 86 | 86 | { |
| 87 | - if(!$this->getInstance()->loaded()){ |
|
| 87 | + if (!$this->getInstance()->loaded()) { |
|
| 88 | 88 | $this->getInstance()->load(); |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | $route = $this->getInstance()->current(); |
| 92 | - if(!empty($route)){ |
|
| 93 | - $route['action'] = function(){ |
|
| 92 | + if (!empty($route)) { |
|
| 93 | + $route['action'] = function() { |
|
| 94 | 94 | $this->getInstance()->executeBefore(); |
| 95 | 95 | $this->getInstance()->executeRouteAction($this->getInstance()->current()['action']); |
| 96 | 96 | $this->getInstance()->executeAfter(); |
| 97 | 97 | }; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - return $this->next($handler)->handle($request->withAttribute('route',$route)); |
|
| 100 | + return $this->next($handler)->handle($request->withAttribute('route', $route)); |
|
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | private function next(RequestHandlerInterface $defaultHandler): RequestHandlerInterface |
@@ -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,16 +131,16 @@ 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]*' |
| 139 | 139 | ]); |
| 140 | 140 | }); |
| 141 | 141 | |
| 142 | -Router::group('/admin2', function(){ |
|
| 143 | - Router::get('/users/{teste}', function($teste){ |
|
| 142 | +Router::group('/admin2', function() { |
|
| 143 | + Router::get('/users/{teste}', function($teste) { |
|
| 144 | 144 | echo $teste; |
| 145 | 145 | })->name('teste'); |
| 146 | 146 | }) |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | */ |
| 152 | 152 | ->groupWhere([ |
| 153 | 153 | 'teste'=>'[a-zA-Z]*' |
| 154 | -],[]); |
|
| 154 | +], []); |
|
| 155 | 155 | |
| 156 | 156 | |
| 157 | 157 | |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | /** |
| 169 | 169 | * Defining route middleware - implements Psr\Http\Server\MiddlewareInterface |
| 170 | 170 | */ |
| 171 | -Router::get('/passingParameters/{:optionalParam}', function($optionalParam = null){ |
|
| 171 | +Router::get('/passingParameters/{:optionalParam}', function($optionalParam = null) { |
|
| 172 | 172 | echo $optionalParam; |
| 173 | 173 | })->middleware([ |
| 174 | 174 | HnrAzevedo\Router\Example\Middleware\Auth::class |
@@ -177,7 +177,7 @@ discard block |
||
| 177 | 177 | /** |
| 178 | 178 | * Defining middleware by nickname |
| 179 | 179 | */ |
| 180 | -Router::get('/lasted', function(){ |
|
| 180 | +Router::get('/lasted', function() { |
|
| 181 | 181 | // |
| 182 | 182 | })->middleware([ |
| 183 | 183 | 'Lasted' |
@@ -187,7 +187,7 @@ discard block |
||
| 187 | 187 | * Defining multiple middlewares |
| 188 | 188 | * NOTE: Importantly, the execution follows the same order of definition |
| 189 | 189 | */ |
| 190 | -Router::get('/middlewares', function(){ |
|
| 190 | +Router::get('/middlewares', function() { |
|
| 191 | 191 | // |
| 192 | 192 | })->middleware([ |
| 193 | 193 | HnrAzevedo\Router\Example\Middleware\Auth::class, |