@@ -64,9 +64,9 @@ discard block |
||
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | $routes[$index] = [ |
| 67 | - 'uri' => new Uri(self::getInstance()->getHost().self::getInstance()->getPrefix().$uri), |
|
| 68 | - 'action' => $closure, |
|
| 69 | - 'method' => strtoupper($method), |
|
| 67 | + 'uri' => new Uri(self::getInstance()->getHost().self::getInstance()->getPrefix().$uri), |
|
| 68 | + 'action' => $closure, |
|
| 69 | + 'method' => strtoupper($method), |
|
| 70 | 70 | 'middlewares' => [], |
| 71 | 71 | 'where' => null, |
| 72 | 72 | 'before' => [], |
@@ -83,7 +83,7 @@ discard block |
||
| 83 | 83 | private static function checkDuplicity(string $uri, string $method): void |
| 84 | 84 | { |
| 85 | 85 | foreach(self::getInstance()->getRoutes() as $route){ |
| 86 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
| 86 | + if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
| 87 | 87 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
| 88 | 88 | } |
| 89 | 89 | } |
@@ -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 | } |
@@ -35,16 +35,16 @@ discard block |
||
| 35 | 35 | |
| 36 | 36 | public static function middleware($middlewares): RouterInterface |
| 37 | 37 | { |
| 38 | - $middlewares = (is_array($middlewares)) ? $middlewares : [ $middlewares ]; |
|
| 38 | + $middlewares = (is_array($middlewares)) ? $middlewares : [$middlewares]; |
|
| 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 | private static function existMiddleware(string $name): void |
| 46 | 46 | { |
| 47 | - if(!class_exists($name) && !array_key_exists($name,self::$globalMiddlewares)){ |
|
| 47 | + if (!class_exists($name) && !array_key_exists($name, self::$globalMiddlewares)) { |
|
| 48 | 48 | throw new \RuntimeException("Middleware {$name} does not exist"); |
| 49 | 49 | } |
| 50 | 50 | } |
@@ -53,9 +53,9 @@ discard block |
||
| 53 | 53 | { |
| 54 | 54 | $factory = new Factory(); |
| 55 | 55 | |
| 56 | - $this->serverRequest = (!isset($this->serverRequest)) ? $factory->createServerRequest($_SERVER['REQUEST_METHOD'], $this->current()['uri'],['route' => $this->current()]) : $this->serverRequest; |
|
| 56 | + $this->serverRequest = (!isset($this->serverRequest)) ? $factory->createServerRequest($_SERVER['REQUEST_METHOD'], $this->current()['uri'], ['route' => $this->current()]) : $this->serverRequest; |
|
| 57 | 57 | |
| 58 | - foreach ($this->current()['middlewares'] as $middleware){ |
|
| 58 | + foreach ($this->current()['middlewares'] as $middleware) { |
|
| 59 | 59 | $this->currentMiddlewares[] = (class_exists($middleware)) ? new $middleware() : new $this->globalMiddlewares[$middleware](); |
| 60 | 60 | } |
| 61 | 61 | |
@@ -30,30 +30,30 @@ discard block |
||
| 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 | throw new \Exception('continue'); |
| 87 | 87 | } |
| 88 | 88 | } |
@@ -9,12 +9,12 @@ |
||
| 9 | 9 | /** |
| 10 | 10 | * @property string $error |
| 11 | 11 | */ |
| 12 | -class Auth extends Middleware{ |
|
| 12 | +class Auth extends Middleware { |
|
| 13 | 13 | |
| 14 | 14 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 15 | 15 | { |
| 16 | 16 | var_dump('Auth'); |
| 17 | - if(!array_key_exists('user',$_SESSION)){ |
|
| 17 | + if (!array_key_exists('user', $_SESSION)) { |
|
| 18 | 18 | $this->error = 'The user must be logged in to the system'; |
| 19 | 19 | } |
| 20 | 20 | |
@@ -10,12 +10,12 @@ |
||
| 10 | 10 | /** |
| 11 | 11 | * @property string $error |
| 12 | 12 | */ |
| 13 | -class Lasted extends Middleware{ |
|
| 13 | +class Lasted extends Middleware { |
|
| 14 | 14 | |
| 15 | 15 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 16 | 16 | { |
| 17 | 17 | var_dump('Lasted'); |
| 18 | - if(!isset($this->error)){ |
|
| 18 | + if (!isset($this->error)) { |
|
| 19 | 19 | throw new Exception("Access not belonged: {$this->error}"); |
| 20 | 20 | } |
| 21 | 21 | |
@@ -34,7 +34,7 @@ |
||
| 34 | 34 | |
| 35 | 35 | |
| 36 | 36 | Router::get('/6my-accounttttt/{param1}/{param2}','HnrAzevedo\Router\Example\Controllers\User@requireLogin') |
| 37 | - ->middleware([HnrAzevedo\Router\Example\Middleware\Auth::class,'Lasted']); |
|
| 37 | + ->middleware([HnrAzevedo\Router\Example\Middleware\Auth::class,'Lasted']); |
|
| 38 | 38 | |
| 39 | 39 | Router::get('/7my-account/{:teste}','Controller\\User:my_account')->where([ |
| 40 | 40 | 'teste'=>'[a-zA-Z]{1,10}' |
@@ -4,28 +4,28 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | |
| 7 | -Router::beforeAll(function(){ |
|
| 7 | +Router::beforeAll(function() { |
|
| 8 | 8 | echo '<br><b>beforeAll</b><br>'; |
| 9 | 9 | },['testes']); |
| 10 | 10 | |
| 11 | -Router::afterAll(function(){ |
|
| 11 | +Router::afterAll(function() { |
|
| 12 | 12 | echo '<br><b>afterAll</b><br>'; |
| 13 | 13 | },['testes']); |
| 14 | 14 | |
| 15 | 15 | |
| 16 | -Router::match('GET|POST|AJAX','/{1parameter}/{otherparameter}', function($parameter, $otherparameter){ |
|
| 16 | +Router::match('GET|POST|AJAX', '/{1parameter}/{otherparameter}', function($parameter, $otherparameter) { |
|
| 17 | 17 | echo "Parameter 1:{$parameter}, Parameter 2:{$otherparameter}."; |
| 18 | 18 | }); |
| 19 | 19 | |
| 20 | -Router::get('/{2controller}/{method}','{controller}:{method}'); |
|
| 20 | +Router::get('/{2controller}/{method}', '{controller}:{method}'); |
|
| 21 | 21 | |
| 22 | -Router::get('/3my-account','Controller\\User:my_account')->before(function(){ |
|
| 22 | +Router::get('/3my-account', 'Controller\\User:my_account')->before(function() { |
|
| 23 | 23 | echo '1'; |
| 24 | 24 | }); |
| 25 | 25 | |
| 26 | -Router::get('/4my-account/teste/teste','Controller\\User:my_account')->name('2'); |
|
| 26 | +Router::get('/4my-account/teste/teste', 'Controller\\User:my_account')->name('2'); |
|
| 27 | 27 | |
| 28 | -Router::get('/5my-account/{:teste}/{teste2}',function($teste, $teste2){ |
|
| 28 | +Router::get('/5my-account/{:teste}/{teste2}', function($teste, $teste2) { |
|
| 29 | 29 | echo $teste.'-'.$teste2; |
| 30 | 30 | })->where([ |
| 31 | 31 | 'teste'=>'[a-zA-Z]{1,10}', |
@@ -33,14 +33,14 @@ discard block |
||
| 33 | 33 | ]); |
| 34 | 34 | |
| 35 | 35 | |
| 36 | -Router::get('/6my-accounttttt/{param1}/{param2}','HnrAzevedo\Router\Example\Controllers\User@requireLogin') |
|
| 37 | - ->middleware([HnrAzevedo\Router\Example\Middleware\Auth::class,'Lasted']); |
|
| 36 | +Router::get('/6my-accounttttt/{param1}/{param2}', 'HnrAzevedo\Router\Example\Controllers\User@requireLogin') |
|
| 37 | + ->middleware([HnrAzevedo\Router\Example\Middleware\Auth::class, 'Lasted']); |
|
| 38 | 38 | |
| 39 | -Router::get('/7my-account/{:teste}','Controller\\User:my_account')->where([ |
|
| 39 | +Router::get('/7my-account/{:teste}', 'Controller\\User:my_account')->where([ |
|
| 40 | 40 | 'teste'=>'[a-zA-Z]{1,10}' |
| 41 | 41 | ]); |
| 42 | 42 | |
| 43 | 43 | |
| 44 | -Router::any('/8',function(){ |
|
| 44 | +Router::any('/8', function() { |
|
| 45 | 45 | // |
| 46 | 46 | })->name('index'); |