From 9eb73ed295fa88597b1c4493f9a61d9891e4bb95 Mon Sep 17 00:00:00 2001 From: Jared Hancock <jared@osticket.com> Date: Wed, 5 Jun 2013 11:57:39 -0500 Subject: [PATCH] Add a check (predicate) callable to the signal connect This allows another signal condition to the connection, so that, in addition to the (sub) class, another check can be made before the signal is delivered to the connected callable --- include/class.signal.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/include/class.signal.php b/include/class.signal.php index 86540054e..ac7a6a638 100644 --- a/include/class.signal.php +++ b/include/class.signal.php @@ -46,18 +46,27 @@ class Signal { * Optionally, if $object is a class and is passed into the ::connect() * method, only instances of the named class or subclass will actually * be connected to the callable function. + * + * A predicate function, $check, can be used to filter calls to the + * signal handler. The function will receive the signal data and should + * return true if the signal handler should be called. */ - /*static*/ function connect($signal, $callable, $object=null) { + /*static*/ function connect($signal, $callable, $object=null, + $check=null) { global $_subscribers; if (!isset($_subscribers[$signal])) $_subscribers[$signal] = array(); // XXX: Ensure $object if set is a class if ($object && !is_string($object)) trigger_error("Invalid object: $object: Expected class"); - $_subscribers[$signal][] = array($object, $callable); + elseif ($check && !is_callable($check)) { + trigger_error("Invalid check function: Must be callable"); + $check = null; + } + $_subscribers[$signal][] = array($object, $callable, $check); } /** - * Publish a signal. + * Publish a signal. * * Signal::send('user.login', $this, array('username'=>'blah')); * @@ -81,9 +90,11 @@ class Signal { if (!isset($_subscribers[$signal])) return; foreach ($_subscribers[$signal] as $sub) { - list($s, $callable) = $sub; + list($s, $callable, $check) = $sub; if ($s && !is_a($object, $s)) continue; + elseif ($check && !call_user_func($check, $data)) + continue; call_user_func($callable, $data); } } -- GitLab