yiisoft /
router
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Yiisoft\Router; |
||
| 6 | |||
| 7 | use InvalidArgumentException; |
||
| 8 | use RuntimeException; |
||
| 9 | use Stringable; |
||
| 10 | use Yiisoft\Http\Method; |
||
| 11 | use Yiisoft\Router\Internal\MiddlewareFilter; |
||
| 12 | |||
| 13 | use function in_array; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Route defines a mapping from URL to callback / name and vice versa. |
||
| 17 | */ |
||
| 18 | final class Route implements Stringable |
||
| 19 | { |
||
| 20 | private ?string $name = null; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string[] |
||
| 24 | */ |
||
| 25 | private array $hosts = []; |
||
| 26 | private bool $override = false; |
||
| 27 | private bool $actionAdded = false; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array[]|callable[]|string[] |
||
| 31 | * @psalm-var list<array|callable|string> |
||
| 32 | */ |
||
| 33 | private array $middlewares = []; |
||
| 34 | |||
| 35 | private array $disabledMiddlewares = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @psalm-var list<array|callable|string>|null |
||
| 39 | */ |
||
| 40 | private ?array $enabledMiddlewaresCache = null; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array<string,string> |
||
| 44 | */ |
||
| 45 | private array $defaults = []; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param string[] $methods |
||
| 49 | */ |
||
| 50 | 91 | private function __construct( |
|
| 51 | private array $methods, |
||
| 52 | private string $pattern, |
||
| 53 | ) { |
||
| 54 | 91 | } |
|
| 55 | |||
| 56 | 79 | public static function get(string $pattern): self |
|
| 57 | { |
||
| 58 | 79 | return self::methods([Method::GET], $pattern); |
|
| 59 | } |
||
| 60 | |||
| 61 | 9 | public static function post(string $pattern): self |
|
| 62 | { |
||
| 63 | 9 | return self::methods([Method::POST], $pattern); |
|
| 64 | } |
||
| 65 | |||
| 66 | 4 | public static function put(string $pattern): self |
|
| 67 | { |
||
| 68 | 4 | return self::methods([Method::PUT], $pattern); |
|
| 69 | } |
||
| 70 | |||
| 71 | 1 | public static function delete(string $pattern): self |
|
| 72 | { |
||
| 73 | 1 | return self::methods([Method::DELETE], $pattern); |
|
| 74 | } |
||
| 75 | |||
| 76 | 1 | public static function patch(string $pattern): self |
|
| 77 | { |
||
| 78 | 1 | return self::methods([Method::PATCH], $pattern); |
|
| 79 | } |
||
| 80 | |||
| 81 | 1 | public static function head(string $pattern): self |
|
| 82 | { |
||
| 83 | 1 | return self::methods([Method::HEAD], $pattern); |
|
| 84 | } |
||
| 85 | |||
| 86 | 9 | public static function options(string $pattern): self |
|
| 87 | { |
||
| 88 | 9 | return self::methods([Method::OPTIONS], $pattern); |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param string[] $methods |
||
| 93 | */ |
||
| 94 | 91 | public static function methods(array $methods, string $pattern): self |
|
| 95 | { |
||
| 96 | 91 | return new self($methods, $pattern); |
|
| 97 | } |
||
| 98 | |||
| 99 | 25 | public function name(string $name): self |
|
| 100 | { |
||
| 101 | 25 | $route = clone $this; |
|
| 102 | 25 | $route->name = $name; |
|
| 103 | 25 | return $route; |
|
| 104 | } |
||
| 105 | |||
| 106 | 24 | public function pattern(string $pattern): self |
|
| 107 | { |
||
| 108 | 24 | $new = clone $this; |
|
| 109 | 24 | $new->pattern = $pattern; |
|
| 110 | 24 | return $new; |
|
| 111 | } |
||
| 112 | |||
| 113 | 11 | public function host(string $host): self |
|
| 114 | { |
||
| 115 | 11 | return $this->hosts($host); |
|
| 116 | } |
||
| 117 | |||
| 118 | 15 | public function hosts(string ...$hosts): self |
|
| 119 | { |
||
| 120 | 15 | $route = clone $this; |
|
| 121 | 15 | $route->hosts = []; |
|
| 122 | |||
| 123 | 15 | foreach ($hosts as $host) { |
|
| 124 | 15 | $host = rtrim($host, '/'); |
|
| 125 | |||
| 126 | 15 | if ($host !== '' && !in_array($host, $route->hosts, true)) { |
|
| 127 | 14 | $route->hosts[] = $host; |
|
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | 15 | return $route; |
|
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Marks route as override. When added it will replace existing route with the same name. |
||
| 136 | */ |
||
| 137 | 4 | public function override(): self |
|
| 138 | { |
||
| 139 | 4 | $route = clone $this; |
|
| 140 | 4 | $route->override = true; |
|
| 141 | 4 | return $route; |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Parameter default values indexed by parameter names. |
||
| 146 | * |
||
| 147 | * @psalm-param array<string,null|Stringable|scalar> $defaults |
||
| 148 | */ |
||
| 149 | 5 | public function defaults(array $defaults): self |
|
| 150 | { |
||
| 151 | 5 | $route = clone $this; |
|
| 152 | 5 | $route->defaults = array_map('\strval', $defaults); |
|
| 153 | 5 | return $route; |
|
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Appends a handler middleware definition that should be invoked for a matched route. |
||
| 158 | * First added handler will be executed first. |
||
| 159 | */ |
||
| 160 | 28 | public function middleware(array|callable|string ...$definition): self |
|
| 161 | { |
||
| 162 | 28 | if ($this->actionAdded) { |
|
| 163 | 1 | throw new RuntimeException('middleware() can not be used after action().'); |
|
| 164 | } |
||
| 165 | |||
| 166 | 27 | $route = clone $this; |
|
| 167 | 27 | array_push( |
|
| 168 | 27 | $route->middlewares, |
|
| 169 | 27 | ...array_values($definition) |
|
| 170 | 27 | ); |
|
| 171 | |||
| 172 | 27 | $route->enabledMiddlewaresCache = null; |
|
| 173 | |||
| 174 | 27 | return $route; |
|
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Prepends a handler middleware definition that should be invoked for a matched route. |
||
| 179 | * Last added handler will be executed first. |
||
| 180 | */ |
||
| 181 | 27 | public function prependMiddleware(array|callable|string ...$definition): self |
|
| 182 | { |
||
| 183 | 27 | if (!$this->actionAdded) { |
|
| 184 | 1 | throw new RuntimeException('prependMiddleware() can not be used before action().'); |
|
| 185 | } |
||
| 186 | |||
| 187 | 26 | $route = clone $this; |
|
| 188 | 26 | array_unshift( |
|
| 189 | 26 | $route->middlewares, |
|
| 190 | 26 | ...array_values($definition) |
|
| 191 | 26 | ); |
|
| 192 | |||
| 193 | 26 | $route->enabledMiddlewaresCache = null; |
|
| 194 | |||
| 195 | 26 | return $route; |
|
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Appends action handler. It is a primary middleware definition that should be invoked last for a matched route. |
||
| 200 | */ |
||
| 201 | 30 | public function action(array|callable|string $middlewareDefinition): self |
|
| 202 | { |
||
| 203 | 30 | $route = clone $this; |
|
| 204 | 30 | $route->middlewares[] = $middlewareDefinition; |
|
| 205 | 30 | $route->actionAdded = true; |
|
| 206 | 30 | return $route; |
|
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Excludes middleware from being invoked when action is handled. |
||
| 211 | * It is useful to avoid invoking one of the parent group middleware for |
||
| 212 | * a certain route. |
||
| 213 | */ |
||
| 214 | 6 | public function disableMiddleware(mixed ...$definition): self |
|
| 215 | { |
||
| 216 | 6 | $route = clone $this; |
|
| 217 | 6 | array_push( |
|
| 218 | 6 | $route->disabledMiddlewares, |
|
| 219 | 6 | ...array_values($definition) |
|
| 220 | 6 | ); |
|
| 221 | |||
| 222 | 6 | $route->enabledMiddlewaresCache = null; |
|
| 223 | |||
| 224 | 6 | return $route; |
|
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @psalm-template T as string |
||
| 229 | * |
||
| 230 | * @psalm-param T $key |
||
| 231 | * |
||
| 232 | * @psalm-return ( |
||
| 233 | * T is ('name'|'pattern') ? string : |
||
| 234 | * (T is 'host' ? string|null : |
||
| 235 | * (T is 'hosts' ? array<array-key, string> : |
||
| 236 | * (T is 'methods' ? array<array-key,string> : |
||
| 237 | * (T is 'defaults' ? array<string,string> : |
||
| 238 | * (T is ('override'|'hasMiddlewares') ? bool : |
||
| 239 | * (T is 'enabledMiddlewares' ? array<array-key,array|callable|string> : mixed) |
||
| 240 | * ) |
||
| 241 | * ) |
||
| 242 | * ) |
||
| 243 | * ) |
||
| 244 | * ) |
||
| 245 | * ) |
||
| 246 | */ |
||
| 247 | 67 | public function getData(string $key): mixed |
|
| 248 | { |
||
| 249 | 67 | return match ($key) { |
|
| 250 | 67 | 'name' => $this->name ?? |
|
| 251 | 36 | (implode(', ', $this->methods) . ' ' . implode('|', $this->hosts) . $this->pattern), |
|
| 252 | 67 | 'pattern' => $this->pattern, |
|
| 253 | 67 | 'host' => $this->hosts[0] ?? null, |
|
| 254 | 67 | 'hosts' => $this->hosts, |
|
| 255 | 67 | 'methods' => $this->methods, |
|
| 256 | 67 | 'defaults' => $this->defaults, |
|
| 257 | 67 | 'override' => $this->override, |
|
| 258 | 67 | 'hasMiddlewares' => $this->middlewares !== [], |
|
| 259 | 67 | 'enabledMiddlewares' => $this->getEnabledMiddlewares(), |
|
| 260 | 67 | default => throw new InvalidArgumentException('Unknown data key: ' . $key), |
|
| 261 | 67 | }; |
|
| 262 | } |
||
| 263 | |||
| 264 | 5 | public function __toString(): string |
|
| 265 | { |
||
| 266 | 5 | $result = $this->name === null |
|
| 267 | 2 | ? '' |
|
| 268 | 3 | : '[' . $this->name . '] '; |
|
| 269 | |||
| 270 | 5 | if ($this->methods !== []) { |
|
| 271 | 5 | $result .= implode(',', $this->methods) . ' '; |
|
| 272 | } |
||
| 273 | |||
| 274 | 5 | if ($this->hosts) { |
|
|
0 ignored issues
–
show
|
|||
| 275 | 2 | $quoted = array_map(static fn ($host) => preg_quote($host, '/'), $this->hosts); |
|
| 276 | |||
| 277 | 2 | if (!preg_match('/' . implode('|', $quoted) . '/', $this->pattern)) { |
|
| 278 | 2 | $result .= implode('|', $this->hosts); |
|
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | 5 | $result .= $this->pattern; |
|
| 283 | |||
| 284 | 5 | return $result; |
|
| 285 | } |
||
| 286 | |||
| 287 | 3 | public function __debugInfo() |
|
| 288 | { |
||
| 289 | 3 | return [ |
|
| 290 | 3 | 'name' => $this->name, |
|
| 291 | 3 | 'methods' => $this->methods, |
|
| 292 | 3 | 'pattern' => $this->pattern, |
|
| 293 | 3 | 'hosts' => $this->hosts, |
|
| 294 | 3 | 'defaults' => $this->defaults, |
|
| 295 | 3 | 'override' => $this->override, |
|
| 296 | 3 | 'actionAdded' => $this->actionAdded, |
|
| 297 | 3 | 'middlewares' => $this->middlewares, |
|
| 298 | 3 | 'disabledMiddlewares' => $this->disabledMiddlewares, |
|
| 299 | 3 | 'enabledMiddlewares' => $this->getEnabledMiddlewares(), |
|
| 300 | 3 | ]; |
|
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @return array[]|callable[]|string[] |
||
| 305 | * @psalm-return list<array|callable|string> |
||
| 306 | */ |
||
| 307 | 30 | private function getEnabledMiddlewares(): array |
|
| 308 | { |
||
| 309 | 30 | if ($this->enabledMiddlewaresCache !== null) { |
|
| 310 | 1 | return $this->enabledMiddlewaresCache; |
|
| 311 | } |
||
| 312 | |||
| 313 | 30 | $this->enabledMiddlewaresCache = MiddlewareFilter::filter($this->middlewares, $this->disabledMiddlewares); |
|
| 314 | |||
| 315 | 30 | return $this->enabledMiddlewaresCache; |
|
|
0 ignored issues
–
show
|
|||
| 316 | } |
||
| 317 | } |
||
| 318 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.