diff --git a/src/EventManager.php b/src/EventManager.php index 0db1098..e3cf290 100644 --- a/src/EventManager.php +++ b/src/EventManager.php @@ -434,41 +434,21 @@ protected function triggerListeners($event, EventDescription $e, $callback = nul // Add static/wildcard listeners to the list of listeners, // but don't modify the listeners object - $staticListeners = $this->getStaticListeners($event); - $wildcardListeners = $this->getListeners('*'); - if (count($staticListeners) || count($wildcardListeners)) { + $staticListeners = $this->getStaticListeners($event); + $staticWildcardListeners = $this->getStaticListeners('*'); + $wildcardListeners = $this->getListeners('*'); + if (count($staticListeners) || count($staticWildcardListeners) || count($wildcardListeners)) { $listeners = clone $listeners; } - // Add static listeners - if (count($staticListeners)) { - foreach ($staticListeners as $listener) { - $priority = $listener->getMetadatum('priority'); - if (null === $priority) { - $priority = 1; - } elseif (is_array($priority)) { - // If we have an array, likely using PriorityQueue. Grab first - // element of the array, as that's the actual priority. - $priority = array_shift($priority); - } - $listeners->insert($listener, $priority); - } - } + // Static listeners on this specific event + $this->insertListeners($listeners, $staticListeners); + + // Static wildcard listeners + $this->insertListeners($listeners, $staticWildcardListeners); // Add wildcard listeners - if (count($wildcardListeners)) { - foreach ($wildcardListeners as $listener) { - $priority = $listener->getMetadatum('priority'); - if (null === $priority) { - $priority = 1; - } elseif (is_array($priority)) { - // If we have an array, likely using PriorityQueue. Grab first - // element of the array, as that's the actual priority. - $priority = array_shift($priority); - } - $listeners->insert($listener, $priority); - } - } + $this->insertListeners($listeners, $wildcardListeners); if ($listeners->isEmpty()) { return $responses; @@ -531,4 +511,32 @@ protected function getStaticListeners($event) return $staticListeners; } + + /** + * Add listeners to the master queue of listeners + * + * Used to inject static listeners and wildcard listeners. + * + * @param PriorityQueue $masterListeners + * @param PriorityQueue $listeners + * @return void + */ + protected function insertListeners($masterListeners, $listeners) + { + if (!count($listeners)) { + return; + } + + foreach ($listeners as $listener) { + $priority = $listener->getMetadatum('priority'); + if (null === $priority) { + $priority = 1; + } elseif (is_array($priority)) { + // If we have an array, likely using PriorityQueue. Grab first + // element of the array, as that's the actual priority. + $priority = array_shift($priority); + } + $masterListeners->insert($listener, $priority); + } + } } diff --git a/test/StaticEventManagerTest.php b/test/StaticEventManagerTest.php index 4489e93..140f191 100644 --- a/test/StaticEventManagerTest.php +++ b/test/StaticEventManagerTest.php @@ -20,9 +20,11 @@ */ namespace ZendTest\EventManager; -use Zend\EventManager\StaticEventManager, + +use PHPUnit_Framework_TestCase as TestCase, + stdClass, Zend\EventManager\EventManager, - PHPUnit_Framework_TestCase as TestCase; + Zend\EventManager\StaticEventManager; /** * @category Zend @@ -151,6 +153,25 @@ public function testCanAttachCallbackToMultipleEventsOnMultipleResourcesAtOnce() } } + public function testListenersAttachedUsingWildcardEventWillBeTriggeredByResource() + { + $test = new stdClass; + $test->events = array(); + $callback = function($e) use ($test) { + $test->events[] = $e->getName(); + }; + + $staticEvents = StaticEventManager::getInstance(); + $staticEvents->attach('bar', '*', $callback); + + $events = new EventManager('bar'); + + foreach (array('foo', 'bar', 'baz') as $event) { + $events->trigger($event); + $this->assertContains($event, $test->events); + } + } + public function testCanDetachListenerFromResource() { $events = StaticEventManager::getInstance();