Skip to content

Commit 05c489b

Browse files
Run equal-priority hooks from different classes in the order added
When static hooks from different classes for the same method had equal priority (i.e. the default 100), getHooks() merged them class by class and the class merged last took the base priority key. The run order of such hooks therefore depended only on which class first had any static hook. For example, an after hook to User::viewable added from ready.php could run before PagePermissions' Page::viewable hook that provides the viewable() method, so it saw a null return value and its result was then overwritten. Each hook now has a sequence number assigned when added. When hooks from multiple sources are merged, they are sorted by priority, then (for equal priority) static hooks before local hooks as before, then in the order added. Ordering of hooks within a single class and method is unchanged. Adds WireHooks tests for a subclass after hook to a hook-provided method and for equal-priority ordering between parent class, subclass and local hooks. Reported in processwire/processwire-issues#2011 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d4dbc13 commit 05c489b

2 files changed

Lines changed: 67 additions & 12 deletions

File tree

‎wire/core/WireHooks/WireHooks.php‎

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,17 @@ class WireHooks {
185185
*/
186186
protected $useGetHooksCache = false;
187187

188+
/**
189+
* Sequence number of the last added hook
190+
*
191+
* Used to run hooks of equal priority from different classes (or local and static) in the order added.
192+
*
193+
* @var int
194+
* @since 3.0.275
195+
*
196+
*/
197+
protected $hookSeq = 0;
198+
188199
/**
189200
* @var ProcessWire
190201
*
@@ -307,15 +318,12 @@ public function getHooks(Wire $object, $method = '', $getHooks = self::getHooksA
307318
// retrieve all static hooks for method
308319
if(!empty($staticHooks[$method])) {
309320
if(count($hooks)) {
310-
$collisions = array_intersect_key($hooks, $staticHooks[$method]);
311-
$hooks = array_merge($hooks, $staticHooks[$method]);
312-
if(count($collisions)) {
313-
// identify and resolve priority collisions
314-
foreach($collisions as $priority => $hook) {
315-
$n = 0;
316-
while(isset($hooks["$priority.$n"])) $n++;
317-
$hooks["$priority.$n"] = $hook;
318-
}
321+
foreach($staticHooks[$method] as $priority => $hook) {
322+
// resolve priority collisions with hooks from other classes or local hooks
323+
$key = $priority;
324+
$n = 0;
325+
while(isset($hooks[$key])) $key = $priority . '.' . ($n++);
326+
$hooks[$key] = $hook;
319327
}
320328
$needSort = true;
321329
} else {
@@ -335,9 +343,15 @@ public function getHooks(Wire $object, $method = '', $getHooks = self::getHooksA
335343
}
336344

337345
if($needSort && count($hooks) > 1) {
338-
defined("SORT_NATURAL") ? ksort($hooks, SORT_NATURAL) : uksort($hooks, "strnatcmp");
346+
// sort by priority, then for equal priority: static hooks before local hooks, then in the order added
347+
uasort($hooks, function($a, $b) {
348+
$n = strnatcmp((string) $a['options']['priority'], (string) $b['options']['priority']);
349+
if($n === 0) $n = (int) empty($a['options']['allInstances']) - (int) empty($b['options']['allInstances']);
350+
if($n === 0) $n = (isset($a['seq']) ? $a['seq'] : 0) - (isset($b['seq']) ? $b['seq'] : 0);
351+
return $n;
352+
});
339353
}
340-
354+
341355
return $hooks;
342356
}
343357

@@ -819,8 +833,9 @@ public function addHook(Wire $object, $method, $toObject, $toMethod = null, $opt
819833
'method' => $method,
820834
'toObject' => $toObject,
821835
'toMethod' => $toMethod,
822-
'toPublic' => $toPublic,
836+
'toPublic' => $toPublic,
823837
'options' => $options,
838+
'seq' => ++$this->hookSeq,
824839
);
825840
$hooks[$method][$priority] = $hook;
826841

‎wire/core/WireHooks/WireHooks.test.php‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function execute() {
2222
$this->testHookPropertiesAndMethods();
2323
$this->testStaticLocalAndRemoval();
2424
$this->testPriorityCustomDataAndCancel();
25+
$this->testEqualPriorityOrder();
2526
$this->testConditionalHooks();
2627
$this->testHookEventArguments();
2728
}
@@ -168,6 +169,42 @@ protected function testPriorityCustomDataAndCancel() {
168169
$this->check('cancelled after hook did not run', false, $cancelled);
169170
}
170171

172+
/**
173+
* Hooks of equal priority: static before local, and static hooks from different classes in the order added
174+
*
175+
*/
176+
protected function testEqualPriorityOrder() {
177+
$wire = $this->wire();
178+
$child = $this->wire(new WireHooksTestChildTarget());
179+
180+
// method provided by a hook to a parent class, then an after hook to it from a subclass
181+
$this->addTestHook($wire, 'addHook', 'WireHooksTestTarget::providedValue', function(HookEvent $event) {
182+
$event->return = 'provided';
183+
});
184+
$seen = array();
185+
$this->addTestHook($wire, 'addHookAfter', 'WireHooksTestChildTarget::providedValue', function(HookEvent $event) use(&$seen) {
186+
$seen[] = $event->return;
187+
$event->return .= ':child';
188+
});
189+
$value = $child->providedValue();
190+
$this->check('subclass after hook sees return value of hook-provided method', array('provided'), $seen);
191+
$this->check('subclass after hook modifies return value of hook-provided method', 'provided:child', $value);
192+
193+
// local and static hooks of equal priority (local added first)
194+
$target = $this->wire(new WireHooksTestTarget());
195+
$order = array();
196+
$this->addTestHook($target, 'addHookAfter', 'echoValue', function() use(&$order) { $order[] = 'local'; });
197+
$this->addTestHook($wire, 'addHookAfter', 'WireHooksTestTarget::echoValue', function() use(&$order) { $order[] = 'static'; });
198+
$this->addTestHook($wire, 'addHookAfter', 'WireHooksTestChildTarget::echoValue', function() use(&$order) { $order[] = 'child'; });
199+
$child2 = $this->wire(new WireHooksTestChildTarget());
200+
$this->addTestHook($child2, 'addHookAfter', 'echoValue', function() use(&$order) { $order[] = 'local2'; });
201+
$target->echoValue('x');
202+
$this->check('equal priority static hooks run before local hooks', array('static', 'local'), $order);
203+
$order = array();
204+
$child2->echoValue('x');
205+
$this->check('equal priority static hooks from parent class and subclass run in the order added, before local hooks', array('static', 'child', 'local2'), $order);
206+
}
207+
171208
protected function testConditionalHooks() {
172209
$ready = $this->wire(new WireHooksTestDataTarget());
173210
$ready->status = 'ready';
@@ -284,3 +321,6 @@ public function ___value($value) {
284321
return $value;
285322
}
286323
}
324+
325+
class WireHooksTestChildTarget extends WireHooksTestTarget {
326+
}

0 commit comments

Comments
 (0)