alexdodonov /
mezon-router
| 1 | <?php |
||
| 2 | namespace Mezon\Router; |
||
| 3 | |||
| 4 | trait RoutesSet |
||
| 5 | { |
||
| 6 | |||
| 7 | /** |
||
| 8 | * List off routes for all supported request methods |
||
| 9 | */ |
||
| 10 | protected $routes = [ |
||
| 11 | 'GET' => [], |
||
| 12 | 'POST' => [], |
||
| 13 | 'PUT' => [], |
||
| 14 | 'DELETE' => [], |
||
| 15 | 'OPTION' => [] |
||
| 16 | ]; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Route names |
||
| 20 | * |
||
| 21 | * @var array |
||
| 22 | */ |
||
| 23 | private $routeNames = []; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * This flag rises when we add route / * / |
||
| 27 | * |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | protected $universalRouteWasAdded = false; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Method validates request method |
||
| 34 | * |
||
| 35 | * @param string $requestMethod |
||
| 36 | * HTTP request method |
||
| 37 | */ |
||
| 38 | protected function validateRequestMethod(string $requestMethod): void |
||
| 39 | { |
||
| 40 | if (isset($this->routes[$requestMethod]) === false) { |
||
| 41 | throw (new \Exception('Unsupported request method')); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Method returns list of routes for the HTTP method. |
||
| 47 | * |
||
| 48 | * @param string $requestMethod |
||
| 49 | * HTTP request method |
||
| 50 | * @return array Routes |
||
| 51 | */ |
||
| 52 | protected function &getRoutesForMethod(string $requestMethod): array |
||
| 53 | { |
||
| 54 | return $this->routes[$requestMethod]; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Method returns a list of supported request methods |
||
| 59 | * |
||
| 60 | * @return array list of supported request methods |
||
| 61 | */ |
||
| 62 | public function getListOfSupportedRequestMethods(): array |
||
| 63 | { |
||
| 64 | return [ |
||
| 65 | 'GET', |
||
| 66 | 'POST', |
||
| 67 | 'PUT', |
||
| 68 | 'DELETE', |
||
| 69 | 'OPTION' |
||
| 70 | ]; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Method clears router data. |
||
| 75 | */ |
||
| 76 | public function clear() |
||
| 77 | { |
||
| 78 | $this->universalRouteWasAdded = false; |
||
| 79 | |||
| 80 | $this->routeNames = []; |
||
| 81 | |||
| 82 | foreach ($this->getListOfSupportedRequestMethods() as $requestMethod) { |
||
| 83 | $this->routes[$requestMethod] = []; |
||
| 84 | } |
||
| 85 | |||
| 86 | $this->cachedRegExps = []; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Method returns true if the router exists |
||
| 91 | * |
||
| 92 | * @param string $route |
||
| 93 | * checking route |
||
| 94 | * @return bool true if the router exists, false otherwise |
||
| 95 | */ |
||
| 96 | public function routeExists(string $route): bool |
||
| 97 | { |
||
| 98 | $route = trim($route, '/'); |
||
| 99 | |||
| 100 | foreach ($this->getListOfSupportedRequestMethods() as $requestMethod) { |
||
| 101 | if (isset($this->routes[$requestMethod][$route])) { |
||
| 102 | return true; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | return false; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Method rturns all available routes |
||
| 111 | */ |
||
| 112 | public function getAllRoutesTrace() |
||
| 113 | { |
||
| 114 | $trace = []; |
||
| 115 | |||
| 116 | foreach ($this->getListOfSupportedRequestMethods() as $requestMethod) { |
||
| 117 | if (count($this->routes[$requestMethod]) > 0) { |
||
| 118 | $trace[] = $requestMethod . ':' . implode(', ', array_keys($this->routes[$requestMethod])); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | return implode('; ', $trace); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Additing route for GET request |
||
| 127 | * |
||
| 128 | * @param string $route |
||
| 129 | * route |
||
| 130 | * @param object $object |
||
| 131 | * callback object |
||
| 132 | * @param string $method |
||
| 133 | * callback method |
||
| 134 | */ |
||
| 135 | public function addGetRoute(string $route, object $object, string $method): void |
||
| 136 | { |
||
| 137 | $this->routes['GET'][trim($route, '/')] = [ |
||
| 138 | $object, |
||
| 139 | $method |
||
| 140 | ]; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Additing route for GET request |
||
| 145 | * |
||
| 146 | * @param string $route |
||
| 147 | * route |
||
| 148 | * @param object $object |
||
| 149 | * callback object |
||
| 150 | * @param string $method |
||
| 151 | * callback method |
||
| 152 | */ |
||
| 153 | public function addPostRoute(string $route, object $object, string $method): void |
||
| 154 | { |
||
| 155 | $this->routes['POST'][trim($route, '/')] = [ |
||
| 156 | $object, |
||
| 157 | $method |
||
| 158 | ]; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Method registers name of the route |
||
| 163 | * |
||
| 164 | * @param string $routeName |
||
| 165 | * route's name |
||
| 166 | * @param string $route |
||
| 167 | * route |
||
| 168 | */ |
||
| 169 | protected function registerRouteName(string $routeName, string $route): void |
||
| 170 | { |
||
| 171 | if ($routeName != '') { |
||
| 172 | $this->routeNames[$routeName] = $route; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Validating that route name exists |
||
| 178 | * |
||
| 179 | * @param string $routeName |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | protected function routeNameExists(string $routeName): bool |
||
| 183 | { |
||
| 184 | return isset($this->routeNames[$routeName]); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Getting route by name |
||
| 189 | * |
||
| 190 | * @param string $routeName |
||
| 191 | * route's name |
||
| 192 | * @return string route |
||
| 193 | */ |
||
| 194 | public function getRouteByName(string $routeName): string |
||
| 195 | { |
||
| 196 | if ($this->routeNameExists($routeName) === false) { |
||
| 197 | throw (new \Exception('Route with name ' . $routeName . ' does not exist')); |
||
| 198 | } |
||
| 199 | |||
| 200 | return $this->routeNames[$routeName]; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Method dumps all routes and their names on disk |
||
| 205 | * |
||
| 206 | * @param string $filePath |
||
| 207 | * file path to cache |
||
| 208 | * @codeCoverageIgnore |
||
| 209 | */ |
||
| 210 | public function dumpOnDisk(string $filePath = './cache/cache.php'): void |
||
| 211 | { |
||
| 212 | file_put_contents( |
||
| 213 | $filePath, |
||
| 214 | '<?php return ' . |
||
| 215 | var_export( |
||
| 216 | [ |
||
| 217 | 0 => $this->routes, |
||
| 218 | 1 => $this->routeNames, |
||
| 219 | 2 => $this->cachedRegExps, |
||
| 220 | 3 => $this->cachedParameters |
||
| 221 | ], |
||
| 222 | true) . ';'); |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Method loads routes from disk |
||
| 227 | * |
||
| 228 | * @param string $filePath |
||
| 229 | * file path to cache |
||
| 230 | * @codeCoverageIgnore |
||
| 231 | */ |
||
| 232 | public function loadFromDisk(string $filePath = './cache/cache.php'): void |
||
| 233 | { |
||
| 234 | list ($this->routes, $this->routeNames, $this->cachedRegExps, $this->cachedParameters) = require ($filePath); |
||
|
0 ignored issues
–
show
|
|||
| 235 | } |
||
| 236 | } |
||
| 237 |