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

Commit

Permalink
Merge branch 'master' of git://github.com/zendframework/zf2
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
16 changes: 14 additions & 2 deletions src/EventManager.php
Expand Up @@ -157,7 +157,7 @@ public function setIdentifiers($identifiers)
public function addIdentifiers($identifiers)
{
if (is_array($identifiers) || $identifiers instanceof Traversable) {
$this->identifiers = array_unique($this->identifiers + (array) $identifiers);
$this->identifiers = array_unique(array_merge($this->identifiers, (array) $identifiers));
} elseif ($identifiers !== null) {
$this->identifiers = array_unique(array_merge($this->identifiers, array($identifiers)));
}
Expand Down Expand Up @@ -201,6 +201,9 @@ public function trigger($event, $target = null, $argv = array(), $callback = nul
throw new Exception\InvalidCallbackException('Invalid callback provided');
}

// Initial value of stop propagation flag should be false
$e->stopPropagation(false);

return $this->triggerListeners($event, $e, $callback);
}

Expand Down Expand Up @@ -243,6 +246,9 @@ public function triggerUntil($event, $target, $argv = null, $callback = null)
throw new Exception\InvalidCallbackException('Invalid callback provided');
}

// Initial value of stop propagation flag should be false
$e->stopPropagation(false);

return $this->triggerListeners($event, $e, $callback);
}

Expand Down Expand Up @@ -455,9 +461,15 @@ protected function triggerListeners($event, EventInterface $e, $callback = null)
}

foreach ($listeners as $listener) {
$listenerCallback = $listener->getCallback();
if (!$listenerCallback) {
$this->detach($listener);
continue;
}

// Trigger the listener's callback, and push its result onto the
// response collection
$responses->push(call_user_func($listener->getCallback(), $e));
$responses->push(call_user_func($listenerCallback, $e));

// If the event was asked to stop propagating, do so
if ($e->propagationIsStopped()) {
Expand Down
2 changes: 1 addition & 1 deletion src/ProvidesEvents.php
Expand Up @@ -29,7 +29,7 @@ trait ProvidesEvents
*/
public function setEventManager(EventManagerInterface $events)
{
$identifiers = array(__CLASS__, get_called_class());
$identifiers = array(__CLASS__, get_class($this));
if (isset($this->eventIdentifier)) {
if ((is_string($this->eventIdentifier))
|| (is_array($this->eventIdentifier))
Expand Down
48 changes: 46 additions & 2 deletions test/EventManagerTest.php
Expand Up @@ -592,7 +592,12 @@ public function testIdentifierGetterSettersWorkWithArrays()
$this->assertSame($this->events->getIdentifiers(), $identifiers);
$identifiers[] = 'baz';
$this->assertInstanceOf('Zend\EventManager\EventManager', $this->events->addIdentifiers($identifiers));
$this->assertSame($this->events->getIdentifiers(), $identifiers);

// This is done because the keys doesn't matter, just the values
$expectedIdentifiers = $this->events->getIdentifiers();
sort($expectedIdentifiers);
sort($identifiers);
$this->assertSame($expectedIdentifiers, $identifiers);
}

public function testIdentifierGetterSettersWorkWithTraversables()
Expand All @@ -602,7 +607,13 @@ public function testIdentifierGetterSettersWorkWithTraversables()
$this->assertSame($this->events->getIdentifiers(), (array) $identifiers);
$identifiers = new ArrayIterator(array('foo', 'bar', 'baz'));
$this->assertInstanceOf('Zend\EventManager\EventManager', $this->events->addIdentifiers($identifiers));
$this->assertSame($this->events->getIdentifiers(), (array) $identifiers);

// This is done because the keys doesn't matter, just the values
$expectedIdentifiers = $this->events->getIdentifiers();
sort($expectedIdentifiers);
$identifiers = (array) $identifiers;
sort($identifiers);
$this->assertSame($expectedIdentifiers, $identifiers);
}

public function testListenersAttachedWithWildcardAreTriggeredForAllEvents()
Expand Down Expand Up @@ -646,4 +657,37 @@ public function testDoesNotCreateStaticInstanceIfNonePresent()
StaticEventManager::resetInstance();
$this->assertFalse($this->events->getSharedManager());
}

public function testTriggerSetsStopPropagationFlagToFalse()
{
$marker = (object) array('propagationIsStopped' => true);
$this->events->attach('foo', function ($e) use ($marker) {
$marker->propagationIsStopped = $e->propagationIsStopped();
});

$event = new Event();
$event->stopPropagation(true);
$this->events->trigger('foo', $event);

$this->assertFalse($marker->propagationIsStopped);
$this->assertFalse($event->propagationIsStopped());
}

public function testTriggerUntilSetsStopPropagationFlagToFalse()
{
$marker = (object) array('propagationIsStopped' => true);
$this->events->attach('foo', function ($e) use ($marker) {
$marker->propagationIsStopped = $e->propagationIsStopped();
});

$criteria = function ($r) {
return false;
};
$event = new Event();
$event->stopPropagation(true);
$this->events->triggerUntil('foo', $event, $criteria);

$this->assertFalse($marker->propagationIsStopped);
$this->assertFalse($event->propagationIsStopped());
}
}

0 comments on commit ce1dffc

Please sign in to comment.