← All changes
|
vendor/wpfluent/framework/src/WPFluent/Http/Router.php
+67
-3
1.3.25
→
1.6.5
View file →
| @@ -57,8 +57,14 @@ | ||
| 57 | 57 | 'after' => [] |
| 58 | 58 | ]; |
| 59 | 59 | |
| 60 | 60 | /** |
| 61 | + * Whether routes created by this router should override existing ones. | |
| 62 | + * @var bool | |
| 63 | + */ | |
| 64 | + protected $shouldOverride = false; | |
| 65 | + | |
| 66 | + /** | |
| 61 | 67 | * Keep the track of number of group calls |
| 62 | 68 | * @var integer |
| 63 | 69 | */ |
| 64 | 70 | protected $groupCount = 0; |
| @@ -63,8 +69,14 @@ | ||
| 63 | 69 | */ |
| 64 | 70 | protected $groupCount = 0; |
| 65 | 71 | |
| 66 | 72 | /** |
| 73 | + * Weak references to groups pending execution. | |
| 74 | + * @var array | |
| 75 | + */ | |
| 76 | + protected $pendingGroups = []; | |
| 77 | + | |
| 78 | + /** | |
| 67 | 79 | * Construct the routet instance |
| 68 | 80 | * @param \FluentCart\Framework\Foundation\Application $app |
| 69 | 81 | */ |
| 70 | 82 | public function __construct($app) |
| @@ -256,10 +268,42 @@ | ||
| 256 | 268 | return $this; |
| 257 | 269 | } |
| 258 | 270 | |
| 259 | 271 | /** |
| 272 | + * Track a group so any instance kept alive past its statement | |
| 273 | + * can still be executed before routes are registered. A weak | |
| 274 | + * reference keeps the destructor firing at end of statement. | |
| 275 | + * | |
| 276 | + * @param Group $group | |
| 277 | + * @return null | |
| 278 | + */ | |
| 279 | + public function trackGroup(Group $group) | |
| 280 | + { | |
| 281 | + if (class_exists(\WeakReference::class)) { | |
| 282 | + $this->pendingGroups[] = \WeakReference::create($group); | |
| 283 | + } | |
| 284 | + } | |
| 285 | + | |
| 286 | + /** | |
| 287 | + * Execute any groups still pending execution because a | |
| 288 | + * reference to them was held beyond their statement. | |
| 289 | + * | |
| 290 | + * @return null | |
| 291 | + */ | |
| 292 | + protected function executePendingGroups() | |
| 293 | + { | |
| 294 | + while ($this->pendingGroups) { | |
| 295 | + $reference = array_shift($this->pendingGroups); | |
| 296 | + | |
| 297 | + if ($group = $reference->get()) { | |
| 298 | + $group->execute(); | |
| 299 | + } | |
| 300 | + } | |
| 301 | + } | |
| 302 | + | |
| 303 | + /** | |
| 260 | 304 | * Execute the route group callback |
| 261 | - * | |
| 305 | + * | |
| 262 | 306 | * @param Closure $callback |
| 263 | 307 | * @return null |
| 264 | 308 | */ |
| 265 | 309 | public function executeGroupCallback($callback) |
| @@ -400,8 +444,12 @@ | ||
| 400 | 444 | if ($this->middleware['after']) { |
| 401 | 445 | $route->after($this->middleware['after']); |
| 402 | 446 | } |
| 403 | 447 | |
| 448 | + if ($this->shouldOverride) { | |
| 449 | + $route->override(); | |
| 450 | + } | |
| 451 | + | |
| 404 | 452 | return $route->preparefrontendHandlers(); |
| 405 | 453 | } |
| 406 | 454 | |
| 407 | 455 | /** |
| @@ -439,15 +487,31 @@ | ||
| 439 | 487 | return trim($prefix, '/') . '/' . trim($uri, '/'); |
| 440 | 488 | } |
| 441 | 489 | |
| 442 | 490 | /** |
| 491 | + * Mark all routes created by this router to override existing ones. | |
| 492 | + * | |
| 493 | + * @return $this | |
| 494 | + */ | |
| 495 | + public function overrideExisting() | |
| 496 | + { | |
| 497 | + $this->shouldOverride = true; | |
| 498 | + | |
| 499 | + return $this; | |
| 500 | + } | |
| 501 | + | |
| 502 | + /** | |
| 443 | 503 | * Register all the routse in WordPress Rest Engine |
| 444 | - * | |
| 504 | + * | |
| 445 | 505 | * @return null |
| 446 | 506 | */ |
| 447 | 507 | public function registerRoutes() |
| 448 | 508 | { |
| 449 | - foreach ($this->routes as $route) $route->register(); | |
| 509 | + $this->executePendingGroups(); | |
| 510 | + | |
| 511 | + foreach ($this->getRoutes() as $route) { | |
| 512 | + $route->register(); | |
| 513 | + } | |
| 450 | 514 | } |
| 451 | 515 | |
| 452 | 516 | /** |
| 453 | 517 | * Get all ther registered routes |