Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
[zendframework/zendframework#40] Added wildcard listener support to S…
Browse files Browse the repository at this point in the history
…taticEventManager

- EventManager now tests for static listeners on the given event, static
  wildcard listeners, and wildcard listeners
  • Loading branch information
weierophinney committed Mar 14, 2012
1 parent 7d6e2ab commit 5859e98
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 32 deletions.
68 changes: 38 additions & 30 deletions src/EventManager.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
25 changes: 23 additions & 2 deletions test/StaticEventManagerTest.php
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 5859e98

Please sign in to comment.