From a48f4038c83c8cda00377a9bb86301fd0ce753e7 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Tue, 17 Oct 2017 10:00:55 -0400 Subject: [PATCH] Enhancement processwire/processwire-issues#405 add support for hooks to be used in classes that extend Wire-derived classes that aren't in the ProcessWire namespace. --- wire/core/WireHooks.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/wire/core/WireHooks.php b/wire/core/WireHooks.php index 9e1b0fd9..4fb25a3d 100644 --- a/wire/core/WireHooks.php +++ b/wire/core/WireHooks.php @@ -152,6 +152,7 @@ class WireHooks { // see if we can do a quick exit if($method && $method !== '*' && !$this->isHookedOrParents($object, $method)) return $hooks; + // first determine which local hooks when should include if($type !== self::getHooksStatic) { @@ -174,6 +175,7 @@ class WireHooks { $needSort = false; $namespace = __NAMESPACE__ ? __NAMESPACE__ . "\\" : ""; + $objectParentNamespaces = array(); // join in static hooks foreach($this->staticHooks as $className => $staticHooks) { @@ -184,7 +186,21 @@ class WireHooks { // objects in other namespaces $_className = $_namespace . $className; if(!$object instanceof $_className && $method !== '*') { - continue; + // object likely extends a class not in PW namespace, so check class parents instead + if(empty($objectParentNamespaces)) { + foreach(wireClassParents($object) as $nscn => $cn) { + list($ns,) = explode("\\", $nscn); + $objectParentNamespaces[$ns] = $ns; + } + } + $nsok = false; + foreach($objectParentNamespaces as $ns) { + $_className = "$ns\\$className"; + if(!$object instanceof $_className) continue; + $nsok = true; + break; + } + if(!$nsok) continue; } } else { continue;