@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | |
5 | 5 | use Exception; |
6 | 6 | |
7 | -class Router{ |
|
7 | +class Router { |
|
8 | 8 | use Helper, CheckTrait; |
9 | 9 | |
10 | 10 | private static $instance = null; |
@@ -59,12 +59,12 @@ discard block |
||
59 | 59 | return self::getInstance()->set($uri, $walking, $protocol); |
60 | 60 | } |
61 | 61 | |
62 | - public function set($url ,$walking , string $protocol): Router |
|
62 | + public function set($url, $walking, string $protocol): Router |
|
63 | 63 | { |
64 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
64 | + $url = (substr($url, 0, 1) !== '/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
65 | 65 | |
66 | - foreach($this->routers as $key => $value){ |
|
67 | - if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
66 | + foreach ($this->routers as $key => $value) { |
|
67 | + if (md5($this->prefix.$value['url'].$value['protocol']) === md5($url.$protocol)) { |
|
68 | 68 | throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured."); |
69 | 69 | } |
70 | 70 | } |
@@ -84,9 +84,9 @@ discard block |
||
84 | 84 | return self::getInstance(); |
85 | 85 | } |
86 | 86 | |
87 | - public static function group(string $prefix,$callback): Router |
|
87 | + public static function group(string $prefix, $callback): Router |
|
88 | 88 | { |
89 | - self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix; |
|
89 | + self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix; |
|
90 | 90 | self::getInstance()->group = sha1(date('d/m/Y h:m:i')); |
91 | 91 | $callback(); |
92 | 92 | self::getInstance()->group = null; |
@@ -97,14 +97,14 @@ discard block |
||
97 | 97 | |
98 | 98 | public static function name(string $name): Router |
99 | 99 | { |
100 | - if(self::getInstance()->lastReturn){ |
|
100 | + if (self::getInstance()->lastReturn) { |
|
101 | 101 | throw new Exception("There is no reason to call a {$name} route group."); |
102 | 102 | } |
103 | 103 | |
104 | 104 | $currentRoute = end(self::getInstance()->routers); |
105 | 105 | |
106 | - foreach(self::getInstance()->routers as $key => $value){ |
|
107 | - if(array_key_exists($name, self::getInstance()->routers)){ |
|
106 | + foreach (self::getInstance()->routers as $key => $value) { |
|
107 | + if (array_key_exists($name, self::getInstance()->routers)) { |
|
108 | 108 | throw new Exception("There is already a route with the name {$name} configured."); |
109 | 109 | } |
110 | 110 | } |
@@ -120,15 +120,15 @@ discard block |
||
120 | 120 | |
121 | 121 | private function byName(?string $route_name) |
122 | 122 | { |
123 | - if(!is_null($route_name)){ |
|
123 | + if (!is_null($route_name)) { |
|
124 | 124 | $currentProtocol = $this->getProtocol(); |
125 | 125 | |
126 | 126 | $this->check_name($route_name); |
127 | 127 | |
128 | 128 | $route = $this->routers[$route_name]; |
129 | 129 | |
130 | - if(!$this->check_protocol($route['protocol'], $currentProtocol)){ |
|
131 | - throw new Exception('Page not found.',404); |
|
130 | + if (!$this->check_protocol($route['protocol'], $currentProtocol)) { |
|
131 | + throw new Exception('Page not found.', 404); |
|
132 | 132 | } |
133 | 133 | |
134 | 134 | $this->check_filtering($route); |
@@ -144,23 +144,23 @@ discard block |
||
144 | 144 | |
145 | 145 | $currentProtocol = $this->getProtocol(); |
146 | 146 | |
147 | - foreach(array_reverse($this->routers) as $r => $route){ |
|
147 | + foreach (array_reverse($this->routers) as $r => $route) { |
|
148 | 148 | |
149 | 149 | $_SESSION['route'] = serialize($route); |
150 | 150 | |
151 | - if(!$this->check_protocol($route['protocol'], $currentProtocol)){ |
|
151 | + if (!$this->check_protocol($route['protocol'], $currentProtocol)) { |
|
152 | 152 | continue; |
153 | 153 | } |
154 | 154 | |
155 | 155 | $this->hasProtocol($route, $currentProtocol); |
156 | 156 | |
157 | - $route_loop = $this->explodeRoute( (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url']); |
|
157 | + $route_loop = $this->explodeRoute((substr($route['url'], strlen($route['url'])-1, 1) === '/'), $route['url']); |
|
158 | 158 | |
159 | 159 | $_SERVER['REQUEST_URI'] = (array_key_exists('REQUEST_URI', $_SERVER)) ? $_SERVER['REQUEST_URI'] : ''; |
160 | 160 | |
161 | - $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI']); |
|
161 | + $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/'), $_SERVER['REQUEST_URI']); |
|
162 | 162 | |
163 | - if($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)){ |
|
163 | + if ($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)) { |
|
164 | 164 | continue; |
165 | 165 | } |
166 | 166 | |
@@ -172,18 +172,18 @@ discard block |
||
172 | 172 | |
173 | 173 | unset($_SESSION['route']); |
174 | 174 | |
175 | - throw new Exception('Page not found.',404); |
|
175 | + throw new Exception('Page not found.', 404); |
|
176 | 176 | } |
177 | 177 | |
178 | 178 | public static function filter($filters): Router |
179 | 179 | { |
180 | - if(self::getInstance()->lastReturn !== null){ |
|
180 | + if (self::getInstance()->lastReturn !== null) { |
|
181 | 181 | $currentGroup = end(self::getInstance()->routers)['group']; |
182 | 182 | |
183 | 183 | foreach (self::getInstance()->routers as $key => $value) { |
184 | 184 | |
185 | - if($value['group'] === $currentGroup){ |
|
186 | - $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters); |
|
185 | + if ($value['group'] === $currentGroup) { |
|
186 | + $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters); |
|
187 | 187 | self::getInstance()->routers[$key] = $currentRoute; |
188 | 188 | } |
189 | 189 | |
@@ -194,24 +194,24 @@ discard block |
||
194 | 194 | return self::getInstance(); |
195 | 195 | } |
196 | 196 | |
197 | - self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters); |
|
197 | + self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters); |
|
198 | 198 | return self::getInstance(); |
199 | 199 | } |
200 | 200 | |
201 | 201 | public static function addFilter(array $route, $filter): array |
202 | 202 | { |
203 | - if(is_null($route['filters'])){ |
|
203 | + if (is_null($route['filters'])) { |
|
204 | 204 | $route['filters'] = $filter; |
205 | 205 | return $route; |
206 | 206 | } |
207 | 207 | |
208 | 208 | $filters = (is_array($filter)) ? $filter : [0 => $filter]; |
209 | 209 | |
210 | - if(is_array($route['filters'])){ |
|
210 | + if (is_array($route['filters'])) { |
|
211 | 211 | foreach ($route['filters'] as $key => $value) { |
212 | 212 | $filters[] = $value; |
213 | 213 | } |
214 | - }else{ |
|
214 | + }else { |
|
215 | 215 | $filters[] = $route['filters']; |
216 | 216 | } |
217 | 217 |