HexMakina /
Hopper
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * huppel konijntje huppel and wiebel |
||
| 5 | * Hommage to Grace Hopper, programmer & expert in *litteral* duck taping |
||
| 6 | ***/ |
||
| 7 | |||
| 8 | namespace HexMakina\Hopper; |
||
| 9 | |||
| 10 | class hopper extends \AltoRouter implements RouterInterface |
||
| 11 | { |
||
| 12 | private $match = null; |
||
| 13 | private $file_root = null; |
||
| 14 | private $controller_namespaces = null; |
||
| 15 | |||
| 16 | //----------------------------------------------------------- INITIALISATION |
||
| 17 | public function __construct($settings) |
||
| 18 | { |
||
| 19 | if (!isset($settings['route_home'])) { |
||
| 20 | throw new RouterException('ROUTE_HOME_UNDEFINED'); |
||
| 21 | } |
||
| 22 | |||
| 23 | parent::__construct(); |
||
| 24 | |||
| 25 | $this->set_web_base($settings['web_base'] ?? ''); |
||
| 26 | $this->set_file_root($settings['file_root'] ?? __DIR__); |
||
| 27 | |||
| 28 | $this->controller_namespaces = $settings['controllers_namespaces'] ?? []; |
||
| 29 | |||
| 30 | $this->map(self::REQUEST_GET, '', $settings['route_home'], self::ROUTE_HOME_NAME); |
||
| 31 | } |
||
| 32 | |||
| 33 | public function __debugInfo(): array |
||
| 34 | { |
||
| 35 | $dbg = get_object_vars($this); |
||
| 36 | $dbg['routes'] = count($dbg['routes']); |
||
| 37 | $dbg['namedRoutes'] = count($dbg['namedRoutes']); |
||
| 38 | unset($dbg['matchTypes']); |
||
| 39 | return $dbg; |
||
| 40 | } |
||
| 41 | // -- MATCHING REQUESTS |
||
| 42 | public function match($requestUrl = null, $requestMethod = null) |
||
| 43 | { |
||
| 44 | $this->match = parent::match($requestUrl, $requestMethod); |
||
| 45 | |||
| 46 | if ($this->match === false) { |
||
| 47 | throw new RouterException('ROUTE_MATCH_FALSE'); |
||
| 48 | } |
||
| 49 | |||
| 50 | $res = explode('::', $this->target()); |
||
| 51 | |||
| 52 | if ($res === false || !isset($res[1]) || isset($res[2])) { |
||
| 53 | throw new RouterException('INVALID_TARGET'); |
||
| 54 | } |
||
| 55 | |||
| 56 | $target_controller = $res[0]; |
||
| 57 | $target_method = $res[1]; |
||
| 58 | $found = false; |
||
| 59 | |||
| 60 | $controller_class_name = null; |
||
| 61 | foreach ($this->controller_namespaces as $controller_ns) { |
||
| 62 | if ($found = class_exists($controller_class_name = "$controller_ns$target_controller")) { |
||
| 63 | break; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | if ($found === false) { |
||
| 68 | throw new RouterException('INVALID_CONTROLLER_NAME'); |
||
| 69 | } |
||
| 70 | |||
| 71 | $this->match['target_controller'] = $controller_class_name; |
||
| 72 | $this->match['target_method'] = $target_method; |
||
| 73 | |||
| 74 | return [$controller_class_name, $target_method]; |
||
| 75 | } |
||
| 76 | |||
| 77 | public function params($param_name = null) |
||
| 78 | { |
||
| 79 | return $this->extract_request($this->match['params'] ?? [], $param_name); |
||
| 80 | } |
||
| 81 | |||
| 82 | public function submitted($param_name = null) |
||
| 83 | { |
||
| 84 | return $this->extract_request($_POST, $param_name); |
||
| 85 | } |
||
| 86 | |||
| 87 | private function extract_request($dat_ass, $key = null) |
||
| 88 | { |
||
| 89 | |||
| 90 | // $key is null, returns $dat_ass or empty array |
||
| 91 | if (is_null($key)) { |
||
| 92 | return $dat_ass ?? []; |
||
| 93 | } |
||
| 94 | |||
| 95 | // $dat_ass[$key] not set, returns null |
||
| 96 | if (!isset($dat_ass[$key])) { |
||
| 97 | return null; |
||
| 98 | } |
||
| 99 | |||
| 100 | // $dat_ass[$key] is a string, returns decoded value |
||
| 101 | if (is_string($dat_ass[$key])) { |
||
| 102 | return urldecode($dat_ass[$key]); |
||
| 103 | } |
||
| 104 | |||
| 105 | // $dat_ass[$key] is not a string, return match[$key] |
||
| 106 | return $dat_ass[$key]; |
||
| 107 | } |
||
| 108 | |||
| 109 | public function target() |
||
| 110 | { |
||
| 111 | return $this->match['target']; |
||
| 112 | } |
||
| 113 | |||
| 114 | public function target_controller() |
||
| 115 | { |
||
| 116 | return $this->match['target_controller']; |
||
| 117 | } |
||
| 118 | |||
| 119 | public function target_method() |
||
| 120 | { |
||
| 121 | return $this->match['target_method']; |
||
| 122 | } |
||
| 123 | |||
| 124 | public function name() |
||
| 125 | { |
||
| 126 | return $this->match['name']; |
||
| 127 | } |
||
| 128 | |||
| 129 | // -- ROUTING TOOLS |
||
| 130 | public function route_exists($route): bool |
||
| 131 | { |
||
| 132 | return isset($this->namedRoutes[$route]); |
||
| 133 | } |
||
| 134 | |||
| 135 | public function named_routes() |
||
| 136 | { |
||
| 137 | return $this->namedRoutes; |
||
| 138 | } |
||
| 139 | |||
| 140 | /* |
||
| 141 | * @param route_name string requires |
||
| 142 | * - a valid AltoRouter route name |
||
| 143 | * - OR a Descendant of Model |
||
| 144 | * @route_params requires |
||
| 145 | * - an assoc_array of url params (strongly AltoRouter-based) |
||
| 146 | * returns: something to put in a href="", action="" or header('Location:'); |
||
| 147 | */ |
||
| 148 | public function prehop($route, $route_params = []) |
||
| 149 | { |
||
| 150 | try { |
||
| 151 | $url = $this->generate($route, $route_params); |
||
| 152 | } catch (\Exception $e) { |
||
| 153 | $url = $this->prehop(self::ROUTE_HOME_NAME); |
||
| 154 | } |
||
| 155 | |||
| 156 | return $url; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function prehop_here($url = null) |
||
| 160 | { |
||
| 161 | return $url ?? $_SERVER['REQUEST_URI']; |
||
| 162 | } |
||
| 163 | |||
| 164 | /* |
||
| 165 | * @params $route is |
||
| 166 | * - empty: default is ROUTE_HOME_NAME |
||
| 167 | * - an existing route name: make url with optional [$route_params]) |
||
| 168 | * - a url, go there |
||
| 169 | * @params $route_params, assoc_data for url creation (i:id, a:format, ..) |
||
| 170 | */ |
||
| 171 | public function hop($route = null, $route_params = []) |
||
| 172 | { |
||
| 173 | $url = null; |
||
| 174 | |||
| 175 | if (is_null($route)) { |
||
| 176 | $url = $this->prehop(self::ROUTE_HOME_NAME, $route_params); |
||
| 177 | } elseif (is_string($route) && $this->route_exists($route)) { |
||
| 178 | $url = $this->prehop($route, $route_params); |
||
| 179 | } else { |
||
| 180 | $url = $route; |
||
| 181 | } |
||
| 182 | |||
| 183 | $this->hop_url($url); |
||
| 184 | } |
||
| 185 | |||
| 186 | // hops back to previous page (referer()), or home if no referer |
||
| 187 | public function hop_back() |
||
| 188 | { |
||
| 189 | if (!is_null($back = $this->referer())) { |
||
| 190 | $this->hop_url($back); |
||
| 191 | } |
||
| 192 | |||
| 193 | $this->hop(); |
||
| 194 | } |
||
| 195 | |||
| 196 | public function hop_url($url) |
||
| 197 | { |
||
| 198 | header('Cache-Control: no-cache, must-revalidate'); |
||
| 199 | header('Expires: Mon, 01 Jan 1970 00:00:00 GMT'); |
||
| 200 | header('Location: ' . $url); |
||
| 201 | exit(); |
||
| 202 | } |
||
| 203 | |||
| 204 | // returns full URL of the refering URL |
||
| 205 | // returns null if same as current URL (prevents endless redirection loop) |
||
| 206 | public function referer() |
||
| 207 | { |
||
| 208 | if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != $this->web_host() . $_SERVER['REQUEST_URI']) { |
||
| 209 | return $_SERVER['HTTP_REFERER']; |
||
| 210 | } |
||
| 211 | |||
| 212 | return null; |
||
| 213 | } |
||
| 214 | |||
| 215 | public function send_file($file_path) |
||
| 216 | { |
||
| 217 | if (!file_exists($file_path)) { |
||
| 218 | throw new RouterException('SENDING_NON_EXISTING_FILE'); |
||
| 219 | } |
||
| 220 | |||
| 221 | $file_name = basename($file_path); |
||
| 222 | |||
| 223 | //Get file type and set it as Content Type |
||
| 224 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
||
| 225 | |||
| 226 | header('Content-Type: ' . finfo_file($finfo, $file_path)); |
||
| 227 | |||
| 228 | finfo_close($finfo); |
||
| 229 | |||
| 230 | //Use Content-Disposition: attachment to specify the filename |
||
| 231 | header('Content-Disposition: attachment; filename=' . $file_name); |
||
| 232 | |||
| 233 | //No cache |
||
| 234 | header('Expires: 0'); |
||
| 235 | header('Cache-Control: must-revalidate'); |
||
| 236 | header('Pragma: public'); |
||
| 237 | |||
| 238 | //Define file size |
||
| 239 | header('Content-Length: ' . filesize($file_path)); |
||
| 240 | |||
| 241 | ob_clean(); |
||
| 242 | flush(); |
||
| 243 | readfile($file_path); |
||
| 244 | die; |
||
|
0 ignored issues
–
show
|
|||
| 245 | } |
||
| 246 | |||
| 247 | // -- PROCESSING REQUESTS |
||
| 248 | public function requests(): bool |
||
| 249 | { |
||
| 250 | return $_SERVER['REQUEST_METHOD'] === self::REQUEST_GET; |
||
| 251 | } |
||
| 252 | |||
| 253 | public function submits(): bool |
||
| 254 | { |
||
| 255 | return $_SERVER['REQUEST_METHOD'] === self::REQUEST_POST; |
||
| 256 | } |
||
| 257 | |||
| 258 | public function web_host(): string |
||
| 259 | { |
||
| 260 | return $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST']; |
||
| 261 | } |
||
| 262 | |||
| 263 | public function web_root(): string |
||
| 264 | { |
||
| 265 | return $this->web_host() . $this->web_base(); |
||
| 266 | } |
||
| 267 | |||
| 268 | public function web_base(): string |
||
| 269 | { |
||
| 270 | return $this->basePath ?? ''; |
||
| 271 | } |
||
| 272 | |||
| 273 | public function set_web_base($setter) |
||
| 274 | { |
||
| 275 | $this->setBasePath($setter); |
||
| 276 | } |
||
| 277 | |||
| 278 | public function file_root(): string |
||
| 279 | { |
||
| 280 | return $this->file_root ?? __DIR__; |
||
| 281 | } |
||
| 282 | |||
| 283 | public function set_file_root($setter) |
||
| 284 | { |
||
| 285 | $this->file_root = realpath($setter) . '/'; |
||
| 286 | } |
||
| 287 | } |
||
| 288 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.