@@ -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; |
@@ -3,30 +3,30 @@ discard block |
||
3 | 3 | use HnrAzevedo\Router\Router; |
4 | 4 | |
5 | 5 | /* Returning parameters passed via URL in anonymous functions */ |
6 | -Router::match('GET|POST|AJAX','/{parameter}/{otherparameter}', function($parameter, $otherparameter){ |
|
6 | +Router::match('GET|POST|AJAX', '/{parameter}/{otherparameter}', function($parameter, $otherparameter) { |
|
7 | 7 | //echo "Parameter 1:{$parameter}, Parameter 2:{$otherparameter}."; |
8 | 8 | }); |
9 | 9 | |
10 | 10 | /* Passing controller and/or method via parameter in URL */ |
11 | -Router::get('/{controller}/{method}','{controller}:{method}'); |
|
11 | +Router::get('/{controller}/{method}', '{controller}:{method}'); |
|
12 | 12 | //Router::get('/{controller}/{method}','{controller}:method'); |
13 | 13 | |
14 | 14 | |
15 | -Router::get('/my-account','Controller\\User:my_account'); |
|
15 | +Router::get('/my-account', 'Controller\\User:my_account'); |
|
16 | 16 | |
17 | 17 | /* Passing value via parameter */ |
18 | -Router::get('/my-account/teste/teste','Controller\\User:my_account')->where([ |
|
18 | +Router::get('/my-account/teste/teste', 'Controller\\User:my_account')->where([ |
|
19 | 19 | 'teste'=>'[a-zA-Z]{1,10}', |
20 | 20 | 'teste2' => '[0-9]{1}' |
21 | 21 | ]); |
22 | 22 | |
23 | -Router::get('/my-account/{:teste}/{teste2}','Controller\\User:my_account')->where([ |
|
23 | +Router::get('/my-account/{:teste}/{teste2}', 'Controller\\User:my_account')->where([ |
|
24 | 24 | 'teste'=>'[a-zA-Z]{1,10}', |
25 | 25 | 'teste2'=>'[a-zA-Z]{1,10}', |
26 | 26 | //'teste2' => '[0-9]{1}' |
27 | 27 | ]); |
28 | 28 | |
29 | -Router::get('/my-account/{:teste}','Controller\\User:my_account')->where([ |
|
29 | +Router::get('/my-account/{:teste}', 'Controller\\User:my_account')->where([ |
|
30 | 30 | 'teste'=>'[a-zA-Z]{1,10}' |
31 | 31 | ]); |
32 | 32 | |
@@ -35,11 +35,11 @@ discard block |
||
35 | 35 | |
36 | 36 | |
37 | 37 | |
38 | -Router::get('/my-account1',function(){ |
|
38 | +Router::get('/my-account1', function() { |
|
39 | 39 | echo 'is Ok!'; |
40 | -})->middleware(['\Example\Middleware\Auth::class','Lasted']); |
|
40 | +})->middleware(['\Example\Middleware\Auth::class', 'Lasted']); |
|
41 | 41 | |
42 | 42 | /* Accessed by all protocols */ |
43 | -Router::any('/',function(){ |
|
43 | +Router::any('/', function() { |
|
44 | 44 | // |
45 | 45 | })->name('index'); |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | $route = self::getInstance()->inSave(); |
34 | 34 | $route['name'] = $name; |
35 | 35 | self::getInstance()->routesNames[$name] = $name; |
36 | - self::getInstance()->unsetRoute(count(self::getInstance()->routes)-1)->updateRoute($route,$name); |
|
36 | + self::getInstance()->unsetRoute(count(self::getInstance()->routes)-1)->updateRoute($route, $name); |
|
37 | 37 | return self::getInstance(); |
38 | 38 | } |
39 | 39 | |
@@ -73,15 +73,15 @@ discard block |
||
73 | 73 | |
74 | 74 | self::getInstance()->sortRoutes(); |
75 | 75 | |
76 | - foreach(self::getInstance()->routes as $r => $route){ |
|
76 | + foreach (self::getInstance()->routes as $r => $route) { |
|
77 | 77 | self::getInstance()->currentRoute = $route; |
78 | 78 | self::getInstance()->currentRoute['name'] = $r; |
79 | 79 | |
80 | - try{ |
|
80 | + try { |
|
81 | 81 | self::getInstance()->checkMethod($route, $_SERVER['REQUEST_METHOD']); |
82 | 82 | self::getInstance()->checkData($route, $_SERVER['REQUEST_URI']); |
83 | 83 | return self::getInstance(); |
84 | - }catch(\Exception $er){ |
|
84 | + }catch (\Exception $er) { |
|
85 | 85 | continue; |
86 | 86 | } |
87 | 87 | } |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | |
93 | 93 | public static function run(): RouterInterface |
94 | 94 | { |
95 | - if(!self::getInstance()->loaded){ |
|
95 | + if (!self::getInstance()->loaded) { |
|
96 | 96 | self::getInstance()->load(); |
97 | 97 | } |
98 | 98 |
@@ -81,7 +81,7 @@ |
||
81 | 81 | self::getInstance()->checkMethod($route, $_SERVER['REQUEST_METHOD']); |
82 | 82 | self::getInstance()->checkData($route, $_SERVER['REQUEST_URI']); |
83 | 83 | return self::getInstance(); |
84 | - }catch(\Exception $er){ |
|
84 | + } catch(\Exception $er){ |
|
85 | 85 | continue; |
86 | 86 | } |
87 | 87 | } |
@@ -9,8 +9,8 @@ discard block |
||
9 | 9 | public static function where(array $wheres): Router |
10 | 10 | { |
11 | 11 | $route = self::getInstance()->inSave(); |
12 | - $route['where'] = (is_array($route['where'])) ? array_merge($route['where'],$wheres) : $wheres; |
|
13 | - self::getInstance()->updateRoute($route,array_key_last(self::getInstance()->routes)); |
|
12 | + $route['where'] = (is_array($route['where'])) ? array_merge($route['where'], $wheres) : $wheres; |
|
13 | + self::getInstance()->updateRoute($route, array_key_last(self::getInstance()->routes)); |
|
14 | 14 | return self::getInstance(); |
15 | 15 | } |
16 | 16 | |
@@ -18,32 +18,32 @@ discard block |
||
18 | 18 | { |
19 | 19 | $this->checkCount($route['uri']->getPath(), $uriPath); |
20 | 20 | |
21 | - $uriPath .= (substr($uriPath,strlen($uriPath)-1) !== '/') ? '/' : ''; |
|
21 | + $uriPath .= (substr($uriPath, strlen($uriPath)-1) !== '/') ? '/' : ''; |
|
22 | 22 | |
23 | - $routePath = explode('/',urldecode($route['uri']->getPath())); |
|
23 | + $routePath = explode('/', urldecode($route['uri']->getPath())); |
|
24 | 24 | unset($routePath[0]); |
25 | - $uriPath = explode('/',urldecode($uriPath)); |
|
25 | + $uriPath = explode('/', urldecode($uriPath)); |
|
26 | 26 | unset($uriPath[0]); |
27 | 27 | |
28 | 28 | $corretRoute = true; |
29 | - foreach($routePath as $r => $routeFrag){ |
|
29 | + foreach ($routePath as $r => $routeFrag) { |
|
30 | 30 | $where = is_array($route['where']) ? $route['where'] : []; |
31 | 31 | $routeFrag = $this->replaceParam($where, $routeFrag, $uriPath[$r]); |
32 | 32 | |
33 | - if($routeFrag !== $uriPath[$r]){ |
|
33 | + if ($routeFrag !== $uriPath[$r]) { |
|
34 | 34 | $corretRoute = false; |
35 | 35 | } |
36 | 36 | } |
37 | 37 | |
38 | - if(!$corretRoute){ |
|
38 | + if (!$corretRoute) { |
|
39 | 39 | throw new \Exception('continue'); |
40 | 40 | } |
41 | 41 | } |
42 | 42 | |
43 | 43 | private function replaceParam(array $where, string $ref, string $value): string |
44 | 44 | { |
45 | - if(((substr($ref,0,1) === '{') && (substr($ref,strlen($ref)-1) === '}'))) { |
|
46 | - if(array_key_exists(str_replace(['{','}',':'],'',$ref),$where)){ |
|
45 | + if (((substr($ref, 0, 1) === '{') && (substr($ref, strlen($ref)-1) === '}'))) { |
|
46 | + if (array_key_exists(str_replace(['{', '}', ':'], '', $ref), $where)) { |
|
47 | 47 | $this->matchParam($where, $ref, $value); |
48 | 48 | } |
49 | 49 | return $value; |
@@ -53,18 +53,18 @@ discard block |
||
53 | 53 | |
54 | 54 | private function checkCount(string $routePath, string $uriPath): void |
55 | 55 | { |
56 | - $countRequest = substr_count($uriPath,'/') - substr_count($routePath,'{:'); |
|
57 | - $countRoute = substr_count($routePath,'/') - substr_count($routePath,'{:'); |
|
56 | + $countRequest = substr_count($uriPath, '/')-substr_count($routePath, '{:'); |
|
57 | + $countRoute = substr_count($routePath, '/')-substr_count($routePath, '{:'); |
|
58 | 58 | |
59 | - if($countRequest !== $countRoute){ |
|
59 | + if ($countRequest !== $countRoute) { |
|
60 | 60 | throw new \Exception('continue'); |
61 | 61 | } |
62 | 62 | } |
63 | 63 | |
64 | 64 | private function matchParam(array $where, string $ref, string $value): void |
65 | 65 | { |
66 | - if(substr($ref,0,2) === '{:' || $value !== ''){ |
|
67 | - if(!preg_match("/^{$where[str_replace(['{','}',':'],'',$ref)]}$/",$value)){ |
|
66 | + if (substr($ref, 0, 2) === '{:' || $value !== '') { |
|
67 | + if (!preg_match("/^{$where[str_replace(['{', '}', ':'], '', $ref)]}$/", $value)) { |
|
68 | 68 | throw new \Exception('continue'); |
69 | 69 | } |
70 | 70 | } |