@@ -7,20 +7,20 @@ discard block |
||
7 | 7 | use HnrAzevedo\Http\Uri; |
8 | 8 | use HnrAzevedo\Http\Factory; |
9 | 9 | |
10 | -trait MiddlewareTrait{ |
|
10 | +trait MiddlewareTrait { |
|
11 | 11 | protected array $middlewares = []; |
12 | 12 | protected RequestHandler $request; |
13 | 13 | protected ServerRequest $serverRequest; |
14 | 14 | |
15 | 15 | protected function executeMiddleware(array $route) |
16 | 16 | { |
17 | - $this->request = new RequestHandler($route['protocol'],new Uri($this->host.$route['url'])); |
|
18 | - $this->serverRequest = (new Factory())->createServerRequest($route['protocol'],new Uri($this->host.$route['url'])); |
|
17 | + $this->request = new RequestHandler($route['protocol'], new Uri($this->host.$route['url'])); |
|
18 | + $this->serverRequest = (new Factory())->createServerRequest($route['protocol'], new Uri($this->host.$route['url'])); |
|
19 | 19 | |
20 | - $middlewares = (is_array($route['middlewares'])) ? $route['middlewares'] : [ $route['middlewares'] ]; |
|
20 | + $middlewares = (is_array($route['middlewares'])) ? $route['middlewares'] : [$route['middlewares']]; |
|
21 | 21 | |
22 | - foreach($middlewares as $middleware){ |
|
23 | - if(is_null($middleware)){ |
|
22 | + foreach ($middlewares as $middleware) { |
|
23 | + if (is_null($middleware)) { |
|
24 | 24 | continue; |
25 | 25 | } |
26 | 26 | |
@@ -38,12 +38,12 @@ discard block |
||
38 | 38 | |
39 | 39 | protected function middlewareExists(string $m) |
40 | 40 | { |
41 | - if(class_exists(str_replace('::class','',$m))){ |
|
42 | - $m = str_replace('::class','',$m); |
|
41 | + if (class_exists(str_replace('::class', '', $m))) { |
|
42 | + $m = str_replace('::class', '', $m); |
|
43 | 43 | return new $m(); |
44 | 44 | } |
45 | 45 | |
46 | - if(array_key_exists($m,$this->middlewares)){ |
|
46 | + if (array_key_exists($m, $this->middlewares)) { |
|
47 | 47 | return new $this->middlewares[$m](); |
48 | 48 | } |
49 | 49 |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | namespace HnrAzevedo\Router; |
4 | 4 | |
5 | 5 | |
6 | -class Router{ |
|
6 | +class Router { |
|
7 | 7 | use Helper, DefinitionsTrait, ExtraJobsTrait; |
8 | 8 | |
9 | 9 | private static Router $instance; |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | |
23 | 23 | public static function create(): Router |
24 | 24 | { |
25 | - if(!self::getInstance()->instanced){ |
|
25 | + if (!self::getInstance()->instanced) { |
|
26 | 26 | self::getInstance()->instanced = true; |
27 | 27 | } |
28 | 28 | |
@@ -41,13 +41,13 @@ discard block |
||
41 | 41 | return self::getInstance(); |
42 | 42 | } |
43 | 43 | |
44 | - public function set(string $url ,$walking , string $protocol): Router |
|
44 | + public function set(string $url, $walking, string $protocol): Router |
|
45 | 45 | { |
46 | 46 | $this->lastReturn = null; |
47 | 47 | |
48 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
48 | + $url = (substr($url, 0, 1) !== '/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
49 | 49 | |
50 | - $this->checkExistence($url,$protocol); |
|
50 | + $this->checkExistence($url, $protocol); |
|
51 | 51 | $this->checkTypeRole($walking); |
52 | 52 | |
53 | 53 | $route = [ |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | |
71 | 71 | public static function group(string $prefix, callable $callback): Router |
72 | 72 | { |
73 | - self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix; |
|
73 | + self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix; |
|
74 | 74 | self::getInstance()->group = sha1(date('d/m/Y h:m:i')); |
75 | 75 | $callback(); |
76 | 76 | self::getInstance()->group = null; |
@@ -90,11 +90,11 @@ discard block |
||
90 | 90 | |
91 | 91 | $currentRoute = end(self::getInstance()->routers); |
92 | 92 | |
93 | - if(in_array($name,self::getInstance()->beforeExcepts)){ |
|
93 | + if (in_array($name, self::getInstance()->beforeExcepts)) { |
|
94 | 94 | $currentRoute['beforeAll'] = null; |
95 | 95 | } |
96 | 96 | |
97 | - if(in_array($name,self::getInstance()->afterExcepts)){ |
|
97 | + if (in_array($name, self::getInstance()->afterExcepts)) { |
|
98 | 98 | $currentRoute['afterAll'] = null; |
99 | 99 | } |
100 | 100 | |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | { |
115 | 115 | $instance = self::create()->getInstance(); |
116 | 116 | |
117 | - if(!$instance->loaded){ |
|
117 | + if (!$instance->loaded) { |
|
118 | 118 | self::load($routeName); |
119 | 119 | } |
120 | 120 | |
@@ -124,36 +124,36 @@ discard block |
||
124 | 124 | |
125 | 125 | public function middleware($middlewares): Router |
126 | 126 | { |
127 | - if($this->lastReturn !== null){ |
|
127 | + if ($this->lastReturn !== null) { |
|
128 | 128 | $currentGroup = end($this->routers)['group']; |
129 | 129 | |
130 | 130 | foreach ($this->routers as $key => $value) { |
131 | - if($value['group'] === $currentGroup){ |
|
132 | - $this->routers[$key] = $this->addMiddleware($this->routers[$key],$middlewares); |
|
131 | + if ($value['group'] === $currentGroup) { |
|
132 | + $this->routers[$key] = $this->addMiddleware($this->routers[$key], $middlewares); |
|
133 | 133 | } |
134 | 134 | } |
135 | 135 | |
136 | 136 | return $this; |
137 | 137 | } |
138 | 138 | |
139 | - $this->routers[count($this->routers)-1] = $this->addMiddleware(end($this->routers),$middlewares); |
|
139 | + $this->routers[count($this->routers)-1] = $this->addMiddleware(end($this->routers), $middlewares); |
|
140 | 140 | return $this; |
141 | 141 | } |
142 | 142 | |
143 | 143 | public static function addMiddleware(array $route, $filter): array |
144 | 144 | { |
145 | - if(is_null($route['middlewares'])){ |
|
145 | + if (is_null($route['middlewares'])) { |
|
146 | 146 | $route['middlewares'] = $filter; |
147 | 147 | return $route; |
148 | 148 | } |
149 | 149 | |
150 | 150 | $middlewares = (is_array($filter)) ? $filter : [0 => $filter]; |
151 | 151 | |
152 | - if(is_array($route['middlewares'])){ |
|
152 | + if (is_array($route['middlewares'])) { |
|
153 | 153 | foreach ($route['middlewares'] as $key => $value) { |
154 | 154 | $middlewares[] = $value; |
155 | 155 | } |
156 | - }else{ |
|
156 | + }else { |
|
157 | 157 | $middlewares[] = $route['middlewares']; |
158 | 158 | } |
159 | 159 |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | |
6 | 6 | use Exception; |
7 | 7 | |
8 | -trait Helper{ |
|
8 | +trait Helper { |
|
9 | 9 | use CheckTrait, ControllerTrait; |
10 | 10 | |
11 | 11 | private $currentRoute = null; |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | protected function getProtocol(): string |
33 | 33 | { |
34 | 34 | $protocol = ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) ? 'ajax' : 'get'; |
35 | - $protocol = (array_key_existS('HTTP_REQUESTED_METHOD',$_SERVER)) ? strtolower($_SERVER['HTTP_REQUESTED_METHOD']) : $protocol; |
|
35 | + $protocol = (array_key_existS('HTTP_REQUESTED_METHOD', $_SERVER)) ? strtolower($_SERVER['HTTP_REQUESTED_METHOD']) : $protocol; |
|
36 | 36 | |
37 | 37 | return $protocol; |
38 | 38 | } |
@@ -51,9 +51,9 @@ discard block |
||
51 | 51 | { |
52 | 52 | $this->checkRole(); |
53 | 53 | $method = ($method !== 'method') ? $method : $this->getData()['POST']['role']; |
54 | - $data = (array_key_exists('data',$values)) ? json_decode($values['data'], true) : []; |
|
54 | + $data = (array_key_exists('data', $values)) ? json_decode($values['data'], true) : []; |
|
55 | 55 | |
56 | - call_user_func_array([$controller,$method], $data); |
|
56 | + call_user_func_array([$controller, $method], $data); |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | protected function Controller(string $controll) |
@@ -61,36 +61,36 @@ discard block |
||
61 | 61 | $data = $this->getData(); |
62 | 62 | |
63 | 63 | foreach ($data['GET'] as $name => $value) { |
64 | - $controll = str_replace('{'.$name.'}',$value,$controll); |
|
64 | + $controll = str_replace('{'.$name.'}', $value, $controll); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | $this->checkControllSettable($controll)->checkControllExist($controll)->checkControllMethod($controll); |
68 | 68 | |
69 | - $controller = ucfirst(explode(':',$controll)[0]); |
|
69 | + $controller = ucfirst(explode(':', $controll)[0]); |
|
70 | 70 | $controller = new $controller(); |
71 | - $method = explode(':',$controll)[1]; |
|
71 | + $method = explode(':', $controll)[1]; |
|
72 | 72 | |
73 | - if( ($this->getProtocol() == 'form') ){ |
|
73 | + if (($this->getProtocol() == 'form')) { |
|
74 | 74 | $this->ControllerForm($controller, $method, $data['POST']); |
75 | 75 | }else { |
76 | - $data = (array_key_exists('data',$data['POST'])) ? json_decode($data['POST']['data'], true) : $data['GET']; |
|
77 | - call_user_func_array([$controller,$method], $data); |
|
76 | + $data = (array_key_exists('data', $data['POST'])) ? json_decode($data['POST']['data'], true) : $data['GET']; |
|
77 | + call_user_func_array([$controller, $method], $data); |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | return $this; |
81 | 81 | } |
82 | 82 | |
83 | - protected function explodeRoutes(bool $bar, string $url ,bool $bar_, string $url_): array |
|
83 | + protected function explodeRoutes(bool $bar, string $url, bool $bar_, string $url_): array |
|
84 | 84 | { |
85 | - $url = $bar ? substr($url, 0, -1) : $url ; |
|
86 | - $url = explode('/',$url); |
|
85 | + $url = $bar ? substr($url, 0, -1) : $url; |
|
86 | + $url = explode('/', $url); |
|
87 | 87 | |
88 | - $url_ = $bar_ ? substr($url_, 0, -1) : $url_ ; |
|
89 | - $url_ = explode('/',$url_); |
|
88 | + $url_ = $bar_ ? substr($url_, 0, -1) : $url_; |
|
89 | + $url_ = explode('/', $url_); |
|
90 | 90 | |
91 | - foreach($url as $ur => $u){ |
|
92 | - if(substr($u,0,2) === '{?'){ |
|
93 | - if(!array_key_exists($ur,$url_)){ |
|
91 | + foreach ($url as $ur => $u) { |
|
92 | + if (substr($u, 0, 2) === '{?') { |
|
93 | + if (!array_key_exists($ur, $url_)) { |
|
94 | 94 | $url_[$ur] = ''; |
95 | 95 | }; |
96 | 96 | } |
@@ -101,25 +101,25 @@ discard block |
||
101 | 101 | |
102 | 102 | protected function run(array $route): bool |
103 | 103 | { |
104 | - $this->callOnRoute($route,'beforeAll')->callOnRoute($route,'before'); |
|
104 | + $this->callOnRoute($route, 'beforeAll')->callOnRoute($route, 'before'); |
|
105 | 105 | |
106 | - if(is_string($route['role'])){ |
|
107 | - $this->Controller($route['role'])->callOnRoute($route,'after')->callOnRoute($route,'afterAll'); |
|
106 | + if (is_string($route['role'])) { |
|
107 | + $this->Controller($route['role'])->callOnRoute($route, 'after')->callOnRoute($route, 'afterAll'); |
|
108 | 108 | return true; |
109 | 109 | } |
110 | 110 | |
111 | - call_user_func_array($route['role'],[$this->getData()['GET']]); |
|
111 | + call_user_func_array($route['role'], [$this->getData()['GET']]); |
|
112 | 112 | |
113 | - $this->callOnRoute($route,'after')->callOnRoute($route,'afterAll'); |
|
113 | + $this->callOnRoute($route, 'after')->callOnRoute($route, 'afterAll'); |
|
114 | 114 | return true; |
115 | 115 | } |
116 | 116 | |
117 | - protected function callOnRoute(array $route,string $state) |
|
117 | + protected function callOnRoute(array $route, string $state) |
|
118 | 118 | { |
119 | - if($route[$state] !== null){ |
|
120 | - if(is_string($route[$state])){ |
|
119 | + if ($route[$state] !== null) { |
|
120 | + if (is_string($route[$state])) { |
|
121 | 121 | $this->Controller($route[$state]); |
122 | - }else{ |
|
122 | + }else { |
|
123 | 123 | $route[$state](); |
124 | 124 | } |
125 | 125 | } |
@@ -130,12 +130,12 @@ discard block |
||
130 | 130 | { |
131 | 131 | $currentProtocol = $this->getProtocol(); |
132 | 132 | |
133 | - foreach(array_reverse($this->routers) as $r => $route){ |
|
133 | + foreach (array_reverse($this->routers) as $r => $route) { |
|
134 | 134 | |
135 | 135 | $this->currentRoute = $route; |
136 | 136 | $this->currentRoute['name'] = $r; |
137 | 137 | |
138 | - if(!$this->checkProtocol($route['protocol'], $currentProtocol)){ |
|
138 | + if (!$this->checkProtocol($route['protocol'], $currentProtocol)) { |
|
139 | 139 | continue; |
140 | 140 | } |
141 | 141 | |
@@ -145,11 +145,11 @@ discard block |
||
145 | 145 | $_SERVER['REQUEST_URI'] = (array_key_exists('REQUEST_URI', $_SERVER)) ? $_SERVER['REQUEST_URI'] : ''; |
146 | 146 | |
147 | 147 | $routs = $this->explodeRoutes( |
148 | - (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url'], |
|
149 | - (substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI'] |
|
148 | + (substr($route['url'], strlen($route['url'])-1, 1) === '/'), $route['url'], |
|
149 | + (substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/'), $_SERVER['REQUEST_URI'] |
|
150 | 150 | ); |
151 | 151 | |
152 | - if(!$this->checkToHiking($route, $routs['routeRequest'], $routs['routeLoop'])){ |
|
152 | + if (!$this->checkToHiking($route, $routs['routeRequest'], $routs['routeLoop'])) { |
|
153 | 153 | continue; |
154 | 154 | } |
155 | 155 | |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | } |
159 | 159 | |
160 | 160 | $this->currentRoute = null; |
161 | - throw new Exception('Page not found.',404); |
|
161 | + throw new Exception('Page not found.', 404); |
|
162 | 162 | } |
163 | 163 | |
164 | 164 | protected function loadByName(string $routName) |
@@ -167,8 +167,8 @@ discard block |
||
167 | 167 | $this->checkName($routName); |
168 | 168 | $route = $this->routers[$routName]; |
169 | 169 | |
170 | - if(!$this->checkProtocol($route['protocol'], $currentProtocol)){ |
|
171 | - throw new Exception('Page not found.',404); |
|
170 | + if (!$this->checkProtocol($route['protocol'], $currentProtocol)) { |
|
171 | + throw new Exception('Page not found.', 404); |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | $this->currentRoute = $route; |
@@ -5,11 +5,11 @@ |
||
5 | 5 | |
6 | 6 | |
7 | 7 | /* Filter example */ |
8 | -Router::get('/my-account',function(array $data){ |
|
8 | +Router::get('/my-account', function(array $data) { |
|
9 | 9 | echo '<pre>'; |
10 | 10 | var_dump($data); |
11 | 11 | echo 'Ok'; |
12 | -})->middleware(['\Example\Middleware\Auth::class','Lasted']); |
|
12 | +})->middleware(['\Example\Middleware\Auth::class', 'Lasted']); |
|
13 | 13 | |
14 | 14 | /* Returning parameters passed via URL in anonymous functions |
15 | 15 | Router::get('/{parameter}/{otherparameter}', function($parameter, $otherparameter){ |
@@ -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 |
@@ -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::defineMiddlewares([ |
15 | 15 | 'Lasted'=> \Example\Middleware\Lasted::class |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | /* Return current action route */ |
27 | 27 | $action = Router::currentRouteAction(); |
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 |