@@ -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; |
@@ -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'); |
@@ -10,32 +10,32 @@ discard block |
||
10 | 10 | |
11 | 11 | public static function get(string $uri, $closure): RouterInterface |
12 | 12 | { |
13 | - return self::set('get',$uri,$closure); |
|
13 | + return self::set('get', $uri, $closure); |
|
14 | 14 | } |
15 | 15 | |
16 | 16 | public static function post(string $uri, $closure): RouterInterface |
17 | 17 | { |
18 | - return self::set('post',$uri,$closure); |
|
18 | + return self::set('post', $uri, $closure); |
|
19 | 19 | } |
20 | 20 | |
21 | 21 | public static function ajax(string $uri, $closure): RouterInterface |
22 | 22 | { |
23 | - return self::set('ajax',$uri,$closure); |
|
23 | + return self::set('ajax', $uri, $closure); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | public static function delete(string $uri, $closure): RouterInterface |
27 | 27 | { |
28 | - return self::set('delete',$uri,$closure); |
|
28 | + return self::set('delete', $uri, $closure); |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | public static function put(string $uri, $closure): RouterInterface |
32 | 32 | { |
33 | - return self::set('put',$uri,$closure); |
|
33 | + return self::set('put', $uri, $closure); |
|
34 | 34 | } |
35 | 35 | |
36 | 36 | public static function patch(string $uri, $closure): RouterInterface |
37 | 37 | { |
38 | - return self::set('patch',$uri,$closure); |
|
38 | + return self::set('patch', $uri, $closure); |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | public static function match(string $method, string $uri, $closure): RouterInterface |
@@ -46,14 +46,14 @@ discard block |
||
46 | 46 | |
47 | 47 | public static function any(string $uri, $closure): RouterInterface |
48 | 48 | { |
49 | - return self::set('*',$uri,$closure); |
|
49 | + return self::set('*', $uri, $closure); |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | private static function set(string $method, string $uri, $closure): RouterInterface |
53 | 53 | { |
54 | - $uri = (substr($uri,0,1) !=='/' and strlen($uri) > 0) ? "/{$uri}" : $uri; |
|
54 | + $uri = (substr($uri, 0, 1) !== '/' and strlen($uri) > 0) ? "/{$uri}" : $uri; |
|
55 | 55 | |
56 | - self::checkDuplicity($uri,$method); |
|
56 | + self::checkDuplicity($uri, $method); |
|
57 | 57 | |
58 | 58 | $routes = self::getInstance()->getRoutes(); |
59 | 59 | $routes[] = [ |
@@ -75,8 +75,8 @@ discard block |
||
75 | 75 | |
76 | 76 | private static function checkDuplicity(string $uri, string $method): void |
77 | 77 | { |
78 | - foreach(self::getInstance()->getRoutes() as $route){ |
|
79 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
78 | + foreach (self::getInstance()->getRoutes() as $route) { |
|
79 | + if (md5($route['uri'].$route['method']) === md5($uri.$method)) { |
|
80 | 80 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
81 | 81 | } |
82 | 82 | } |
@@ -57,9 +57,9 @@ discard block |
||
57 | 57 | |
58 | 58 | $routes = self::getInstance()->getRoutes(); |
59 | 59 | $routes[] = [ |
60 | - 'uri' => new Uri(self::getInstance()->getHost().self::getInstance()->getPrefix().$uri), |
|
61 | - 'action' => $closure, |
|
62 | - 'method' => strtoupper($method), |
|
60 | + 'uri' => new Uri(self::getInstance()->getHost().self::getInstance()->getPrefix().$uri), |
|
61 | + 'action' => $closure, |
|
62 | + 'method' => strtoupper($method), |
|
63 | 63 | 'middlewares' => null, |
64 | 64 | 'where' => null, |
65 | 65 | 'before' => [], |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | private static function checkDuplicity(string $uri, string $method): void |
77 | 77 | { |
78 | 78 | foreach(self::getInstance()->getRoutes() as $route){ |
79 | - if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
79 | + if( md5($route['uri'].$route['method']) === md5($uri.$method) ){ |
|
80 | 80 | throw new \RuntimeException("There is already a route with the URI {$uri} and with the {$method} METHOD configured."); |
81 | 81 | } |
82 | 82 | } |