@@ -188,8 +188,8 @@ |
||
188 | 188 | |
189 | 189 | public function hop_url($url) |
190 | 190 | { |
191 | - header('Cache-Control: no-cache, must-revalidate'); |
|
192 | - header('Expires: Mon, 01 Jan 1970 00:00:00 GMT'); |
|
191 | + header('Cache-Control: no-cache, must-revalidate'); |
|
192 | + header('Expires: Mon, 01 Jan 1970 00:00:00 GMT'); |
|
193 | 193 | header('Location: '.$url); |
194 | 194 | exit(); |
195 | 195 | } |
@@ -8,14 +8,14 @@ discard block |
||
8 | 8 | |
9 | 9 | class hopper extends \AltoRouter implements RouterInterface |
10 | 10 | { |
11 | - private $match=null; |
|
12 | - private $file_root=null; |
|
11 | + private $match = null; |
|
12 | + private $file_root = null; |
|
13 | 13 | private $controller_namespaces = null; |
14 | 14 | |
15 | 15 | //----------------------------------------------------------- INITIALISATION |
16 | 16 | public function __construct($settings) |
17 | 17 | { |
18 | - if(!isset($settings['route_home'])) |
|
18 | + if (!isset($settings['route_home'])) |
|
19 | 19 | throw new RouterException('ROUTE_HOME_UNDEFINED'); |
20 | 20 | |
21 | 21 | parent::__construct(); |
@@ -41,12 +41,12 @@ discard block |
||
41 | 41 | { |
42 | 42 | $this->match = parent::match($requestUrl, $requestMethod); |
43 | 43 | |
44 | - if($this->match === false) |
|
44 | + if ($this->match === false) |
|
45 | 45 | throw new RouterException('ROUTE_MATCH_FALSE'); |
46 | 46 | |
47 | 47 | $res = explode('::', $this->target()); |
48 | 48 | |
49 | - if($res === false || !isset($res[1]) || isset($res[2])) |
|
49 | + if ($res === false || !isset($res[1]) || isset($res[2])) |
|
50 | 50 | throw new RouterException('INVALID_TARGET'); |
51 | 51 | |
52 | 52 | $target_controller = $res[0]; |
@@ -54,11 +54,11 @@ discard block |
||
54 | 54 | $found = false; |
55 | 55 | |
56 | 56 | $controller_class_name = null; |
57 | - foreach($this->controller_namespaces as $controller_ns) |
|
58 | - if($found = class_exists($controller_class_name = "$controller_ns$target_controller")) |
|
57 | + foreach ($this->controller_namespaces as $controller_ns) |
|
58 | + if ($found = class_exists($controller_class_name = "$controller_ns$target_controller")) |
|
59 | 59 | break; |
60 | 60 | |
61 | - if($found === false) |
|
61 | + if ($found === false) |
|
62 | 62 | throw new RouterException('INVALID_CONTROLLER_NAME'); |
63 | 63 | |
64 | 64 | $this->match['target_controller'] = $controller_class_name; |
@@ -67,29 +67,29 @@ discard block |
||
67 | 67 | return [$controller_class_name, $target_method]; |
68 | 68 | } |
69 | 69 | |
70 | - public function params($param_name=null) |
|
70 | + public function params($param_name = null) |
|
71 | 71 | { |
72 | 72 | return $this->extract_request($this->match['params'] ?? [], $param_name); |
73 | 73 | } |
74 | 74 | |
75 | - public function submitted($param_name=null) |
|
75 | + public function submitted($param_name = null) |
|
76 | 76 | { |
77 | 77 | return $this->extract_request($_POST, $param_name); |
78 | 78 | } |
79 | 79 | |
80 | - private function extract_request($dat_ass, $key=null) |
|
80 | + private function extract_request($dat_ass, $key = null) |
|
81 | 81 | { |
82 | 82 | |
83 | 83 | // $key is null, returns $dat_ass or empty array |
84 | - if(is_null($key)) |
|
84 | + if (is_null($key)) |
|
85 | 85 | return $dat_ass ?? []; |
86 | 86 | |
87 | 87 | // $dat_ass[$key] not set, returns null |
88 | - if(!isset($dat_ass[$key])) |
|
88 | + if (!isset($dat_ass[$key])) |
|
89 | 89 | return null; |
90 | 90 | |
91 | 91 | // $dat_ass[$key] is a string, returns decoded value |
92 | - if(is_string($dat_ass[$key])) |
|
92 | + if (is_string($dat_ass[$key])) |
|
93 | 93 | return urldecode($dat_ass[$key]); |
94 | 94 | |
95 | 95 | // $dat_ass[$key] is not a string, return match[$key] |
@@ -136,18 +136,18 @@ discard block |
||
136 | 136 | * - an assoc_array of url params (strongly AltoRouter-based) |
137 | 137 | * returns: something to put in a href="", action="" or header('Location:'); |
138 | 138 | */ |
139 | - public function prehop($route, $route_params=[]) |
|
139 | + public function prehop($route, $route_params = []) |
|
140 | 140 | { |
141 | - try{ |
|
141 | + try { |
|
142 | 142 | $url = $this->generate($route, $route_params); |
143 | - }catch(\Exception $e){ |
|
143 | + }catch (\Exception $e) { |
|
144 | 144 | $url = $this->prehop(self::ROUTE_HOME_NAME); |
145 | 145 | } |
146 | 146 | |
147 | 147 | return $url; |
148 | 148 | } |
149 | 149 | |
150 | - public function prehop_here($url=null) |
|
150 | + public function prehop_here($url = null) |
|
151 | 151 | { |
152 | 152 | return $url ?? $_SERVER['REQUEST_URI']; |
153 | 153 | } |
@@ -159,13 +159,13 @@ discard block |
||
159 | 159 | * - a url, go there |
160 | 160 | * @params $route_params, assoc_data for url creation (i:id, a:format, ..) |
161 | 161 | */ |
162 | - public function hop($route=null, $route_params=[]) |
|
162 | + public function hop($route = null, $route_params = []) |
|
163 | 163 | { |
164 | 164 | $url = null; |
165 | 165 | |
166 | - if(is_null($route)) |
|
166 | + if (is_null($route)) |
|
167 | 167 | $url = $this->prehop(self::ROUTE_HOME_NAME, $route_params); |
168 | - elseif(is_string($route) && $this->route_exists($route)) |
|
168 | + elseif (is_string($route) && $this->route_exists($route)) |
|
169 | 169 | $url = $this->prehop($route, $route_params); |
170 | 170 | else |
171 | 171 | $url = $route; |
@@ -176,7 +176,7 @@ discard block |
||
176 | 176 | // hops back to previous page (referer()), or home if no referer |
177 | 177 | public function hop_back() |
178 | 178 | { |
179 | - if(!is_null($back = $this->referer())) |
|
179 | + if (!is_null($back = $this->referer())) |
|
180 | 180 | $this->hop_url($back); |
181 | 181 | |
182 | 182 | $this->hop(); |
@@ -194,7 +194,7 @@ discard block |
||
194 | 194 | // returns null if same as current URL (prevents endless redirection loop) |
195 | 195 | public function referer() |
196 | 196 | { |
197 | - if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != $this->web_host() .$_SERVER['REQUEST_URI']) |
|
197 | + if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != $this->web_host().$_SERVER['REQUEST_URI']) |
|
198 | 198 | return $_SERVER['HTTP_REFERER']; |
199 | 199 | |
200 | 200 | return null; |
@@ -202,7 +202,7 @@ discard block |
||
202 | 202 | |
203 | 203 | public function send_file($file_path) |
204 | 204 | { |
205 | - if(!file_exists($file_path)) |
|
205 | + if (!file_exists($file_path)) |
|
206 | 206 | throw new RouterException('SENDING_NON_EXISTING_FILE'); |
207 | 207 | |
208 | 208 | $file_name = basename($file_path); |
@@ -210,7 +210,7 @@ discard block |
||
210 | 210 | //Get file type and set it as Content Type |
211 | 211 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
212 | 212 | |
213 | - header('Content-Type: ' . finfo_file($finfo, $file_path)); |
|
213 | + header('Content-Type: '.finfo_file($finfo, $file_path)); |
|
214 | 214 | |
215 | 215 | finfo_close($finfo); |
216 | 216 | |
@@ -223,7 +223,7 @@ discard block |
||
223 | 223 | header('Pragma: public'); |
224 | 224 | |
225 | 225 | //Define file size |
226 | - header('Content-Length: ' . filesize($file_path)); |
|
226 | + header('Content-Length: '.filesize($file_path)); |
|
227 | 227 | |
228 | 228 | ob_clean(); |
229 | 229 | flush(); |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | |
250 | 250 | public function web_root() : string |
251 | 251 | { |
252 | - return $this->web_host() . $this->web_base(); |
|
252 | + return $this->web_host().$this->web_base(); |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | public function web_base() : string |
@@ -15,8 +15,9 @@ discard block |
||
15 | 15 | //----------------------------------------------------------- INITIALISATION |
16 | 16 | public function __construct($settings) |
17 | 17 | { |
18 | - if(!isset($settings['route_home'])) |
|
19 | - throw new RouterException('ROUTE_HOME_UNDEFINED'); |
|
18 | + if(!isset($settings['route_home'])) { |
|
19 | + throw new RouterException('ROUTE_HOME_UNDEFINED'); |
|
20 | + } |
|
20 | 21 | |
21 | 22 | parent::__construct(); |
22 | 23 | |
@@ -41,25 +42,29 @@ discard block |
||
41 | 42 | { |
42 | 43 | $this->match = parent::match($requestUrl, $requestMethod); |
43 | 44 | |
44 | - if($this->match === false) |
|
45 | - throw new RouterException('ROUTE_MATCH_FALSE'); |
|
45 | + if($this->match === false) { |
|
46 | + throw new RouterException('ROUTE_MATCH_FALSE'); |
|
47 | + } |
|
46 | 48 | |
47 | 49 | $res = explode('::', $this->target()); |
48 | 50 | |
49 | - if($res === false || !isset($res[1]) || isset($res[2])) |
|
50 | - throw new RouterException('INVALID_TARGET'); |
|
51 | + if($res === false || !isset($res[1]) || isset($res[2])) { |
|
52 | + throw new RouterException('INVALID_TARGET'); |
|
53 | + } |
|
51 | 54 | |
52 | 55 | $target_controller = $res[0]; |
53 | 56 | $target_method = $res[1]; |
54 | 57 | $found = false; |
55 | 58 | |
56 | 59 | $controller_class_name = null; |
57 | - foreach($this->controller_namespaces as $controller_ns) |
|
58 | - if($found = class_exists($controller_class_name = "$controller_ns$target_controller")) |
|
60 | + foreach($this->controller_namespaces as $controller_ns) { |
|
61 | + if($found = class_exists($controller_class_name = "$controller_ns$target_controller")) |
|
59 | 62 | break; |
63 | + } |
|
60 | 64 | |
61 | - if($found === false) |
|
62 | - throw new RouterException('INVALID_CONTROLLER_NAME'); |
|
65 | + if($found === false) { |
|
66 | + throw new RouterException('INVALID_CONTROLLER_NAME'); |
|
67 | + } |
|
63 | 68 | |
64 | 69 | $this->match['target_controller'] = $controller_class_name; |
65 | 70 | $this->match['target_method'] = $target_method; |
@@ -81,16 +86,19 @@ discard block |
||
81 | 86 | { |
82 | 87 | |
83 | 88 | // $key is null, returns $dat_ass or empty array |
84 | - if(is_null($key)) |
|
85 | - return $dat_ass ?? []; |
|
89 | + if(is_null($key)) { |
|
90 | + return $dat_ass ?? []; |
|
91 | + } |
|
86 | 92 | |
87 | 93 | // $dat_ass[$key] not set, returns null |
88 | - if(!isset($dat_ass[$key])) |
|
89 | - return null; |
|
94 | + if(!isset($dat_ass[$key])) { |
|
95 | + return null; |
|
96 | + } |
|
90 | 97 | |
91 | 98 | // $dat_ass[$key] is a string, returns decoded value |
92 | - if(is_string($dat_ass[$key])) |
|
93 | - return urldecode($dat_ass[$key]); |
|
99 | + if(is_string($dat_ass[$key])) { |
|
100 | + return urldecode($dat_ass[$key]); |
|
101 | + } |
|
94 | 102 | |
95 | 103 | // $dat_ass[$key] is not a string, return match[$key] |
96 | 104 | return $dat_ass[$key]; |
@@ -140,7 +148,7 @@ discard block |
||
140 | 148 | { |
141 | 149 | try{ |
142 | 150 | $url = $this->generate($route, $route_params); |
143 | - }catch(\Exception $e){ |
|
151 | + } catch(\Exception $e){ |
|
144 | 152 | $url = $this->prehop(self::ROUTE_HOME_NAME); |
145 | 153 | } |
146 | 154 | |
@@ -163,12 +171,13 @@ discard block |
||
163 | 171 | { |
164 | 172 | $url = null; |
165 | 173 | |
166 | - if(is_null($route)) |
|
167 | - $url = $this->prehop(self::ROUTE_HOME_NAME, $route_params); |
|
168 | - elseif(is_string($route) && $this->route_exists($route)) |
|
169 | - $url = $this->prehop($route, $route_params); |
|
170 | - else |
|
171 | - $url = $route; |
|
174 | + if(is_null($route)) { |
|
175 | + $url = $this->prehop(self::ROUTE_HOME_NAME, $route_params); |
|
176 | + } elseif(is_string($route) && $this->route_exists($route)) { |
|
177 | + $url = $this->prehop($route, $route_params); |
|
178 | + } else { |
|
179 | + $url = $route; |
|
180 | + } |
|
172 | 181 | |
173 | 182 | $this->hop_url($url); |
174 | 183 | } |
@@ -176,8 +185,9 @@ discard block |
||
176 | 185 | // hops back to previous page (referer()), or home if no referer |
177 | 186 | public function hop_back() |
178 | 187 | { |
179 | - if(!is_null($back = $this->referer())) |
|
180 | - $this->hop_url($back); |
|
188 | + if(!is_null($back = $this->referer())) { |
|
189 | + $this->hop_url($back); |
|
190 | + } |
|
181 | 191 | |
182 | 192 | $this->hop(); |
183 | 193 | } |
@@ -194,16 +204,18 @@ discard block |
||
194 | 204 | // returns null if same as current URL (prevents endless redirection loop) |
195 | 205 | public function referer() |
196 | 206 | { |
197 | - if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != $this->web_host() .$_SERVER['REQUEST_URI']) |
|
198 | - return $_SERVER['HTTP_REFERER']; |
|
207 | + if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != $this->web_host() .$_SERVER['REQUEST_URI']) { |
|
208 | + return $_SERVER['HTTP_REFERER']; |
|
209 | + } |
|
199 | 210 | |
200 | 211 | return null; |
201 | 212 | } |
202 | 213 | |
203 | 214 | public function send_file($file_path) |
204 | 215 | { |
205 | - if(!file_exists($file_path)) |
|
206 | - throw new RouterException('SENDING_NON_EXISTING_FILE'); |
|
216 | + if(!file_exists($file_path)) { |
|
217 | + throw new RouterException('SENDING_NON_EXISTING_FILE'); |
|
218 | + } |
|
207 | 219 | |
208 | 220 | $file_name = basename($file_path); |
209 | 221 |
@@ -13,17 +13,17 @@ |
||
13 | 13 | public function match($requestUrl = null, $requestMethod = null); |
14 | 14 | |
15 | 15 | // generates URL for href and location |
16 | - public function prehop($route, $route_params=[]); |
|
16 | + public function prehop($route, $route_params = []); |
|
17 | 17 | |
18 | 18 | // heads to another location |
19 | - public function hop($route, $route_params=[]); |
|
19 | + public function hop($route, $route_params = []); |
|
20 | 20 | |
21 | 21 | // do you GET it ? |
22 | 22 | public function requests() : bool; |
23 | 23 | |
24 | 24 | // have you POSTed it ? |
25 | 25 | public function submits() : bool; |
26 | - public function submitted($param_name=null); |
|
26 | + public function submitted($param_name = null); |
|
27 | 27 | |
28 | 28 | public function send_file($file_path); |
29 | 29 |