@@ -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, |
@@ -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 | |
@@ -23,8 +23,8 @@ discard block |
||
| 23 | 23 | $excepts = (is_array($excepts)) ? $excepts : []; |
| 24 | 24 | |
| 25 | 25 | $group = self::getInstance()->inSave()['group']; |
| 26 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
| 27 | - if($route['group'] !== $group || in_array($route['name'], $excepts)){ |
|
| 26 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
| 27 | + if ($route['group'] !== $group || in_array($route['name'], $excepts)) { |
|
| 28 | 28 | continue; |
| 29 | 29 | } |
| 30 | 30 | |
@@ -49,30 +49,30 @@ discard block |
||
| 49 | 49 | unset($uriPath[0]); |
| 50 | 50 | |
| 51 | 51 | $corretRoute = true; |
| 52 | - foreach ($routePath as $r => $routeFrag){ |
|
| 52 | + foreach ($routePath as $r => $routeFrag) { |
|
| 53 | 53 | $where = is_array($route['where']) ? $route['where'] : []; |
| 54 | 54 | $routeFrag = $this->replaceParam($where, $routeFrag, $uriPath[$r]); |
| 55 | 55 | |
| 56 | - if($routeFrag !== $uriPath[$r]){ |
|
| 56 | + if ($routeFrag !== $uriPath[$r]) { |
|
| 57 | 57 | $corretRoute = false; |
| 58 | 58 | } |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | - if(!$corretRoute){ |
|
| 61 | + if (!$corretRoute) { |
|
| 62 | 62 | throw new \Exception('continue'); |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - $_REQUEST = array_merge($_REQUEST,$this->parameters); |
|
| 65 | + $_REQUEST = array_merge($_REQUEST, $this->parameters); |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | private function replaceParam(array $where, string $ref, string $value): string |
| 69 | 69 | { |
| 70 | - if(((substr($ref,0,1) === '{') && (substr($ref,strlen($ref)-1) === '}'))) { |
|
| 71 | - $this->parameters[str_replace(['{:','{','}'],'',$ref)] = $value; |
|
| 70 | + if (((substr($ref, 0, 1) === '{') && (substr($ref, strlen($ref)-1) === '}'))) { |
|
| 71 | + $this->parameters[str_replace(['{:', '{', '}'], '', $ref)] = $value; |
|
| 72 | 72 | |
| 73 | - $this->checkValueRequire($ref,$value); |
|
| 73 | + $this->checkValueRequire($ref, $value); |
|
| 74 | 74 | |
| 75 | - if(array_key_exists(str_replace(['{:','{','}'],'',$ref),$where)){ |
|
| 75 | + if (array_key_exists(str_replace(['{:', '{', '}'], '', $ref), $where)) { |
|
| 76 | 76 | $this->matchParam($where, $ref, $value); |
| 77 | 77 | } |
| 78 | 78 | |
@@ -83,25 +83,25 @@ discard block |
||
| 83 | 83 | |
| 84 | 84 | private function checkValueRequire(string $ref, string $value): void |
| 85 | 85 | { |
| 86 | - if(substr($ref,0,2) !== '{:' && strlen($value) === 0){ |
|
| 86 | + if (substr($ref, 0, 2) !== '{:' && strlen($value) === 0) { |
|
| 87 | 87 | throw new \Exception('continue'); |
| 88 | 88 | } |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | 91 | private function checkCount(string $routePath, string $uriPath): void |
| 92 | 92 | { |
| 93 | - $countRequest = substr_count($uriPath,'/') - substr_count($routePath,'{:'); |
|
| 94 | - $countRoute = substr_count($routePath,'/') - substr_count($routePath,'{:'); |
|
| 93 | + $countRequest = substr_count($uriPath, '/')-substr_count($routePath, '{:'); |
|
| 94 | + $countRoute = substr_count($routePath, '/')-substr_count($routePath, '{:'); |
|
| 95 | 95 | |
| 96 | - if($countRequest !== $countRoute){ |
|
| 96 | + if ($countRequest !== $countRoute) { |
|
| 97 | 97 | throw new \Exception('continue'); |
| 98 | 98 | } |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | private function matchParam(array $where, string $ref, string $value): void |
| 102 | 102 | { |
| 103 | - if(substr($ref,0,2) === '{' || $value !== ''){ |
|
| 104 | - if(!preg_match("/^{$where[str_replace(['{:','{','}'],'',$ref)]}$/",$value)){ |
|
| 103 | + if (substr($ref, 0, 2) === '{' || $value !== '') { |
|
| 104 | + if (!preg_match("/^{$where[str_replace(['{:', '{', '}'], '', $ref)]}$/", $value)) { |
|
| 105 | 105 | throw new \Exception('continue'); |
| 106 | 106 | } |
| 107 | 107 | } |
@@ -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,7 +12,7 @@ discard block |
||
| 12 | 12 | |
| 13 | 13 | protected function sorted(?bool $sorted = null): bool |
| 14 | 14 | { |
| 15 | - if(null !== $sorted){ |
|
| 15 | + if (null !== $sorted) { |
|
| 16 | 16 | $this->sorted = $sorted; |
| 17 | 17 | } |
| 18 | 18 | return $this->sorted; |
@@ -20,35 +20,35 @@ discard block |
||
| 20 | 20 | |
| 21 | 21 | protected function sortRoutes(): bool |
| 22 | 22 | { |
| 23 | - if($this->sorted()){ |
|
| 23 | + if ($this->sorted()) { |
|
| 24 | 24 | return true; |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | $staticRoutes = []; |
| 28 | 28 | $paramRoutes = []; |
| 29 | 29 | |
| 30 | - foreach($this->getRoutes() as $r => $route){ |
|
| 30 | + foreach ($this->getRoutes() as $r => $route) { |
|
| 31 | 31 | |
| 32 | 32 | $path = urldecode(unserialize($route['uri'])->getPath()); |
| 33 | 33 | |
| 34 | - if(strstr($path,'{')){ |
|
| 35 | - $paramRoutes[$this->getKeyArray(substr_count($path,'/') + substr_count($path,'{'),$paramRoutes)] = $route; |
|
| 34 | + if (strstr($path, '{')) { |
|
| 35 | + $paramRoutes[$this->getKeyArray(substr_count($path, '/')+substr_count($path, '{'), $paramRoutes)] = $route; |
|
| 36 | 36 | continue; |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | - $staticRoutes[$this->getKeyArray(substr_count($path,'/'),$staticRoutes)] = $route; |
|
| 39 | + $staticRoutes[$this->getKeyArray(substr_count($path, '/'), $staticRoutes)] = $route; |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | rsort($paramRoutes); |
| 43 | 43 | rsort($staticRoutes); |
| 44 | 44 | |
| 45 | - $this->orderRoutes(array_merge($staticRoutes,$paramRoutes)); |
|
| 45 | + $this->orderRoutes(array_merge($staticRoutes, $paramRoutes)); |
|
| 46 | 46 | return $this->sorted(true); |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | private function getKeyArray(int $index, array $array): int |
| 50 | 50 | { |
| 51 | - while(array_key_exists($index,$array)){ |
|
| 51 | + while (array_key_exists($index, $array)) { |
|
| 52 | 52 | $index++; |
| 53 | 53 | } |
| 54 | 54 | return $index; |
@@ -57,8 +57,8 @@ discard block |
||
| 57 | 57 | private function orderRoutes(array $routes):void |
| 58 | 58 | { |
| 59 | 59 | $kRoutes = $routes; |
| 60 | - foreach($routes as $r => $route){ |
|
| 61 | - if(array_key_exists('name',$route)){ |
|
| 60 | + foreach ($routes as $r => $route) { |
|
| 61 | + if (array_key_exists('name', $route)) { |
|
| 62 | 62 | unset($kRoutes[$r]); |
| 63 | 63 | $kRoutes["'{$route['name']}'"] = $route; |
| 64 | 64 | } |
@@ -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 | |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | { |
| 55 | 55 | self::getInstance()->loaded = true; |
| 56 | 56 | |
| 57 | - if(null !== $name){ |
|
| 57 | + if (null !== $name) { |
|
| 58 | 58 | return self::getInstance()->loadByName($name); |
| 59 | 59 | } |
| 60 | 60 | |
@@ -62,15 +62,15 @@ discard block |
||
| 62 | 62 | |
| 63 | 63 | $requestMethod = (isset($_REQUEST['REQUEST_METHOD'])) ? $_REQUEST['REQUEST_METHOD'] : $_SERVER['REQUEST_METHOD']; |
| 64 | 64 | |
| 65 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
| 65 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
| 66 | 66 | self::getInstance()->currentRoute = $route; |
| 67 | 67 | self::getInstance()->currentRoute['name'] = $r; |
| 68 | 68 | |
| 69 | - try{ |
|
| 69 | + try { |
|
| 70 | 70 | self::getInstance()->checkMethod($route, $requestMethod); |
| 71 | 71 | self::getInstance()->checkData($route, (new Uri($_SERVER['REQUEST_URI']))->getPath()); |
| 72 | 72 | return self::getInstance(); |
| 73 | - }catch(\Exception $er){ |
|
| 73 | + }catch (\Exception $er) { |
|
| 74 | 74 | continue; |
| 75 | 75 | } |
| 76 | 76 | } |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | |
| 85 | 85 | public static function run(?string $name = null): RouterInterface |
| 86 | 86 | { |
| 87 | - if(!self::getInstance()->loaded){ |
|
| 87 | + if (!self::getInstance()->loaded) { |
|
| 88 | 88 | self::getInstance()->load($name); |
| 89 | 89 | } |
| 90 | 90 | |
@@ -94,10 +94,10 @@ discard block |
||
| 94 | 94 | |
| 95 | 95 | self::getInstance()->executeBefore(); |
| 96 | 96 | |
| 97 | - try{ |
|
| 97 | + try { |
|
| 98 | 98 | $action = unserialize(self::getInstance()->current()['action']); |
| 99 | - self::getInstance()->executeRouteAction( (is_string($action)) ? $action : $action->getClosure() ); |
|
| 100 | - }catch(\Exception $er){ |
|
| 99 | + self::getInstance()->executeRouteAction((is_string($action)) ? $action : $action->getClosure()); |
|
| 100 | + }catch (\Exception $er) { |
|
| 101 | 101 | self::getInstance()->error = $er; |
| 102 | 102 | } |
| 103 | 103 | |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | |
| 111 | 111 | private function checkError(): void |
| 112 | 112 | { |
| 113 | - if(isset($this->error)){ |
|
| 113 | + if (isset($this->error)) { |
|
| 114 | 114 | throw $this->error; |
| 115 | 115 | } |
| 116 | 116 | } |
@@ -124,7 +124,7 @@ discard block |
||
| 124 | 124 | |
| 125 | 125 | public static function routes(?array $routes = null): array |
| 126 | 126 | { |
| 127 | - if(null !== $routes){ |
|
| 127 | + if (null !== $routes) { |
|
| 128 | 128 | self::getInstance()->setRoutes($routes); |
| 129 | 129 | self::getInstance()->sortRoutes(); |
| 130 | 130 | } |
@@ -70,7 +70,7 @@ discard block |
||
| 70 | 70 | self::getInstance()->checkMethod($route, $requestMethod); |
| 71 | 71 | self::getInstance()->checkData($route, (new Uri($_SERVER['REQUEST_URI']))->getPath()); |
| 72 | 72 | return self::getInstance(); |
| 73 | - }catch(\Exception $er){ |
|
| 73 | + } catch(\Exception $er){ |
|
| 74 | 74 | continue; |
| 75 | 75 | } |
| 76 | 76 | } |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | try{ |
| 98 | 98 | $action = unserialize(self::getInstance()->current()['action']); |
| 99 | 99 | self::getInstance()->executeRouteAction( (is_string($action)) ? $action : $action->getClosure() ); |
| 100 | - }catch(\Exception $er){ |
|
| 100 | + } catch(\Exception $er){ |
|
| 101 | 101 | self::getInstance()->error = $er; |
| 102 | 102 | } |
| 103 | 103 | |
@@ -69,9 +69,9 @@ discard block |
||
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | $routes[$index] = [ |
| 72 | - 'uri' => serialize(new Uri(self::getInstance()->getHost().self::getInstance()->getPrefix().$uri)), |
|
| 73 | - 'action' => (is_callable($closure)) ? serialize(new SerializableClosure($closure)) : serialize($closure), |
|
| 74 | - 'method' => strtoupper($method), |
|
| 72 | + 'uri' => serialize(new Uri(self::getInstance()->getHost().self::getInstance()->getPrefix().$uri)), |
|
| 73 | + 'action' => (is_callable($closure)) ? serialize(new SerializableClosure($closure)) : serialize($closure), |
|
| 74 | + 'method' => strtoupper($method), |
|
| 75 | 75 | 'middlewares' => [], |
| 76 | 76 | 'where' => [], |
| 77 | 77 | 'before' => [], |
@@ -89,7 +89,7 @@ discard block |
||
| 89 | 89 | private static function checkDuplicity(string $uri, string $method): void |
| 90 | 90 | { |
| 91 | 91 | foreach(self::getInstance()->getRoutes() as $route){ |
| 92 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
| 92 | + if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
| 93 | 93 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
| 94 | 94 | } |
| 95 | 95 | } |
@@ -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,32 +13,32 @@ discard block |
||
| 13 | 13 | |
| 14 | 14 | public static function get(string $uri, $closure): RouterInterface |
| 15 | 15 | { |
| 16 | - return self::set('get',$uri,$closure); |
|
| 16 | + return self::set('get', $uri, $closure); |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | public static function post(string $uri, $closure): RouterInterface |
| 20 | 20 | { |
| 21 | - return self::set('post',$uri,$closure); |
|
| 21 | + return self::set('post', $uri, $closure); |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | public static function ajax(string $uri, $closure): RouterInterface |
| 25 | 25 | { |
| 26 | - return self::set('ajax',$uri,$closure); |
|
| 26 | + return self::set('ajax', $uri, $closure); |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | public static function delete(string $uri, $closure): RouterInterface |
| 30 | 30 | { |
| 31 | - return self::set('delete',$uri,$closure); |
|
| 31 | + return self::set('delete', $uri, $closure); |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | public static function put(string $uri, $closure): RouterInterface |
| 35 | 35 | { |
| 36 | - return self::set('put',$uri,$closure); |
|
| 36 | + return self::set('put', $uri, $closure); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | public static function patch(string $uri, $closure): RouterInterface |
| 40 | 40 | { |
| 41 | - return self::set('patch',$uri,$closure); |
|
| 41 | + return self::set('patch', $uri, $closure); |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | public static function match(string $method, string $uri, $closure): RouterInterface |
@@ -49,22 +49,22 @@ discard block |
||
| 49 | 49 | |
| 50 | 50 | public static function any(string $uri, $closure): RouterInterface |
| 51 | 51 | { |
| 52 | - return self::set('*',$uri,$closure); |
|
| 52 | + return self::set('*', $uri, $closure); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | private static $count = 0; |
| 56 | 56 | |
| 57 | 57 | private static function set(string $method, string $uri, $closure): RouterInterface |
| 58 | 58 | { |
| 59 | - $uri = (substr($uri,0,1) !=='/' and strlen($uri) > 0) ? "/{$uri}" : $uri; |
|
| 59 | + $uri = (substr($uri, 0, 1) !== '/' and strlen($uri) > 0) ? "/{$uri}" : $uri; |
|
| 60 | 60 | |
| 61 | - self::checkDuplicity($uri,$method); |
|
| 61 | + self::checkDuplicity($uri, $method); |
|
| 62 | 62 | |
| 63 | 63 | $routes = self::getInstance()->getRoutes(); |
| 64 | 64 | |
| 65 | 65 | $index = sha1(date('d/m/Y H:i:s').count($routes)); |
| 66 | 66 | |
| 67 | - while(array_key_exists($index,$routes)){ |
|
| 67 | + while (array_key_exists($index, $routes)) { |
|
| 68 | 68 | $index = sha1(date('d/m/Y H:i:s').rand(count($routes))); |
| 69 | 69 | } |
| 70 | 70 | |
@@ -88,8 +88,8 @@ discard block |
||
| 88 | 88 | |
| 89 | 89 | private static function checkDuplicity(string $uri, string $method): void |
| 90 | 90 | { |
| 91 | - foreach(self::getInstance()->getRoutes() as $route){ |
|
| 92 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
| 91 | + foreach (self::getInstance()->getRoutes() as $route) { |
|
| 92 | + if (md5($route['uri'].$route['method']) === md5($uri.$method)) { |
|
| 93 | 93 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
| 94 | 94 | } |
| 95 | 95 | } |
@@ -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 | |
@@ -22,8 +22,8 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | public static function globalMiddlewares(array $middlewares): RouterInterface |
| 24 | 24 | { |
| 25 | - foreach($middlewares as $middleware){ |
|
| 26 | - if(!class_exists($middleware)){ |
|
| 25 | + foreach ($middlewares as $middleware) { |
|
| 26 | + if (!class_exists($middleware)) { |
|
| 27 | 27 | throw new \RuntimeException("Middleware class {$middleware} not exists"); |
| 28 | 28 | } |
| 29 | 29 | } |
@@ -39,8 +39,8 @@ discard block |
||
| 39 | 39 | public static function middleware(array $middlewares): RouterInterface |
| 40 | 40 | { |
| 41 | 41 | $route = self::getInstance()->inSave(); |
| 42 | - $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'],$middlewares) : $middlewares; |
|
| 43 | - self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->getRoutes())); |
|
| 42 | + $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'], $middlewares) : $middlewares; |
|
| 43 | + self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->getRoutes())); |
|
| 44 | 44 | return self::getInstance(); |
| 45 | 45 | } |
| 46 | 46 | |
@@ -48,8 +48,8 @@ discard block |
||
| 48 | 48 | { |
| 49 | 49 | $excepts = (is_array($excepts)) ? $excepts : []; |
| 50 | 50 | $group = self::getInstance()->inSave()['group']; |
| 51 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
| 52 | - if($route['group'] !== $group || in_array($route['name'], $excepts)){ |
|
| 51 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
| 52 | + if ($route['group'] !== $group || in_array($route['name'], $excepts)) { |
|
| 53 | 53 | continue; |
| 54 | 54 | } |
| 55 | 55 | |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | |
| 62 | 62 | private static function existMiddleware(string $name): void |
| 63 | 63 | { |
| 64 | - if(!class_exists($name) && !array_key_exists($name,self::$globalMiddlewares)){ |
|
| 64 | + if (!class_exists($name) && !array_key_exists($name, self::$globalMiddlewares)) { |
|
| 65 | 65 | throw new \RuntimeException("Middleware {$name} does not exist"); |
| 66 | 66 | } |
| 67 | 67 | } |
@@ -70,9 +70,9 @@ discard block |
||
| 70 | 70 | { |
| 71 | 71 | $factory = new Factory(); |
| 72 | 72 | |
| 73 | - $this->serverRequest = (!isset($this->serverRequest)) ? $factory->createServerRequest($_SERVER['REQUEST_METHOD'], unserialize($this->current()['uri']),['route' => $this->current()]) : $this->serverRequest; |
|
| 73 | + $this->serverRequest = (!isset($this->serverRequest)) ? $factory->createServerRequest($_SERVER['REQUEST_METHOD'], unserialize($this->current()['uri']), ['route' => $this->current()]) : $this->serverRequest; |
|
| 74 | 74 | |
| 75 | - foreach ($this->current()['middlewares'] as $middleware){ |
|
| 75 | + foreach ($this->current()['middlewares'] as $middleware) { |
|
| 76 | 76 | $this->currentMiddlewares[] = (class_exists($middleware)) ? new $middleware() : new $this->globalMiddlewares[$middleware](); |
| 77 | 77 | } |
| 78 | 78 | |
@@ -87,20 +87,20 @@ discard block |
||
| 87 | 87 | |
| 88 | 88 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 89 | 89 | { |
| 90 | - if(!$this->getInstance()->loaded()){ |
|
| 90 | + if (!$this->getInstance()->loaded()) { |
|
| 91 | 91 | $this->getInstance()->load(); |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | $route = $this->getInstance()->current(); |
| 95 | - if(!empty($route)){ |
|
| 96 | - $route['action'] = function(){ |
|
| 95 | + if (!empty($route)) { |
|
| 96 | + $route['action'] = function() { |
|
| 97 | 97 | $this->getInstance()->executeBefore(); |
| 98 | 98 | $this->getInstance()->executeRouteAction($this->getInstance()->current()['action']); |
| 99 | 99 | $this->getInstance()->executeAfter(); |
| 100 | 100 | }; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - return $this->next($handler)->handle($request->withAttribute('route',$route)); |
|
| 103 | + return $this->next($handler)->handle($request->withAttribute('route', $route)); |
|
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | private function next(RequestHandlerInterface $defaultHandler): RequestHandlerInterface |
@@ -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,7 +20,7 @@ discard block |
||
| 20 | 20 | $this->beforeAll = (!isset($this->beforeAll)) ? function() {} : $this->beforeAll; |
| 21 | 21 | $this->afterAll = (!isset($this->afterAll)) ? function() {} : $this->afterAll; |
| 22 | 22 | |
| 23 | - if($state === 'before'){ |
|
| 23 | + if ($state === 'before') { |
|
| 24 | 24 | return ($except) ? $this->beforeExcepts : $this->beforeAll; |
| 25 | 25 | } |
| 26 | 26 | |
@@ -29,8 +29,8 @@ discard block |
||
| 29 | 29 | |
| 30 | 30 | protected function setState(string $state, $settable, bool $except = false): bool |
| 31 | 31 | { |
| 32 | - if($state === 'before'){ |
|
| 33 | - if($except){ |
|
| 32 | + if ($state === 'before') { |
|
| 33 | + if ($except) { |
|
| 34 | 34 | $this->beforeExcepts = $settable; |
| 35 | 35 | return true; |
| 36 | 36 | } |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | return true; |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - if($except){ |
|
| 42 | + if ($except) { |
|
| 43 | 43 | $this->afterExcepts = $settable; |
| 44 | 44 | return true; |
| 45 | 45 | } |
@@ -60,14 +60,14 @@ discard block |
||
| 60 | 60 | |
| 61 | 61 | public static function beforeAll($closure, ?array $excepts = null): RouterInterface |
| 62 | 62 | { |
| 63 | - self::getInstance()->setState('before', (is_array($excepts)) ? $excepts : [] ,true); |
|
| 63 | + self::getInstance()->setState('before', (is_array($excepts)) ? $excepts : [], true); |
|
| 64 | 64 | self::getInstance()->setState('before', $closure, false); |
| 65 | 65 | return self::getInstance(); |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | public static function afterAll($closure, ?array $excepts = null): RouterInterface |
| 69 | 69 | { |
| 70 | - self::getInstance()->setState('after', (is_array($excepts)) ? $excepts : [] ,true); |
|
| 70 | + self::getInstance()->setState('after', (is_array($excepts)) ? $excepts : [], true); |
|
| 71 | 71 | self::getInstance()->setState('after', $closure, false); |
| 72 | 72 | return self::getInstance(); |
| 73 | 73 | } |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | |
| 85 | 85 | protected function executeRouteAction($action): bool |
| 86 | 86 | { |
| 87 | - if(is_callable($action)){ |
|
| 87 | + if (is_callable($action)) { |
|
| 88 | 88 | call_user_func_array($action, $_REQUEST); |
| 89 | 89 | return true; |
| 90 | 90 | } |
@@ -99,9 +99,9 @@ discard block |
||
| 99 | 99 | $excepts = (is_array($excepts)) ? $excepts : []; |
| 100 | 100 | $group = self::getInstance()->inSave()['group']; |
| 101 | 101 | |
| 102 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
| 103 | - if($route['group'] === $group && !in_array($r, $excepts)){ |
|
| 104 | - self::getInstance()->getRoutes()[$r][$state] = (is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
| 102 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
| 103 | + if ($route['group'] === $group && !in_array($r, $excepts)) { |
|
| 104 | + self::getInstance()->getRoutes()[$r][$state] = (is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
| 105 | 105 | } |
| 106 | 106 | } |
| 107 | 107 | |
@@ -111,14 +111,14 @@ discard block |
||
| 111 | 111 | private static function addInRoute(string $state, $closure): RouterInterface |
| 112 | 112 | { |
| 113 | 113 | $route = self::getInstance()->inSave(); |
| 114 | - $route[$state] = (!is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
| 115 | - self::updateRoute($route,array_key_last(self::getInstance()->getRoutes())); |
|
| 114 | + $route[$state] = (!is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
| 115 | + self::updateRoute($route, array_key_last(self::getInstance()->getRoutes())); |
|
| 116 | 116 | return self::getInstance(); |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | protected function executeBefore(): void |
| 120 | 120 | { |
| 121 | - if(!in_array($this->currentName(), (array) $this->getState('before', true))){ |
|
| 121 | + if (!in_array($this->currentName(), (array) $this->getState('before', true))) { |
|
| 122 | 122 | ($this->getState('before', false))(); |
| 123 | 123 | } |
| 124 | 124 | |
@@ -129,15 +129,15 @@ discard block |
||
| 129 | 129 | { |
| 130 | 130 | $this->executeState('after'); |
| 131 | 131 | |
| 132 | - if(!in_array($this->currentName(), (array) $this->getState('after', true))){ |
|
| 132 | + if (!in_array($this->currentName(), (array) $this->getState('after', true))) { |
|
| 133 | 133 | ($this->getState('after', false))(); |
| 134 | 134 | } |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | private function executeState(string $stage): void |
| 138 | 138 | { |
| 139 | - foreach($this->current()[$stage] as $state){ |
|
| 140 | - if(is_callable($state)){ |
|
| 139 | + foreach ($this->current()[$stage] as $state) { |
|
| 140 | + if (is_callable($state)) { |
|
| 141 | 141 | $state(); |
| 142 | 142 | continue; |
| 143 | 143 | } |
@@ -148,31 +148,31 @@ discard block |
||
| 148 | 148 | |
| 149 | 149 | private function executeController(string $controllerMeth): void |
| 150 | 150 | { |
| 151 | - if(count(explode('@',$controllerMeth)) !== 2){ |
|
| 151 | + if (count(explode('@', $controllerMeth)) !== 2) { |
|
| 152 | 152 | $path = urldecode($this->current()['uri']->getPath()); |
| 153 | 153 | throw new \RuntimeException("Misconfigured route action ({$path})"); |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | - $controller = (string) explode('@',$controllerMeth)[0]; |
|
| 157 | - $method = (string) explode('@',$controllerMeth)[1]; |
|
| 156 | + $controller = (string) explode('@', $controllerMeth)[0]; |
|
| 157 | + $method = (string) explode('@', $controllerMeth)[1]; |
|
| 158 | 158 | |
| 159 | 159 | $this->checkControllerMeth($controllerMeth); |
| 160 | 160 | |
| 161 | - call_user_func_array([(new $controller()),$method], $_REQUEST); |
|
| 161 | + call_user_func_array([(new $controller()), $method], $_REQUEST); |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | private function checkControllerMeth(string $controllerMeth): void |
| 165 | 165 | { |
| 166 | - $routeURI = str_replace(['{:','{','}'],'',urldecode(unserialize($this->current()['uri'])->getPath())); |
|
| 166 | + $routeURI = str_replace(['{:', '{', '}'], '', urldecode(unserialize($this->current()['uri'])->getPath())); |
|
| 167 | 167 | |
| 168 | - $controller = (string) explode('@',$controllerMeth)[0]; |
|
| 169 | - $method = (string) explode('@',$controllerMeth)[1]; |
|
| 168 | + $controller = (string) explode('@', $controllerMeth)[0]; |
|
| 169 | + $method = (string) explode('@', $controllerMeth)[1]; |
|
| 170 | 170 | |
| 171 | - if(!class_exists($controller)){ |
|
| 171 | + if (!class_exists($controller)) { |
|
| 172 | 172 | throw new \RuntimeException("Controller not found in route URI {$routeURI}"); |
| 173 | 173 | } |
| 174 | 174 | |
| 175 | - if(!method_exists($controller, $method)){ |
|
| 175 | + if (!method_exists($controller, $method)) { |
|
| 176 | 176 | throw new \RuntimeException("Method {$method} not found in controller {$controller} in route URI {$routeURI}"); |
| 177 | 177 | } |
| 178 | 178 | |