PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Event » Event_Dispatcher » Bug #7459

Add support for filtering notifications by class hierarchy rather than name

Details

Request #7459Add support for filtering notifications by class hierarchy rather than name
Submitted2006-04-22 18:10 UTC
Fromjared at intuitivefuture dot com
StatusOpen
PackageEvent_Dispatcher
PHP VersionIrrelevant
OSMac OS X 10.4
Roadmaps(Not assigned)

Comments

[2006-04-22 18:10 UTC] jared at intuitivefuture dot com

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();
}