← All changes
|
vendor/wpfluent/framework/src/WPFluent/Http/Router.php
+101
-6
1.32
→
2.1.0
View file →
| @@ -1,8 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentBoards\Framework\Http; |
| 4 | 4 | |
| 5 | +use Closure; | |
| 6 | + | |
| 5 | 7 | class Router |
| 6 | 8 | { |
| 7 | 9 | /** |
| 8 | 10 | * Application Instance |
| @@ -8,8 +10,20 @@ | ||
| 8 | 10 | * Application Instance |
| 9 | 11 | * @var \FluentBoards\Framework\Foundation\Application |
| 10 | 12 | */ |
| 11 | 13 | protected $app = null; |
| 14 | + | |
| 15 | + /** | |
| 16 | + * Name for the route. | |
| 17 | + * @var array | |
| 18 | + */ | |
| 19 | + protected $name = []; | |
| 20 | + | |
| 21 | + /** | |
| 22 | + * Mapping of named routes. | |
| 23 | + * @var array | |
| 24 | + */ | |
| 25 | + protected $namedRoutes = []; | |
| 12 | 26 | |
| 13 | 27 | /** |
| 14 | 28 | * Prefix for the route |
| 15 | 29 | * @var array |
| @@ -43,8 +57,14 @@ | ||
| 43 | 57 | 'after' => [] |
| 44 | 58 | ]; |
| 45 | 59 | |
| 46 | 60 | /** |
| 61 | + * Whether routes created by this router should override existing ones. | |
| 62 | + * @var bool | |
| 63 | + */ | |
| 64 | + protected $shouldOverride = false; | |
| 65 | + | |
| 66 | + /** | |
| 47 | 67 | * Keep the track of number of group calls |
| 48 | 68 | * @var integer |
| 49 | 69 | */ |
| 50 | 70 | protected $groupCount = 0; |
| @@ -60,20 +80,24 @@ | ||
| 60 | 80 | |
| 61 | 81 | /** |
| 62 | 82 | * Create a route group |
| 63 | 83 | * @param array $attributes |
| 64 | - * @param \Closure|null $callback | |
| 84 | + * @param Closure|null $callback | |
| 65 | 85 | * @return null |
| 66 | 86 | */ |
| 67 | - public function group($attributes = [], \Closure $callback = null) | |
| 87 | + public function group($attributes = [], ?Closure $callback = null) | |
| 68 | 88 | { |
| 69 | 89 | $this->groupCount += 1; |
| 70 | 90 | |
| 71 | - if ($attributes instanceof \Closure) { | |
| 91 | + if ($attributes instanceof Closure) { | |
| 72 | 92 | $callback = $attributes; |
| 73 | 93 | $attributes = []; |
| 74 | 94 | } |
| 75 | 95 | |
| 96 | + if (isset($attributes['name'])) { | |
| 97 | + $this->name($attributes['name']); | |
| 98 | + } | |
| 99 | + | |
| 76 | 100 | if (isset($attributes['prefix'])) { |
| 77 | 101 | $this->prefix($attributes['prefix']); |
| 78 | 102 | } |
| 79 | 103 | |
| @@ -127,8 +151,21 @@ | ||
| 127 | 151 | return new Group($this, $callback); |
| 128 | 152 | } |
| 129 | 153 | |
| 130 | 154 | /** |
| 155 | + * Set the route name | |
| 156 | + * | |
| 157 | + * @param string $name | |
| 158 | + * @return self | |
| 159 | + */ | |
| 160 | + public function name($name) | |
| 161 | + { | |
| 162 | + $this->name[] = $name; | |
| 163 | + | |
| 164 | + return $this; | |
| 165 | + } | |
| 166 | + | |
| 167 | + /** | |
| 131 | 168 | * Set the route prefix |
| 132 | 169 | * |
| 133 | 170 | * @param string $prefix |
| 134 | 171 | * @return self |
| @@ -142,9 +179,9 @@ | ||
| 142 | 179 | |
| 143 | 180 | /** |
| 144 | 181 | * Set the namespace for the action/controller |
| 145 | 182 | * |
| 146 | - * @param string $namespace | |
| 183 | + * @param string $ns | |
| 147 | 184 | * @return self |
| 148 | 185 | */ |
| 149 | 186 | public function namespace($ns) |
| 150 | 187 | { |
| @@ -153,8 +190,21 @@ | ||
| 153 | 190 | return $this; |
| 154 | 191 | } |
| 155 | 192 | |
| 156 | 193 | /** |
| 194 | + * Set the default route policy. | |
| 195 | + * | |
| 196 | + * @return self | |
| 197 | + */ | |
| 198 | + public function withDefaultPolicy() | |
| 199 | + { | |
| 200 | + return $this->withPolicy( | |
| 201 | + // @phpstan-ignore-next-line | |
| 202 | + $this->app->__namespace__.'\\App\\Http\\Policies\\Policy' | |
| 203 | + ); | |
| 204 | + } | |
| 205 | + | |
| 206 | + /** | |
| 157 | 207 | * Set the route policy |
| 158 | 208 | * |
| 159 | 209 | * @param mixed $handler |
| 160 | 210 | * @param string|null $method |
| @@ -221,8 +271,9 @@ | ||
| 221 | 271 | public function executeGroupCallback($callback) |
| 222 | 272 | { |
| 223 | 273 | $callback($this); |
| 224 | 274 | $this->groupCount -= 1; |
| 275 | + array_pop($this->name); | |
| 225 | 276 | array_pop($this->prefix); |
| 226 | 277 | array_pop($this->namespace); |
| 227 | 278 | array_pop($this->middleware['before']); |
| 228 | 279 | array_pop($this->middleware['after']); |
| @@ -335,8 +386,12 @@ | ||
| 335 | 386 | $handler, |
| 336 | 387 | $method |
| 337 | 388 | ); |
| 338 | 389 | |
| 390 | + if ($this->name) { | |
| 391 | + $route->withName($this->name); | |
| 392 | + } | |
| 393 | + | |
| 339 | 394 | if ($this->namespace) { |
| 340 | 395 | $route->withNamespace($this->namespace); |
| 341 | 396 | } |
| 342 | 397 | |
| @@ -351,9 +406,13 @@ | ||
| 351 | 406 | if ($this->middleware['after']) { |
| 352 | 407 | $route->after($this->middleware['after']); |
| 353 | 408 | } |
| 354 | 409 | |
| 355 | - return $route; | |
| 410 | + if ($this->shouldOverride) { | |
| 411 | + $route->override(); | |
| 412 | + } | |
| 413 | + | |
| 414 | + return $route->preparefrontendHandlers(); | |
| 356 | 415 | } |
| 357 | 416 | |
| 358 | 417 | /** |
| 359 | 418 | * Resolve the rest namespace for the plugin |
| @@ -390,10 +449,22 @@ | ||
| 390 | 449 | return trim($prefix, '/') . '/' . trim($uri, '/'); |
| 391 | 450 | } |
| 392 | 451 | |
| 393 | 452 | /** |
| 453 | + * Mark all routes created by this router to override existing ones. | |
| 454 | + * | |
| 455 | + * @return $this | |
| 456 | + */ | |
| 457 | + public function overrideExisting() | |
| 458 | + { | |
| 459 | + $this->shouldOverride = true; | |
| 460 | + | |
| 461 | + return $this; | |
| 462 | + } | |
| 463 | + | |
| 464 | + /** | |
| 394 | 465 | * Register all the routse in WordPress Rest Engine |
| 395 | - * | |
| 466 | + * | |
| 396 | 467 | * @return null |
| 397 | 468 | */ |
| 398 | 469 | public function registerRoutes() |
| 399 | 470 | { |
| @@ -406,6 +477,30 @@ | ||
| 406 | 477 | */ |
| 407 | 478 | public function getRoutes() |
| 408 | 479 | { |
| 409 | 480 | return $this->routes; |
| 481 | + } | |
| 482 | + | |
| 483 | + /** | |
| 484 | + * Set a named route in the router. | |
| 485 | + * | |
| 486 | + * @param string $name | |
| 487 | + * @param Route $route | |
| 488 | + */ | |
| 489 | + public function setNamedRoute($name, Route $route) | |
| 490 | + { | |
| 491 | + $this->namedRoutes[$name] = $route; | |
| 492 | + | |
| 493 | + return $route; | |
| 494 | + } | |
| 495 | + | |
| 496 | + /** | |
| 497 | + * Get a route by name. | |
| 498 | + * | |
| 499 | + * @param string $name | |
| 500 | + * @return Route|null | |
| 501 | + */ | |
| 502 | + public function getByName($name) | |
| 503 | + { | |
| 504 | + return $this->namedRoutes[$name] ?? null; | |
| 410 | 505 | } |
| 411 | 506 | } |