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

Commit

Permalink
Allow EventManagers to propagate on multiple static ids
Browse files Browse the repository at this point in the history
- When triggering static connections, iterate through each ID to which the
  manager is broadcasting.
  • Loading branch information
weierophinney committed Feb 1, 2011
1 parent d71e4d1 commit f10c77c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/EventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,20 +354,26 @@ protected function triggerStaticHandlers($callback, Event $event, ResponseCollec
if (!$staticConnections = $this->getStaticConnections()) {
return $responses;
}
if (!$handlers = $staticConnections->getHandlers($this->identifier, $event->getName())) {
return $responses;
}
foreach ($handlers as $handler) {
$responses->push(call_user_func($handler->getCallback(), $event));
if ($event->propagationIsStopped()) {
$responses->setStopped(true);
break;

$identifiers = (array) $this->identifier;

foreach ($identifiers as $id) {
if (!$handlers = $staticConnections->getHandlers($id, $event->getName())) {
continue;
}
if (call_user_func($callback, $responses->last())) {
$responses->setStopped(true);
break;
foreach ($handlers as $handler) {
$responses->push(call_user_func($handler->getCallback(), $event));
if ($event->propagationIsStopped()) {
$responses->setStopped(true);
break;
}
if (call_user_func($callback, $responses->last())) {
$responses->setStopped(true);
break;
}
}
}

return $responses;
}
}
24 changes: 24 additions & 0 deletions test/StaticEventManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

namespace ZendTest\EventManager;
use Zend\EventManager\StaticEventManager,
Zend\EventManager\EventManager,
PHPUnit_Framework_TestCase as TestCase;

/**
Expand Down Expand Up @@ -156,4 +157,27 @@ public function testCanClearHandlersByResourceAndEvent()
$this->assertInstanceOf('Zend\Stdlib\PriorityQueue', $events->getHandlers('foo', 'bat'));
$this->assertEquals(1, count($events->getHandlers('foo', 'bat')));
}

public function testCanPassArrayOfIdentifiersToConstructor()
{
$identifiers = array('foo', 'bar');
$manager = new EventManager($identifiers);
}

public function testHandlersAttachedToAnyIdentifierProvidedToEventManagerWillBeTriggered()
{
$identifiers = array('foo', 'bar');
$manager = new EventManager($identifiers);
$events = StaticEventManager::getInstance();
$test = new \stdClass;
$test->triggered = 0;
$events->attach('foo', 'bar', function($e) use ($test) {
$test->triggered++;
});
$events->attach('bar', 'bar', function($e) use ($test) {
$test->triggered++;
});
$manager->trigger('bar', $this, array());
$this->assertEquals(2, $test->triggered);
}
}

0 comments on commit f10c77c

Please sign in to comment.