@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | { |
17 | 17 | $this->hasRouteName($name); |
18 | 18 | |
19 | - if(!$this->loaded){ |
|
19 | + if (!$this->loaded) { |
|
20 | 20 | $this->loadIn($name); |
21 | 21 | } |
22 | 22 | |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | |
26 | 26 | public function hasCurrentRoute(): void |
27 | 27 | { |
28 | - if(!isset($this->currentRoute)){ |
|
28 | + if (!isset($this->currentRoute)) { |
|
29 | 29 | throw new \RuntimeException('Route not yet loaded'); |
30 | 30 | } |
31 | 31 | } |
@@ -12,32 +12,32 @@ discard block |
||
12 | 12 | |
13 | 13 | public static function get(string $uri, $closure): RouterInterface |
14 | 14 | { |
15 | - return self::set('get',$uri,$closure); |
|
15 | + return self::set('get', $uri, $closure); |
|
16 | 16 | } |
17 | 17 | |
18 | 18 | public static function post(string $uri, $closure): RouterInterface |
19 | 19 | { |
20 | - return self::set('post',$uri,$closure); |
|
20 | + return self::set('post', $uri, $closure); |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | public static function ajax(string $uri, $closure): RouterInterface |
24 | 24 | { |
25 | - return self::set('ajax',$uri,$closure); |
|
25 | + return self::set('ajax', $uri, $closure); |
|
26 | 26 | } |
27 | 27 | |
28 | 28 | public static function delete(string $uri, $closure): RouterInterface |
29 | 29 | { |
30 | - return self::set('delete',$uri,$closure); |
|
30 | + return self::set('delete', $uri, $closure); |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | public static function put(string $uri, $closure): RouterInterface |
34 | 34 | { |
35 | - return self::set('put',$uri,$closure); |
|
35 | + return self::set('put', $uri, $closure); |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | public static function patch(string $uri, $closure): RouterInterface |
39 | 39 | { |
40 | - return self::set('patch',$uri,$closure); |
|
40 | + return self::set('patch', $uri, $closure); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | public static function match(string $method, string $uri, $closure): RouterInterface |
@@ -48,14 +48,14 @@ discard block |
||
48 | 48 | |
49 | 49 | public static function any(string $uri, $closure): RouterInterface |
50 | 50 | { |
51 | - return self::set('*',$uri,$closure); |
|
51 | + return self::set('*', $uri, $closure); |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | private static function set(string $method, string $uri, $closure): RouterInterface |
55 | 55 | { |
56 | - $uri = (substr($uri,0,1) !=='/' and strlen($uri) > 0) ? "/{$uri}" : $uri; |
|
56 | + $uri = (substr($uri, 0, 1) !== '/' and strlen($uri) > 0) ? "/{$uri}" : $uri; |
|
57 | 57 | |
58 | - self::checkDuplicity($uri,$method); |
|
58 | + self::checkDuplicity($uri, $method); |
|
59 | 59 | |
60 | 60 | self::getInstance()->routes[] = [ |
61 | 61 | 'uri' => new Uri(self::getInstance()->host.self::getInstance()->prefix.$uri), |
@@ -74,8 +74,8 @@ discard block |
||
74 | 74 | |
75 | 75 | private static function checkDuplicity(string $uri, string $method): void |
76 | 76 | { |
77 | - foreach(self::getInstance()->routes as $route){ |
|
78 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
77 | + foreach (self::getInstance()->routes as $route) { |
|
78 | + if (md5($route['uri'].$route['method']) === md5($uri.$method)) { |
|
79 | 79 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
80 | 80 | } |
81 | 81 | } |
@@ -57,10 +57,10 @@ discard block |
||
57 | 57 | |
58 | 58 | self::checkDuplicity($uri,$method); |
59 | 59 | |
60 | - self::getInstance()->routes[] = [ |
|
61 | - 'uri' => new Uri(self::getInstance()->host.self::getInstance()->prefix.$uri), |
|
62 | - 'action' => $closure, |
|
63 | - 'method' => strtoupper($method), |
|
60 | + self::getInstance()->routes[] = [ |
|
61 | + 'uri' => new Uri(self::getInstance()->host.self::getInstance()->prefix.$uri), |
|
62 | + 'action' => $closure, |
|
63 | + 'method' => strtoupper($method), |
|
64 | 64 | 'middlewares' => null, |
65 | 65 | 'where' => null, |
66 | 66 | 'before' => [], |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | private static function checkDuplicity(string $uri, string $method): void |
76 | 76 | { |
77 | 77 | foreach(self::getInstance()->routes as $route){ |
78 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
78 | + if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
79 | 79 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
80 | 80 | } |
81 | 81 | } |
@@ -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 | } |
@@ -31,16 +31,16 @@ discard block |
||
31 | 31 | |
32 | 32 | public static function middleware($middlewares): RouterInterface |
33 | 33 | { |
34 | - $middlewares = (is_array($middlewares)) ? $middlewares : [ $middlewares ]; |
|
34 | + $middlewares = (is_array($middlewares)) ? $middlewares : [$middlewares]; |
|
35 | 35 | $route = self::getInstance()->inSave(); |
36 | - $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'],$middlewares) : $middlewares; |
|
37 | - self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->routes)); |
|
36 | + $route['middlewares'] = (is_array($route['middlewares'])) ? array_merge($route['middlewares'], $middlewares) : $middlewares; |
|
37 | + self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->routes)); |
|
38 | 38 | return self::getInstance(); |
39 | 39 | } |
40 | 40 | |
41 | 41 | private static function existMiddleware(string $name): void |
42 | 42 | { |
43 | - if(!class_exists($name) && !array_key_exists($name,self::$globalMiddlewares)){ |
|
43 | + if (!class_exists($name) && !array_key_exists($name, self::$globalMiddlewares)) { |
|
44 | 44 | throw new \RuntimeException("Middleware {$name} does not exist"); |
45 | 45 | } |
46 | 46 | } |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | |
10 | 10 | /* NOTE: in case of error an exception is thrown */ |
11 | 11 | |
12 | -try{ |
|
12 | +try { |
|
13 | 13 | |
14 | 14 | Router::globalMiddlewares([ |
15 | 15 | 'Lasted'=> \Example\Middleware\Lasted::class |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | /* Return current action route */ |
27 | 27 | $action = Router::currentAction(); |
28 | 28 | |
29 | -}catch(Exception $er){ |
|
29 | +}catch (Exception $er) { |
|
30 | 30 | |
31 | 31 | die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); |
32 | 32 |
@@ -26,7 +26,7 @@ |
||
26 | 26 | /* Return current action route */ |
27 | 27 | $action = Router::currentAction(); |
28 | 28 | |
29 | -}catch(Exception $er){ |
|
29 | +} catch(Exception $er){ |
|
30 | 30 | |
31 | 31 | die("Code Error: {$er->getCode()}, Line: {$er->getLine()}, File: {$er->getFile()}, Message: {$er->getMessage()}."); |
32 | 32 |
@@ -8,23 +8,23 @@ |
||
8 | 8 | use Psr\Http\Server\MiddlewareInterface; |
9 | 9 | use Psr\Http\Server\RequestHandlerInterface; |
10 | 10 | |
11 | -class Middleware implements MiddlewareInterface{ |
|
11 | +class Middleware implements MiddlewareInterface { |
|
12 | 12 | protected static array $data = []; |
13 | 13 | protected static Response $response; |
14 | 14 | |
15 | 15 | public function __get($key) |
16 | 16 | { |
17 | - return (array_key_exists($key,self::$data)) ? self::$data[$key] : null; |
|
17 | + return (array_key_exists($key, self::$data)) ? self::$data[$key] : null; |
|
18 | 18 | } |
19 | 19 | |
20 | - public function __set($key,$value) |
|
20 | + public function __set($key, $value) |
|
21 | 21 | { |
22 | 22 | self::$data[$key] = $value; |
23 | 23 | } |
24 | 24 | |
25 | 25 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
26 | 26 | { |
27 | - if(!isset(self::$response)){ |
|
27 | + if (!isset(self::$response)) { |
|
28 | 28 | self::$response = new Response(); |
29 | 29 | } |
30 | 30 | return self::$response; |
@@ -10,28 +10,28 @@ discard block |
||
10 | 10 | |
11 | 11 | protected function hasRouteName(string $name): void |
12 | 12 | { |
13 | - if(!isset($this->routesName[$name])){ |
|
13 | + if (!isset($this->routesName[$name])) { |
|
14 | 14 | throw new \RuntimeException("There is no route named with {$name}"); |
15 | 15 | } |
16 | 16 | } |
17 | 17 | |
18 | 18 | protected function isInNameGroup(): void |
19 | 19 | { |
20 | - if(!is_null($this->group)){ |
|
20 | + if (!is_null($this->group)) { |
|
21 | 21 | throw new \RuntimeException("It is not allowed to assign names to groups"); |
22 | 22 | } |
23 | 23 | } |
24 | 24 | |
25 | 25 | protected function isInPseudGroup(): void |
26 | 26 | { |
27 | - if(!is_null($this->group)){ |
|
27 | + if (!is_null($this->group)) { |
|
28 | 28 | throw new \RuntimeException("To assign actions before or after the execution of the route, use beforeGroup or afterGroup"); |
29 | 29 | } |
30 | 30 | } |
31 | 31 | |
32 | 32 | protected function existRouteName(string $name): void |
33 | 33 | { |
34 | - if(isset($this->routesName[$name])){ |
|
34 | + if (isset($this->routesName[$name])) { |
|
35 | 35 | throw new \RuntimeException("There is already a route named with {$name}"); |
36 | 36 | } |
37 | 37 | } |
@@ -39,12 +39,12 @@ discard block |
||
39 | 39 | protected function checkMethod(array $route, $method): void |
40 | 40 | { |
41 | 41 | $hasMethod = false; |
42 | - foreach(explode('|',$route['method']) as $routeMethod){ |
|
43 | - if(@preg_match("/{$routeMethod}/",$method) !== 0 || $method === '*'){ |
|
42 | + foreach (explode('|', $route['method']) as $routeMethod) { |
|
43 | + if (@preg_match("/{$routeMethod}/", $method) !== 0 || $method === '*') { |
|
44 | 44 | $hasMethod = true; |
45 | 45 | } |
46 | 46 | } |
47 | - if(!$hasMethod){ |
|
47 | + if (!$hasMethod) { |
|
48 | 48 | throw new \Exception('This route is not released for the accessed method'); |
49 | 49 | } |
50 | 50 |
@@ -2,7 +2,7 @@ |
||
2 | 2 | |
3 | 3 | namespace Example\Controllers; |
4 | 4 | |
5 | -class User{ |
|
5 | +class User { |
|
6 | 6 | |
7 | 7 | public function requireLogin($param, $param2): void |
8 | 8 | { |
@@ -2,7 +2,7 @@ discard block |
||
2 | 2 | |
3 | 3 | namespace HnrAzevedo\Router; |
4 | 4 | |
5 | -trait PrioritizeTrait{ |
|
5 | +trait PrioritizeTrait { |
|
6 | 6 | |
7 | 7 | protected array $routes = []; |
8 | 8 | |
@@ -11,27 +11,27 @@ discard block |
||
11 | 11 | $staticRoutes = []; |
12 | 12 | $paramRoutes = []; |
13 | 13 | |
14 | - foreach($this->routes as $r => $route){ |
|
14 | + foreach ($this->routes as $r => $route) { |
|
15 | 15 | |
16 | 16 | $path = urldecode($route['uri']->getPath()); |
17 | 17 | |
18 | - if(strstr($path,'{')){ |
|
19 | - $paramRoutes[$this->getKeyArray(substr_count($path,'/') + substr_count($path,'{'),$paramRoutes)] = $route; |
|
18 | + if (strstr($path, '{')) { |
|
19 | + $paramRoutes[$this->getKeyArray(substr_count($path, '/')+substr_count($path, '{'), $paramRoutes)] = $route; |
|
20 | 20 | continue; |
21 | 21 | } |
22 | 22 | |
23 | - $staticRoutes[$this->getKeyArray(substr_count($path,'/'),$staticRoutes)] = $route; |
|
23 | + $staticRoutes[$this->getKeyArray(substr_count($path, '/'), $staticRoutes)] = $route; |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | rsort($paramRoutes); |
27 | 27 | rsort($staticRoutes); |
28 | 28 | |
29 | - $this->orderRoutes(array_merge($staticRoutes,$paramRoutes)); |
|
29 | + $this->orderRoutes(array_merge($staticRoutes, $paramRoutes)); |
|
30 | 30 | } |
31 | 31 | |
32 | 32 | private function getKeyArray(int $index, array $array): int |
33 | 33 | { |
34 | - while(array_key_exists($index,$array)){ |
|
34 | + while (array_key_exists($index, $array)) { |
|
35 | 35 | $index++; |
36 | 36 | } |
37 | 37 | return $index; |
@@ -40,8 +40,8 @@ discard block |
||
40 | 40 | private function orderRoutes(array $routes):void |
41 | 41 | { |
42 | 42 | $kRoutes = $routes; |
43 | - foreach($routes as $r => $route){ |
|
44 | - if(array_key_exists('name',$route)){ |
|
43 | + foreach ($routes as $r => $route) { |
|
44 | + if (array_key_exists('name', $route)) { |
|
45 | 45 | unset($kRoutes[$r]); |
46 | 46 | $kRoutes["'{$route['name']}'"] = $route; |
47 | 47 | } |
@@ -11,8 +11,8 @@ discard block |
||
11 | 11 | public static function where(array $wheres): Router |
12 | 12 | { |
13 | 13 | $route = self::getInstance()->inSave(); |
14 | - $route['where'] = (is_array($route['where'])) ? array_merge($route['where'],$wheres) : $wheres; |
|
15 | - self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->routes)); |
|
14 | + $route['where'] = (is_array($route['where'])) ? array_merge($route['where'], $wheres) : $wheres; |
|
15 | + self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->routes)); |
|
16 | 16 | return self::getInstance(); |
17 | 17 | } |
18 | 18 | |
@@ -22,36 +22,36 @@ discard block |
||
22 | 22 | |
23 | 23 | $this->parameters = []; |
24 | 24 | |
25 | - $uriPath .= (substr($uriPath,strlen($uriPath)-1) !== '/') ? '/' : ''; |
|
25 | + $uriPath .= (substr($uriPath, strlen($uriPath)-1) !== '/') ? '/' : ''; |
|
26 | 26 | |
27 | - $routePath = explode('/',urldecode($route['uri']->getPath())); |
|
27 | + $routePath = explode('/', urldecode($route['uri']->getPath())); |
|
28 | 28 | unset($routePath[0]); |
29 | - $uriPath = explode('/',urldecode($uriPath)); |
|
29 | + $uriPath = explode('/', urldecode($uriPath)); |
|
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 | - if(array_key_exists(str_replace(['{:','{','}'],'',$ref),$where)){ |
|
54 | + if (array_key_exists(str_replace(['{:', '{', '}'], '', $ref), $where)) { |
|
55 | 55 | $this->matchParam($where, $ref, $value); |
56 | 56 | } |
57 | 57 | return $value; |
@@ -61,18 +61,18 @@ discard block |
||
61 | 61 | |
62 | 62 | private function checkCount(string $routePath, string $uriPath): void |
63 | 63 | { |
64 | - $countRequest = substr_count($uriPath,'/') - substr_count($routePath,'{:'); |
|
65 | - $countRoute = substr_count($routePath,'/') - substr_count($routePath,'{:'); |
|
64 | + $countRequest = substr_count($uriPath, '/')-substr_count($routePath, '{:'); |
|
65 | + $countRoute = substr_count($routePath, '/')-substr_count($routePath, '{:'); |
|
66 | 66 | |
67 | - if($countRequest !== $countRoute){ |
|
67 | + if ($countRequest !== $countRoute) { |
|
68 | 68 | throw new \Exception('continue'); |
69 | 69 | } |
70 | 70 | } |
71 | 71 | |
72 | 72 | private function matchParam(array $where, string $ref, string $value): void |
73 | 73 | { |
74 | - if(substr($ref,0,2) === '{:' || $value !== ''){ |
|
75 | - if(!preg_match("/^{$where[str_replace(['{:','{','}'],'',$ref)]}$/",$value)){ |
|
74 | + if (substr($ref, 0, 2) === '{:' || $value !== '') { |
|
75 | + if (!preg_match("/^{$where[str_replace(['{:', '{', '}'], '', $ref)]}$/", $value)) { |
|
76 | 76 | throw new \Exception('continue'); |
77 | 77 | } |
78 | 78 | } |