| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\Framework\Http; |
| 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 |
*/ |
| 40 |
class Group |
| 41 |
{ |
| 42 |
protected $router = null; |
| 43 |
protected $callback = null; |
| 44 |
protected $attributes = []; |
| 45 |
protected $executed = false; |
| 46 |
|
| 47 |
public function __construct($router, $callback, array $attributes = []) |
| 48 |
{ |
| 49 |
$this->router = $router; |
| 50 |
$this->callback = $callback; |
| 51 |
$this->attributes = $attributes; |
| 52 |
$this->router->trackGroup($this); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Forward chained configuration to the router, then claim |
| 57 |
* whatever it staged as this group's own attributes. |
| 58 |
*/ |
| 59 |
public function __call($method, $params) |
| 60 |
{ |
| 61 |
$this->router->{$method}(...$params); |
| 62 |
|
| 63 |
$this->attributes = $this->router->absorbStaged($this->attributes); |
| 64 |
|
| 65 |
return $this; |
| 66 |
} |
| 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 |
|
| 83 |
public function __destruct() |
| 84 |
{ |
| 85 |
$this->execute(); |
| 86 |
} |
| 87 |
} |
| 88 |
|