@@ -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 | } |
@@ -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 | { |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | self::getInstance()->checkMethod($route, $_SERVER['REQUEST_METHOD']); |
80 | 80 | self::getInstance()->checkData($route, (new Uri($_SERVER['REQUEST_URI']))->getPath()); |
81 | 81 | return self::getInstance(); |
82 | - }catch(\Exception $er){ |
|
82 | + } catch(\Exception $er){ |
|
83 | 83 | continue; |
84 | 84 | } |
85 | 85 | } |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | |
99 | 99 | try{ |
100 | 100 | self::getInstance()->executeRouteAction(); |
101 | - }catch(\Exception $er){ |
|
101 | + } catch(\Exception $er){ |
|
102 | 102 | self::getInstance()->error = $er; |
103 | 103 | } |
104 | 104 |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | $route = self::getInstance()->inSave(); |
26 | 26 | $route['name'] = $name; |
27 | 27 | self::getInstance()->routesName[$name] = $name; |
28 | - self::getInstance()->unsetRoute(array_key_last(self::getInstance()->getRoutes()))->updateRoute($route,$name); |
|
28 | + self::getInstance()->unsetRoute(array_key_last(self::getInstance()->getRoutes()))->updateRoute($route, $name); |
|
29 | 29 | return self::getInstance(); |
30 | 30 | } |
31 | 31 | |
@@ -64,15 +64,15 @@ discard block |
||
64 | 64 | self::getInstance()->loaded = true; |
65 | 65 | self::getInstance()->sortRoutes(); |
66 | 66 | |
67 | - foreach(self::getInstance()->getRoutes() as $r => $route){ |
|
67 | + foreach (self::getInstance()->getRoutes() as $r => $route) { |
|
68 | 68 | self::getInstance()->currentRoute = $route; |
69 | 69 | self::getInstance()->currentRoute['name'] = $r; |
70 | 70 | |
71 | - try{ |
|
71 | + try { |
|
72 | 72 | self::getInstance()->checkMethod($route, $_SERVER['REQUEST_METHOD']); |
73 | 73 | self::getInstance()->checkData($route, (new Uri($_SERVER['REQUEST_URI']))->getPath()); |
74 | 74 | return self::getInstance(); |
75 | - }catch(\Exception $er){ |
|
75 | + }catch (\Exception $er) { |
|
76 | 76 | continue; |
77 | 77 | } |
78 | 78 | } |
@@ -83,15 +83,15 @@ discard block |
||
83 | 83 | |
84 | 84 | public static function run(): RouterInterface |
85 | 85 | { |
86 | - if(!self::getInstance()->loaded){ |
|
86 | + if (!self::getInstance()->loaded) { |
|
87 | 87 | self::getInstance()->load(); |
88 | 88 | } |
89 | 89 | |
90 | 90 | self::getInstance()->executeBefore(); |
91 | 91 | |
92 | - try{ |
|
92 | + try { |
|
93 | 93 | self::getInstance()->executeRouteAction(); |
94 | - }catch(\Exception $er){ |
|
94 | + }catch (\Exception $er) { |
|
95 | 95 | self::getInstance()->error = $er; |
96 | 96 | } |
97 | 97 | |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | |
105 | 105 | private function checkError(): void |
106 | 106 | { |
107 | - if(isset($this->error)){ |
|
107 | + if (isset($this->error)) { |
|
108 | 108 | throw $this->error; |
109 | 109 | } |
110 | 110 | } |
@@ -8,8 +8,8 @@ |
||
8 | 8 | use Psr\Http\Server\RequestHandlerInterface; |
9 | 9 | |
10 | 10 | /** |
11 | - * @property string $error |
|
12 | - */ |
|
11 | + * @property string $error |
|
12 | + */ |
|
13 | 13 | class Lasted extends Middleware{ |
14 | 14 | |
15 | 15 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
@@ -10,11 +10,11 @@ |
||
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 | - if(!isset($this->error)){ |
|
17 | + if (!isset($this->error)) { |
|
18 | 18 | throw new Exception("Access not belonged: {$this->error}"); |
19 | 19 | } |
20 | 20 |
@@ -7,8 +7,8 @@ |
||
7 | 7 | use Psr\Http\Server\RequestHandlerInterface; |
8 | 8 | |
9 | 9 | /** |
10 | - * @property string $error |
|
11 | - */ |
|
10 | + * @property string $error |
|
11 | + */ |
|
12 | 12 | class Auth extends Middleware{ |
13 | 13 | |
14 | 14 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
@@ -9,11 +9,11 @@ |
||
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 | - if(!array_key_exists('user',$_SESSION)){ |
|
16 | + if (!array_key_exists('user', $_SESSION)) { |
|
17 | 17 | $this->error = 'The user must be logged in to the system'; |
18 | 18 | } |
19 | 19 |
@@ -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}','\Example\Controllers\User@requireLogin'); |
|
36 | +Router::get('/6my-accounttttt/{param1}/{param2}', '\Example\Controllers\User@requireLogin'); |
|
37 | 37 | //->middleware(['\Example\Middleware\Auth::class','Lasted']); |
38 | 38 | |
39 | -Router::get('/5my-account/{:teste}','Controller\\User:my_account')->where([ |
|
39 | +Router::get('/5my-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'); |