HexMakina /
Hopper
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * huppel konijntje huppel and wiebel |
||
| 5 | * Hommage to Grace Hopper, programmer & expert in *litteral* bug taping |
||
| 6 | ***/ |
||
| 7 | |||
| 8 | namespace HexMakina\Hopper; |
||
| 9 | |||
| 10 | class Hopper extends \AltoRouter implements \HexMakina\BlackBox\RouterInterface |
||
| 11 | { |
||
| 12 | private Request $request; |
||
| 13 | private Target $target; |
||
| 14 | |||
| 15 | private $file_root = null; |
||
| 16 | |||
| 17 | public function __construct($route_home, $web_base, $file_root) |
||
| 18 | { |
||
| 19 | $this->mapHomeRoute($route_home); |
||
| 20 | $this->basePath($web_base); |
||
| 21 | $this->filePath($file_root); |
||
| 22 | } |
||
| 23 | |||
| 24 | //----------------------------------------------------------- INITIALISATION |
||
| 25 | |||
| 26 | public function mapHomeRoute($route) |
||
| 27 | { |
||
| 28 | $this->map(self::REQUEST_GET, '', $route, self::ROUTE_HOME_NAME); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function __debugInfo(): array |
||
| 32 | { |
||
| 33 | $dbg = get_object_vars($this); |
||
| 34 | $dbg['routes'] = count($dbg['routes']); |
||
| 35 | $dbg['namedRoutes'] = count($dbg['namedRoutes']); |
||
| 36 | unset($dbg['matchTypes']); |
||
| 37 | return $dbg; |
||
| 38 | } |
||
| 39 | |||
| 40 | // -- MATCHING REQUESTS |
||
| 41 | public function match($requestUrl = null, $requestMethod = null) |
||
| 42 | { |
||
| 43 | $match = parent::match($requestUrl, $requestMethod); |
||
| 44 | |||
| 45 | if ($match === false) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 46 | throw new RouterException('ROUTE_MATCH_FALSE'); |
||
| 47 | } |
||
| 48 | |||
| 49 | $this->request = new Request($match); |
||
| 50 | $this->target = new Target($this->request); |
||
| 51 | return [$this->target->controller(), $this->target->method()]; |
||
| 52 | } |
||
| 53 | |||
| 54 | // DEPRECATE |
||
| 55 | public function params($param_name = null) |
||
| 56 | { |
||
| 57 | return $this->request->params($param_name); |
||
| 58 | } |
||
| 59 | |||
| 60 | // DEPRECATE |
||
| 61 | public function submitted($param_name = null) |
||
| 62 | { |
||
| 63 | return $this->request->submitted($param_name); |
||
| 64 | } |
||
| 65 | |||
| 66 | // DEPRECATE |
||
| 67 | public function name() |
||
| 68 | { |
||
| 69 | return $this->request->name(); |
||
| 70 | } |
||
| 71 | |||
| 72 | // DEPRECATE |
||
| 73 | // public function target() |
||
| 74 | // { |
||
| 75 | // return $this->match['target']; |
||
| 76 | // } |
||
| 77 | |||
| 78 | // DEPRECATE |
||
| 79 | public function targetController() |
||
| 80 | { |
||
| 81 | return $this->target->controller(); |
||
| 82 | } |
||
| 83 | |||
| 84 | // DEPRECATE |
||
| 85 | public function targetMethod() |
||
| 86 | { |
||
| 87 | return $this->target->method(); |
||
| 88 | } |
||
| 89 | |||
| 90 | // -- ROUTING TOOLS |
||
| 91 | public function routeExists($route): bool |
||
| 92 | { |
||
| 93 | return isset($this->namedRoutes[$route]); |
||
| 94 | } |
||
| 95 | |||
| 96 | public function namedRoutes() |
||
| 97 | { |
||
| 98 | return $this->namedRoutes; |
||
| 99 | } |
||
| 100 | |||
| 101 | /* Generates HYPertext reference |
||
| 102 | * @param route_name string requires |
||
| 103 | * - a valid AltoRouter route name |
||
| 104 | * - OR a Descendant of Model |
||
| 105 | * @route_params requires |
||
| 106 | * - an assoc_array of url params (strongly AltoRouter-based) |
||
| 107 | * returns: something to put in a href="", action="" or header('Location:'); |
||
| 108 | */ |
||
| 109 | public function hyp($route, $route_params = []) |
||
| 110 | { |
||
| 111 | try { |
||
| 112 | $url = $this->generate($route, $route_params); |
||
| 113 | } catch (\Exception $e) { |
||
| 114 | $url = $this->hyp(self::ROUTE_HOME_NAME); |
||
| 115 | } |
||
| 116 | |||
| 117 | return $url; |
||
| 118 | } |
||
| 119 | |||
| 120 | /* |
||
| 121 | * @params $route is |
||
| 122 | * - empty: default is ROUTE_HOME_NAME |
||
| 123 | * - an existing route name: make url with optional [$route_params]) |
||
| 124 | * - a url, go there |
||
| 125 | * @params $route_params, assoc_data for url creation (i:id, a:format, ..) |
||
| 126 | */ |
||
| 127 | public function hop($route = null, $route_params = []) |
||
| 128 | { |
||
| 129 | $url = null; |
||
| 130 | |||
| 131 | if (is_null($route)) { |
||
| 132 | $url = $this->hyp(self::ROUTE_HOME_NAME, $route_params); |
||
| 133 | } elseif (is_string($route) && $this->routeExists($route)) { |
||
| 134 | $url = $this->hyp($route, $route_params); |
||
| 135 | } else { |
||
| 136 | $url = $route; |
||
| 137 | } |
||
| 138 | |||
| 139 | $this->hopURL($url); |
||
| 140 | } |
||
| 141 | |||
| 142 | public function stay($url = null) |
||
| 143 | { |
||
| 144 | return $url ?? $_SERVER['REQUEST_URI']; |
||
| 145 | } |
||
| 146 | |||
| 147 | // hops back to previous page (referer()), or home if no referer |
||
| 148 | public function hopBack() |
||
| 149 | { |
||
| 150 | if (!is_null($back = $this->referer())) { |
||
| 151 | $this->hopURL($back); |
||
| 152 | } |
||
| 153 | |||
| 154 | $this->hop(); |
||
| 155 | } |
||
| 156 | |||
| 157 | public function hopURL($url) |
||
| 158 | { |
||
| 159 | header('Cache-Control: no-cache, must-revalidate'); |
||
| 160 | header('Expires: Mon, 01 Jan 1970 00:00:00 GMT'); |
||
| 161 | header('Location: ' . $url); |
||
| 162 | exit(); |
||
| 163 | } |
||
| 164 | |||
| 165 | // returns full URL of the refering URL |
||
| 166 | // returns null if same as current URL (prevents endless redirection loop) |
||
| 167 | public function referer() |
||
| 168 | { |
||
| 169 | if (isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != $this->webHost() . $_SERVER['REQUEST_URI']) { |
||
| 170 | return $_SERVER['HTTP_REFERER']; |
||
| 171 | } |
||
| 172 | |||
| 173 | return null; |
||
| 174 | } |
||
| 175 | |||
| 176 | public function sendFile($file_path) |
||
| 177 | { |
||
| 178 | if (!file_exists($file_path)) { |
||
| 179 | throw new RouterException('SENDING_NON_EXISTING_FILE'); |
||
| 180 | } |
||
| 181 | |||
| 182 | $file_name = basename($file_path); |
||
| 183 | |||
| 184 | //Get file type and set it as Content Type |
||
| 185 | $finfo = finfo_open(FILEINFO_MIME_TYPE); |
||
| 186 | |||
| 187 | header('Content-Type: ' . finfo_file($finfo, $file_path)); |
||
| 188 | |||
| 189 | finfo_close($finfo); |
||
| 190 | |||
| 191 | //Use Content-Disposition: attachment to specify the filename |
||
| 192 | header('Content-Disposition: attachment; filename=' . $file_name); |
||
| 193 | |||
| 194 | //No cache |
||
| 195 | header('Expires: 0'); |
||
| 196 | header('Cache-Control: must-revalidate'); |
||
| 197 | header('Pragma: public'); |
||
| 198 | |||
| 199 | //Define file size |
||
| 200 | header('Content-Length: ' . filesize($file_path)); |
||
| 201 | |||
| 202 | ob_clean(); |
||
| 203 | flush(); |
||
| 204 | readfile($file_path); |
||
| 205 | // die; // might be useless after all |
||
| 206 | } |
||
| 207 | |||
| 208 | // -- PROCESSING REQUESTS |
||
| 209 | public function requests(): bool |
||
| 210 | { |
||
| 211 | return $_SERVER['REQUEST_METHOD'] === self::REQUEST_GET; |
||
| 212 | } |
||
| 213 | |||
| 214 | public function submits(): bool |
||
| 215 | { |
||
| 216 | return $_SERVER['REQUEST_METHOD'] === self::REQUEST_POST; |
||
| 217 | } |
||
| 218 | |||
| 219 | public function webHost(): string |
||
| 220 | { |
||
| 221 | return $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST']; |
||
| 222 | } |
||
| 223 | |||
| 224 | public function webRoot(): string |
||
| 225 | { |
||
| 226 | return $this->webHost() . $this->basePath(); |
||
| 227 | } |
||
| 228 | |||
| 229 | // return web base |
||
| 230 | public function basePath($setter = null): string |
||
| 231 | { |
||
| 232 | if (!is_null($setter)) { |
||
| 233 | $this->basePath = $setter; |
||
| 234 | } |
||
| 235 | |||
| 236 | return $this->basePath ?? ''; |
||
| 237 | } |
||
| 238 | |||
| 239 | // returns root filepath for project |
||
| 240 | // default out of vendor/hexmakina/Hopper |
||
| 241 | public function filePath($setter = null): string |
||
| 242 | { |
||
| 243 | if (!is_null($setter)) { |
||
| 244 | $this->file_root = realpath($setter) . '/'; |
||
| 245 | } |
||
| 246 | |||
| 247 | return $this->file_root ?? __DIR__ . '/../../'; |
||
| 248 | } |
||
| 249 | |||
| 250 | |||
| 251 | |||
| 252 | } |
||
| 253 |