PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.10.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.10.0
2.11.0 2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 All 78 releases
fluent-community / vendor / wpfluent / framework / src / WPFluent / Http / Group.php

Group.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.10.0, at vendor/wpfluent/framework/src/WPFluent/Http/Group.php

88 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\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