alexdodonov /
mezon-router
| 1 | <?php |
||||||
| 2 | declare(strict_types = 1); |
||||||
| 3 | namespace Mezon\Router; |
||||||
| 4 | |||||||
| 5 | /** |
||||||
| 6 | * Trait RoutesSetBase |
||||||
| 7 | * |
||||||
| 8 | * @package Router |
||||||
| 9 | * @author Dodonov A.A. |
||||||
| 10 | * @version v.1.0 (2021/09/27) |
||||||
| 11 | * @copyright Copyright (c) 2021, http://aeon.su |
||||||
| 12 | */ |
||||||
| 13 | trait RoutesSetBase |
||||||
| 14 | { |
||||||
| 15 | |||||||
| 16 | /** |
||||||
| 17 | * Method returns true if the param router exists |
||||||
| 18 | * |
||||||
| 19 | * @param string $route |
||||||
| 20 | * checking route |
||||||
| 21 | * @param string $requestMethod |
||||||
| 22 | * HTTP request method |
||||||
| 23 | * @return bool true if the param router exists, false otherwise |
||||||
| 24 | */ |
||||||
| 25 | protected abstract function paramRouteExists(string $route, string $requestMethod): bool; |
||||||
| 26 | |||||||
| 27 | /** |
||||||
| 28 | * Method returns true if the router exists |
||||||
| 29 | * |
||||||
| 30 | * @param string $route |
||||||
| 31 | * checking route |
||||||
| 32 | * @return bool true if the router exists, false otherwise |
||||||
| 33 | */ |
||||||
| 34 | public function routeExists(string $route): bool |
||||||
| 35 | { |
||||||
| 36 | $route = trim($route, '/'); |
||||||
| 37 | |||||||
| 38 | foreach (SupportedRequestMethods::getListOfSupportedRequestMethods() as $requestMethod) { |
||||||
| 39 | if (isset($this->staticRoutes[$requestMethod][$route])) { |
||||||
| 40 | return true; |
||||||
| 41 | } else { |
||||||
| 42 | if ($this->paramRouteExists($route, $requestMethod)) { |
||||||
| 43 | return true; |
||||||
| 44 | } |
||||||
| 45 | } |
||||||
| 46 | } |
||||||
| 47 | |||||||
| 48 | return false; |
||||||
| 49 | } |
||||||
| 50 | |||||||
| 51 | /** |
||||||
| 52 | * Route names |
||||||
| 53 | * |
||||||
| 54 | * @var string[] |
||||||
| 55 | */ |
||||||
| 56 | private $routeNames = []; |
||||||
| 57 | |||||||
| 58 | /** |
||||||
| 59 | * Method clears other data |
||||||
| 60 | */ |
||||||
| 61 | protected function clearOtherData(): void |
||||||
| 62 | {} |
||||||
| 63 | |||||||
| 64 | /** |
||||||
| 65 | * Method clears middleware |
||||||
| 66 | */ |
||||||
| 67 | protected abstract function clearMiddleware(): void; |
||||||
| 68 | |||||||
| 69 | /** |
||||||
| 70 | * Method clears router data |
||||||
| 71 | */ |
||||||
| 72 | public function clear(): void |
||||||
| 73 | { |
||||||
| 74 | $this->routeNames = []; |
||||||
| 75 | |||||||
| 76 | foreach (SupportedRequestMethods::getListOfSupportedRequestMethods() as $requestMethod) { |
||||||
| 77 | $this->staticRoutes[$requestMethod] = []; |
||||||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||||
| 78 | $this->paramRoutes[$requestMethod] = []; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 79 | } |
||||||
| 80 | |||||||
| 81 | $this->clearMiddleware(); |
||||||
| 82 | |||||||
| 83 | $this->clearOtherData(); |
||||||
| 84 | } |
||||||
| 85 | |||||||
| 86 | /** |
||||||
| 87 | * Getting route by name |
||||||
| 88 | * |
||||||
| 89 | * @param string $routeName |
||||||
| 90 | * route's name |
||||||
| 91 | * @return string route |
||||||
| 92 | */ |
||||||
| 93 | public function getRouteByName(string $routeName): string |
||||||
| 94 | { |
||||||
| 95 | if ($this->routeNameExists($routeName) === false) { |
||||||
| 96 | throw (new \Exception('Route with name ' . $routeName . ' does not exist')); |
||||||
| 97 | } |
||||||
| 98 | |||||||
| 99 | return $this->routeNames[$routeName]; |
||||||
| 100 | } |
||||||
| 101 | |||||||
| 102 | /** |
||||||
| 103 | * Validating that route name exists |
||||||
| 104 | * |
||||||
| 105 | * @param string $routeName |
||||||
| 106 | * route name |
||||||
| 107 | * @return bool true if the route exists, false otherwise |
||||||
| 108 | */ |
||||||
| 109 | protected function routeNameExists(string $routeName): bool |
||||||
| 110 | { |
||||||
| 111 | return isset($this->routeNames[$routeName]); |
||||||
| 112 | } |
||||||
| 113 | |||||||
| 114 | /** |
||||||
| 115 | * Method registers name of the route |
||||||
| 116 | * |
||||||
| 117 | * @param string $routeName |
||||||
| 118 | * route's name |
||||||
| 119 | * @param string $route |
||||||
| 120 | * route |
||||||
| 121 | */ |
||||||
| 122 | protected function registerRouteName(string $routeName, string $route): void |
||||||
| 123 | { |
||||||
| 124 | if ($routeName != '') { |
||||||
| 125 | $this->routeNames[$routeName] = $route; |
||||||
| 126 | } |
||||||
| 127 | } |
||||||
| 128 | |||||||
| 129 | /** |
||||||
| 130 | * Additing route for GET request |
||||||
| 131 | * |
||||||
| 132 | * @param string $route |
||||||
| 133 | * route |
||||||
| 134 | * @param object $object |
||||||
| 135 | * callback object |
||||||
| 136 | * @param string $method |
||||||
| 137 | * callback method |
||||||
| 138 | */ |
||||||
| 139 | public function addGetRoute(string $route, object $object, string $method): void |
||||||
| 140 | { |
||||||
| 141 | $this->addRoute($route, [ |
||||||
| 142 | $object, |
||||||
| 143 | $method |
||||||
| 144 | ], 'GET'); |
||||||
| 145 | } |
||||||
| 146 | |||||||
| 147 | /** |
||||||
| 148 | * Additing route for GET request |
||||||
| 149 | * |
||||||
| 150 | * @param string $route |
||||||
| 151 | * route |
||||||
| 152 | * @param object $object |
||||||
| 153 | * callback object |
||||||
| 154 | * @param string $method |
||||||
| 155 | * callback method |
||||||
| 156 | */ |
||||||
| 157 | public function addPostRoute(string $route, object $object, string $method): void |
||||||
| 158 | { |
||||||
| 159 | $this->addRoute($route, [ |
||||||
| 160 | $object, |
||||||
| 161 | $method |
||||||
| 162 | ], 'POST'); |
||||||
| 163 | } |
||||||
| 164 | |||||||
| 165 | /** |
||||||
| 166 | * Method adds route and it's handler |
||||||
| 167 | * |
||||||
| 168 | * $callback function may have two parameters - $route and $parameters. Where $route is a called route, |
||||||
| 169 | * and $parameters is associative array (parameter name => parameter value) with URL parameters |
||||||
| 170 | * |
||||||
| 171 | * @param string $route |
||||||
| 172 | * route |
||||||
| 173 | * @param array{0: object, 1: string}|array{0: string, 1: string}|callable|string $callback |
||||||
| 174 | * callback wich will be processing route call. |
||||||
| 175 | * @param string|string[] $requestMethod |
||||||
| 176 | * request type |
||||||
| 177 | * @param string $routeName |
||||||
| 178 | * name of the route |
||||||
| 179 | */ |
||||||
| 180 | public function addRoute(string $route, $callback, $requestMethod = 'GET', string $routeName = ''): void |
||||||
| 181 | { |
||||||
| 182 | $route = Utils::prepareRoute($route); |
||||||
| 183 | |||||||
| 184 | if (is_array($requestMethod)) { |
||||||
| 185 | foreach ($requestMethod as $r) { |
||||||
| 186 | $this->addRoute($route, $callback, $r, $routeName); |
||||||
| 187 | } |
||||||
| 188 | } else { |
||||||
| 189 | SupportedRequestMethods::validateRequestMethod($requestMethod); |
||||||
| 190 | |||||||
| 191 | if (strpos($route, '[') === false) { |
||||||
| 192 | $this->staticRoutes[$requestMethod][$route] = $callback; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 193 | } else { |
||||||
| 194 | $this->addParamRoute($requestMethod, $route, $callback); |
||||||
|
0 ignored issues
–
show
The method
addParamRoute() does not exist on Mezon\Router\RoutesSetBase. Did you maybe mean addRoute()?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 195 | } |
||||||
| 196 | // register route name |
||||||
| 197 | $this->registerRouteName($routeName, $route); |
||||||
| 198 | } |
||||||
| 199 | } |
||||||
| 200 | |||||||
| 201 | /** |
||||||
| 202 | * Compiling route into URL |
||||||
| 203 | * |
||||||
| 204 | * @param string $routeName |
||||||
| 205 | * route name |
||||||
| 206 | * @param string[] $parameters |
||||||
| 207 | * parameters to use in URL |
||||||
| 208 | * @return string compiled route |
||||||
| 209 | */ |
||||||
| 210 | public function reverse(string $routeName, array $parameters = []): string |
||||||
| 211 | { |
||||||
| 212 | $route = $this->getRouteByName($routeName); |
||||||
| 213 | |||||||
| 214 | foreach ($parameters as $name => $value) { |
||||||
| 215 | $route = preg_replace('/\[([A-Za-z_\-])\:' . $name . ']/', $value, $route); |
||||||
| 216 | } |
||||||
| 217 | |||||||
| 218 | return $route; |
||||||
| 219 | } |
||||||
| 220 | } |
||||||
| 221 |