@@ -4,7 +4,7 @@ |
||
4 | 4 | |
5 | 5 | use HnrAzevedo\Router\Controller; |
6 | 6 | |
7 | -class User extends Controller{ |
|
7 | +class User extends Controller { |
|
8 | 8 | |
9 | 9 | public function requereLogin($username, $password): void |
10 | 10 | { |
@@ -6,11 +6,11 @@ |
||
6 | 6 | use Psr\Http\Message\ResponseInterface; |
7 | 7 | use Psr\Http\Server\RequestHandlerInterface; |
8 | 8 | |
9 | -class Auth extends Middleware{ |
|
9 | +class Auth extends Middleware { |
|
10 | 10 | |
11 | 11 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
12 | 12 | { |
13 | - if(!array_key_exists('user',$_SESSION)){ |
|
13 | + if (!array_key_exists('user', $_SESSION)) { |
|
14 | 14 | $this->error = 'The user must be logged in to the system'; |
15 | 15 | } |
16 | 16 |
@@ -7,11 +7,11 @@ |
||
7 | 7 | use Psr\Http\Message\ResponseInterface; |
8 | 8 | use Psr\Http\Server\RequestHandlerInterface; |
9 | 9 | |
10 | -class Lasted extends Middleware{ |
|
10 | +class Lasted extends Middleware { |
|
11 | 11 | |
12 | 12 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
13 | 13 | { |
14 | - if(!isset($this->error)){ |
|
14 | + if (!isset($this->error)) { |
|
15 | 15 | throw new Exception("Access not belonged: {$this->error}"); |
16 | 16 | } |
17 | 17 |
@@ -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 | } |
@@ -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) == true || $method === '*'){ |
|
42 | + foreach (explode('|', $route['method']) as $routeMethod) { |
|
43 | + if (@preg_match("/{$routeMethod}/", $method) == true || $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 |
@@ -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' => null, |
@@ -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 | } |
@@ -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 | } |
@@ -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,28 +11,28 @@ 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 | |
27 | 27 | rsort($paramRoutes); |
28 | 28 | rsort($staticRoutes); |
29 | 29 | |
30 | - $this->orderRoutes(array_merge($staticRoutes,$paramRoutes)); |
|
30 | + $this->orderRoutes(array_merge($staticRoutes, $paramRoutes)); |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | private function getKeyArray(int $index, array $array): int |
34 | 34 | { |
35 | - while(array_key_exists($index,$array)){ |
|
35 | + while (array_key_exists($index, $array)) { |
|
36 | 36 | $index++; |
37 | 37 | } |
38 | 38 | return $index; |
@@ -41,8 +41,8 @@ discard block |
||
41 | 41 | private function orderRoutes(array $routes):void |
42 | 42 | { |
43 | 43 | $kRoutes = $routes; |
44 | - foreach($routes as $r => $route){ |
|
45 | - if(array_key_exists('name',$route)){ |
|
44 | + foreach ($routes as $r => $route) { |
|
45 | + if (array_key_exists('name', $route)) { |
|
46 | 46 | unset($kRoutes[$r]); |
47 | 47 | $kRoutes[$route['name']] = $route; |
48 | 48 | } |
@@ -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 | } |
@@ -11,24 +11,24 @@ discard block |
||
11 | 11 | |
12 | 12 | public static function before($closure): RouterInterface |
13 | 13 | { |
14 | - return self::addInRoute('before',$closure); |
|
14 | + return self::addInRoute('before', $closure); |
|
15 | 15 | } |
16 | 16 | |
17 | 17 | public static function after($closure): RouterInterface |
18 | 18 | { |
19 | - return self::addInRoute('after',$closure); |
|
19 | + return self::addInRoute('after', $closure); |
|
20 | 20 | } |
21 | 21 | |
22 | 22 | public static function beforeAll($closure, $excepts): RouterInterface |
23 | 23 | { |
24 | - self::getInstance()->beforeExcepts = (is_array($excepts)) ? $excepts : [ $excepts ]; |
|
24 | + self::getInstance()->beforeExcepts = (is_array($excepts)) ? $excepts : [$excepts]; |
|
25 | 25 | self::getInstance()->beforeAll = $closure; |
26 | 26 | return self::getInstance(); |
27 | 27 | } |
28 | 28 | |
29 | 29 | public static function afterAll($closure, $excepts): RouterInterface |
30 | 30 | { |
31 | - self::getInstance()->afterExcepts = (is_array($excepts)) ? $excepts : [ $excepts ]; |
|
31 | + self::getInstance()->afterExcepts = (is_array($excepts)) ? $excepts : [$excepts]; |
|
32 | 32 | self::getInstance()->afterAll = $closure; |
33 | 33 | return self::getInstance(); |
34 | 34 | } |
@@ -46,12 +46,12 @@ discard block |
||
46 | 46 | private static function addInRoutes(string $state, $closure, $excepts): RouterInterface |
47 | 47 | { |
48 | 48 | self::getInstance()->isInPseudGroup(); |
49 | - $excepts = (is_array($excepts)) ? $excepts : [ $excepts ]; |
|
49 | + $excepts = (is_array($excepts)) ? $excepts : [$excepts]; |
|
50 | 50 | $group = self::getInstance()->inSave()['group']; |
51 | 51 | |
52 | - foreach(self::getInstance()->routes as $r => $route){ |
|
53 | - if($route['group'] === $group && !in_array($r,$excepts)){ |
|
54 | - self::getInstance()->routes[$r][$state] = (is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
52 | + foreach (self::getInstance()->routes as $r => $route) { |
|
53 | + if ($route['group'] === $group && !in_array($r, $excepts)) { |
|
54 | + self::getInstance()->routes[$r][$state] = (is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
55 | 55 | } |
56 | 56 | } |
57 | 57 | |
@@ -61,9 +61,9 @@ discard block |
||
61 | 61 | private static function addInRoute(string $state, $closure): RouterInterface |
62 | 62 | { |
63 | 63 | $route = self::getInstance()->inSave(); |
64 | - $state = (!is_null($route[$state])) ? [ $closure ] : array_merge($route[$state], [ $closure ]); |
|
64 | + $state = (!is_null($route[$state])) ? [$closure] : array_merge($route[$state], [$closure]); |
|
65 | 65 | $route[$state] = $state; |
66 | - self::updateRoute($route,array_key_last(self::getInstance()->routes)); |
|
66 | + self::updateRoute($route, array_key_last(self::getInstance()->routes)); |
|
67 | 67 | return self::getInstance(); |
68 | 68 | } |
69 | 69 |