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