From 203488bd4a16da8e8d6dab1aca009e6243067a8f Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 7 Jul 2023 11:37:52 -0400 Subject: [PATCH] Add support for conditional hooks that match by argument type. More details here: https://processwire.com/docs/modules/hooks/#conditional-hooks-by-type --- wire/core/WireHooks.php | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/wire/core/WireHooks.php b/wire/core/WireHooks.php index 3993c2c9..45e959ff 100644 --- a/wire/core/WireHooks.php +++ b/wire/core/WireHooks.php @@ -5,7 +5,7 @@ * * This class is for internal use. You should manipulate hooks from Wire-derived classes instead. * - * ProcessWire 3.x, Copyright 2022 by Ryan Cramer + * ProcessWire 3.x, Copyright 2023 by Ryan Cramer * https://processwire.com * */ @@ -173,6 +173,22 @@ class WireHooks { */ protected $wire; + /** + * Conditional argument match types and the PHP function to detect them + * + * @var string[] + * + */ + protected $argMatchTypes = array( + 'array' => 'is_array', + 'bool' => 'is_bool', + 'float' => 'is_float', + 'int' => 'is_int', + 'null' => 'is_null', + 'object' => 'is_object', + 'string' => 'is_string', + ); + /** * Construct WireHooks * @@ -992,6 +1008,24 @@ class WireHooks { // we don't work with non-object here $matches = false; } + } else if(is_string($argMatch) && strpos($argMatch, '<') === 0 && substr($argMatch, -1) === '>') { + // i.e. , , , , , etc. + $argMatch = trim($argMatch, '<>'); + if(strpos($argMatch, '|')) { + // i.e. or etc. + $argMatches = explode('|', str_replace(array('<', '>'), '', $argMatch)); + } else { + $argMatches = array($argMatch); + } + foreach($argMatches as $argMatchType) { + if(isset($this->argMatchTypes[$argMatchType])) { + $argMatchFunc = $this->argMatchTypes[$argMatchType]; + $matches = $argMatchFunc($argVal); + } else { + $matches = wireInstanceOf($argVal, $argMatchType); + } + if($matches) break; + } } else { if(is_array($argVal)) { // match any array element