Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.1.0

Request #7459 Add support for filtering notifications by class hierarchy rather than name
Submitted: 2006-04-22 18:10 UTC
From: jared at intuitivefuture dot com Assigned:
Status: Open Package: Event_Dispatcher (version 1.0.0)
PHP Version: Irrelevant OS: Mac OS X 10.4
Roadmaps: (Not assigned)    
Subscription  


 [2006-04-22 18:10 UTC] jared at intuitivefuture dot com (Jared White)
Description: ------------ When using the third argument of addObserver to filter notifications based on class name, the problem is that only that single class name is used when checking the class that posted a notification. If a subclass of that class posts a notification, the notification will be filtered out, resulting in a 1:1 coupling of an observer and a single superclass rather than a coupling of an observer and a class hierarchy. So I changed the postNotification method slightly to check the class hierarchy so that if a superclass in the hierarchy matches the class name given via addObserver, it doesn't filter out the notification. The patch: --- Event/Dispatcher.php 2005-09-22 08:37:10.000000000 -0700 +++ DispatcherNEW.php 2006-04-22 11:03:56.000000000 -0700 @@ -267,6 +267,8 @@ $this->_pending[$nName][] =& $notification; } $objClass = get_class($notification- >getNotificationObject()); + $objClasses = array(strtolower($objClass)); + while($objClass = get_parent_class($objClass)) { $objClasses[] = strtolower($objClass); } // Find the registered observers if (isset($this->_ro[$nName])) { @@ -276,7 +278,7 @@ return $notification; } if (empty($rObserver['class']) || - strcasecmp($rObserver['class'], $objClass) == 0) { + array_search(strtolower($rObserver ['class']), $objClasses) !== false) { call_user_func_array($rObserver ['callback'], array(&$notification)); $notification- >increaseNotificationCount(); }

Comments