← All changes
|
vendor/wpfluent/framework/src/WPFluent/Http/Group.php
+62
-2
1.0.91
→
2.11.0
View file →
| @@ -1,27 +1,87 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentCommunity\Framework\Http; |
| 4 | 4 | |
| 5 | +/** | |
| 6 | + * Route group with deferred, destructor-driven registration. | |
| 7 | + * | |
| 8 | + * The group callback runs from __destruct, NOT from the constructor. | |
| 9 | + * This is a deliberate design decision: it lets the caller chain | |
| 10 | + * configuration onto the group before the callback executes: | |
| 11 | + * | |
| 12 | + * $router->group($attrs, $cb)->withPolicy(...); | |
| 13 | + * // The callback runs here — when the statement's temporary is | |
| 14 | + * // destroyed, after the whole chain has been applied. | |
| 15 | + * | |
| 16 | + * The group owns its attributes. It captures the enclosing group's | |
| 17 | + * attributes plus its own at creation time, and every call chained | |
| 18 | + * after group() is absorbed into that set. When the callback finally | |
| 19 | + * runs, the router activates exactly this set as the context, then | |
| 20 | + * restores the previous one. So the routes a group declares come | |
| 21 | + * out the same whether the callback runs at end of statement or | |
| 22 | + * later from registerRoutes(), at any nesting depth, and whether or | |
| 23 | + * not a sibling group was executed in between. | |
| 24 | + * | |
| 25 | + * INVARIANT: the caller's expression should stay the only strong | |
| 26 | + * reference to a Group. A second strong reference — or a reference | |
| 27 | + * cycle — defers destruction to PHP's cyclic GC, whose timing varies | |
| 28 | + * per host (zend.enable_gc, loaded extensions, allocation churn). | |
| 29 | + * Two safety nets back the invariant: the router tracks every group | |
| 30 | + * as a WeakReference (so tracking never extends a group's lifetime), | |
| 31 | + * and registerRoutes() flushes any still-pending group before routes | |
| 32 | + * are handed to WordPress, while the $executed flag keeps execute() | |
| 33 | + * idempotent so the destructor and the flush can never run a | |
| 34 | + * callback twice (see the fluentform 6.2.x incident). | |
| 35 | + * | |
| 36 | + * Do NOT "simplify" this by executing the callback in the | |
| 37 | + * constructor: the constructor runs before the chain, so every | |
| 38 | + * chained configuration would be silently ignored. | |
| 39 | + */ | |
| 5 | 40 | class Group |
| 6 | 41 | { |
| 7 | 42 | protected $router = null; |
| 8 | 43 | protected $callback = null; |
| 44 | + protected $attributes = []; | |
| 45 | + protected $executed = false; | |
| 9 | 46 | |
| 10 | - public function __construct($router, $callback) | |
| 47 | + public function __construct($router, $callback, array $attributes = []) | |
| 11 | 48 | { |
| 12 | 49 | $this->router = $router; |
| 13 | 50 | $this->callback = $callback; |
| 51 | + $this->attributes = $attributes; | |
| 52 | + $this->router->trackGroup($this); | |
| 14 | 53 | } |
| 15 | 54 | |
| 55 | + /** | |
| 56 | + * Forward chained configuration to the router, then claim | |
| 57 | + * whatever it staged as this group's own attributes. | |
| 58 | + */ | |
| 16 | 59 | public function __call($method, $params) |
| 17 | 60 | { |
| 18 | 61 | $this->router->{$method}(...$params); |
| 19 | 62 | |
| 63 | + $this->attributes = $this->router->absorbStaged($this->attributes); | |
| 64 | + | |
| 20 | 65 | return $this; |
| 21 | 66 | } |
| 22 | 67 | |
| 68 | + public function getAttributes() | |
| 69 | + { | |
| 70 | + return $this->attributes; | |
| 71 | + } | |
| 72 | + | |
| 73 | + public function execute() | |
| 74 | + { | |
| 75 | + if (!$this->executed) { | |
| 76 | + $this->executed = true; | |
| 77 | + $this->router->executeGroupCallback( | |
| 78 | + $this->callback, $this->attributes | |
| 79 | + ); | |
| 80 | + } | |
| 81 | + } | |
| 82 | + | |
| 23 | 83 | public function __destruct() |
| 24 | 84 | { |
| 25 | - $this->router->executeGroupCallback($this->callback); | |
| 85 | + $this->execute(); | |
| 26 | 86 | } |
| 27 | 87 | } |