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

Commit

Permalink
Merge branch 'develop' of git://github.com/zendframework/zf2 into hot…
Browse files Browse the repository at this point in the history
…fix/cache-empty-namespace
  • Loading branch information
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 42 deletions.
24 changes: 8 additions & 16 deletions src/EventManager.php
Expand Up @@ -144,7 +144,7 @@ public function getIdentifiers()
*/
public function setIdentifiers($identifiers)
{
if (is_array($identifiers) || $identifiers instanceof \Traversable) {
if (is_array($identifiers) || $identifiers instanceof Traversable) {
$this->identifiers = array_unique((array) $identifiers);
} elseif ($identifiers !== null) {
$this->identifiers = array($identifiers);
Expand All @@ -160,7 +160,7 @@ public function setIdentifiers($identifiers)
*/
public function addIdentifiers($identifiers)
{
if (is_array($identifiers) || $identifiers instanceof \Traversable) {
if (is_array($identifiers) || $identifiers instanceof Traversable) {
$this->identifiers = array_unique($this->identifiers + (array) $identifiers);
} elseif ($identifiers !== null) {
$this->identifiers = array_unique(array_merge($this->identifiers, array($identifiers)));
Expand Down Expand Up @@ -447,19 +447,15 @@ protected function triggerListeners($event, EventInterface $e, $callback = null)
$wildcardListeners = $this->getListeners('*');
if (count($sharedListeners) || count($sharedWildcardListeners) || count($wildcardListeners)) {
$listeners = clone $listeners;
}

// Shared listeners on this specific event
$this->insertListeners($listeners, $sharedListeners);

// Shared wildcard listeners
$this->insertListeners($listeners, $sharedWildcardListeners);
// Shared listeners on this specific event
$this->insertListeners($listeners, $sharedListeners);

// Add wildcard listeners
$this->insertListeners($listeners, $wildcardListeners);
// Shared wildcard listeners
$this->insertListeners($listeners, $sharedWildcardListeners);

if ($listeners->isEmpty()) {
return $responses;
// Add wildcard listeners
$this->insertListeners($listeners, $wildcardListeners);
}

foreach ($listeners as $listener) {
Expand Down Expand Up @@ -535,10 +531,6 @@ protected function getSharedListeners($event)
*/
protected function insertListeners($masterListeners, $listeners)
{
if (!count($listeners)) {
return;
}

foreach ($listeners as $listener) {
$priority = $listener->getMetadatum('priority');
if (null === $priority) {
Expand Down
22 changes: 22 additions & 0 deletions src/EventManagerAwareTrait.php
@@ -0,0 +1,22 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_EventManager
*/

namespace Zend\EventManager;

use \Zend\EventManager\ProvidesEvents;

/**
* @category Zend
* @package Zend_EventManager
*/
trait EventManagerAwareTrait
{
use ProvidesEvents;
}
38 changes: 38 additions & 0 deletions src/SharedEventAggregateAwareInterface.php
@@ -0,0 +1,38 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_EventManager
*/

namespace Zend\EventManager;


/**
* Interface for allowing attachment of shared aggregate listeners.
*
* @category Zend
* @package Zend_EventManager
*/
interface SharedEventAggregateAwareInterface
{
/**
* Attach a listener aggregate
*
* @param SharedListenerAggregateInterface $aggregate
* @param int $priority If provided, a suggested priority for the aggregate to use
* @return mixed return value of {@link SharedListenerAggregateInterface::attachShared()}
*/
public function attachAggregate(SharedListenerAggregateInterface $aggregate, $priority = 1);

/**
* Detach a listener aggregate
*
* @param SharedListenerAggregateInterface $aggregate
* @return mixed return value of {@link SharedListenerAggregateInterface::detachShared()}
*/
public function detachAggregate(SharedListenerAggregateInterface $aggregate);
}
34 changes: 33 additions & 1 deletion src/SharedEventManager.php
Expand Up @@ -23,7 +23,9 @@
* @category Zend
* @package Zend_EventManager
*/
class SharedEventManager implements SharedEventManagerInterface
class SharedEventManager implements
SharedEventAggregateAwareInterface,
SharedEventManagerInterface
{
/**
* Identifiers with event connections
Expand Down Expand Up @@ -77,6 +79,22 @@ public function attach($id, $event, $callback, $priority = 1)
return $listeners[0];
}

/**
* Attach a listener aggregate
*
* Listener aggregates accept an EventManagerInterface instance, and call attachShared()
* one or more times, typically to attach to multiple events using local
* methods.
*
* @param SharedListenerAggregateInterface $aggregate
* @param int $priority If provided, a suggested priority for the aggregate to use
* @return mixed return value of {@link ListenerAggregateInterface::attachShared()}
*/
public function attachAggregate(SharedListenerAggregateInterface $aggregate, $priority = 1)
{
return $aggregate->attachShared($this, $priority);
}

/**
* Detach a listener from an event offered by a given resource
*
Expand All @@ -92,6 +110,20 @@ public function detach($id, CallbackHandler $listener)
return $this->identifiers[$id]->detach($listener);
}

/**
* Detach a listener aggregate
*
* Listener aggregates accept an SharedEventManagerInterface instance, and call detachShared()
* of all previously attached listeners.
*
* @param SharedListenerAggregateInterface $aggregate
* @return mixed return value of {@link SharedListenerAggregateInterface::detachShared()}
*/
public function detachAggregate(SharedListenerAggregateInterface $aggregate)
{
return $aggregate->detachShared($this);
}

/**
* Retrieve all registered events for a given resource
*
Expand Down
1 change: 0 additions & 1 deletion src/SharedEventManagerInterface.php
Expand Up @@ -66,5 +66,4 @@ public function getEvents($id);
* @return bool
*/
public function clearListeners($id, $event = null);

}
42 changes: 42 additions & 0 deletions src/SharedListenerAggregateInterface.php
@@ -0,0 +1,42 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_EventManager
*/

namespace Zend\EventManager;

/**
* Interface for self-registering event listeners.
*
* Classes implementing this interface may be registered by name or instance
* with an SharedEventManager, without an event name. The {@link attach()} method will
* then be called with the current SharedEventManager instance, allowing the class to
* wire up one or more listeners.
*
* @category Zend
* @package Zend_EventManager
*/
interface SharedListenerAggregateInterface
{
/**
* Attach one or more listeners
*
* Implementors may add an optional $priority argument; the SharedEventManager
* implementation will pass this to the aggregate.
*
* @param SharedEventManagerInterface $events
*/
public function attachShared(SharedEventManagerInterface $events);

/**
* Detach all previously attached listeners
*
* @param SharedEventManagerInterface $events
*/
public function detachShared(SharedEventManagerInterface $events);
}
46 changes: 46 additions & 0 deletions test/EventManagerAwareTraitTest.php
@@ -0,0 +1,46 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_EventManager
*/

namespace ZendTest\EventManager;

use \PHPUnit_Framework_TestCase as TestCase;
use \Zend\EventManager\EventManager;

/**
* @requires PHP 5.4
*/
class EventManagerAwareTraitTest extends TestCase
{
public function testSetEventManager()
{
$object = $this->getObjectForTrait('\Zend\EventManager\EventManagerAwareTrait');

$this->assertAttributeEquals(null, 'events', $object);

$eventManager = new EventManager;

$object->setEventManager($eventManager);

$this->assertAttributeEquals($eventManager, 'events', $object);
}

public function testGetEventManager()
{
$object = $this->getObjectForTrait('\Zend\EventManager\EventManagerAwareTrait');

$this->assertInstanceOf('\Zend\EventManager\EventManagerInterface', $object->getEventManager());

$eventManager = new EventManager;

$object->setEventManager($eventManager);

$this->assertSame($eventManager, $object->getEventManager());
}
}
36 changes: 26 additions & 10 deletions test/EventManagerTest.php
Expand Up @@ -277,10 +277,18 @@ public function testAttachAggregateReturnsAttachOfListenerAggregate()
public function testCanDetachListenerAggregates()
{
// setup some other event listeners, to ensure appropriate items are detached
$listenerFooBar1 = $this->events->attach('foo.bar', function() { return true; });
$listenerFooBar2 = $this->events->attach('foo.bar', function() { return true; });
$listenerFooBaz1 = $this->events->attach('foo.baz', function() { return true; });
$listenerOther = $this->events->attach('other', function() { return true; });
$listenerFooBar1 = $this->events->attach('foo.bar', function () {
return true;
});
$listenerFooBar2 = $this->events->attach('foo.bar', function () {
return true;
});
$listenerFooBaz1 = $this->events->attach('foo.baz', function () {
return true;
});
$listenerOther = $this->events->attach('other', function () {
return true;
});

$aggregate = new TestAsset\MockAggregate();
$this->events->attachAggregate($aggregate);
Expand All @@ -307,10 +315,18 @@ public function testCanDetachListenerAggregates()
public function testCanDetachListenerAggregatesViaDetach()
{
// setup some other event listeners, to ensure appropriate items are detached
$listenerFooBar1 = $this->events->attach('foo.bar', function() { return true; });
$listenerFooBar2 = $this->events->attach('foo.bar', function() { return true; });
$listenerFooBaz1 = $this->events->attach('foo.baz', function() { return true; });
$listenerOther = $this->events->attach('other', function() { return true; });
$listenerFooBar1 = $this->events->attach('foo.bar', function () {
return true;
});
$listenerFooBar2 = $this->events->attach('foo.bar', function () {
return true;
});
$listenerFooBaz1 = $this->events->attach('foo.baz', function () {
return true;
});
$listenerOther = $this->events->attach('other', function () {
return true;
});

$aggregate = new TestAsset\MockAggregate();
$this->events->attach($aggregate);
Expand Down Expand Up @@ -593,7 +609,7 @@ public function testListenersAttachedWithWildcardAreTriggeredForAllEvents()
{
$test = new stdClass;
$test->events = array();
$callback = function($e) use ($test) {
$callback = function ($e) use ($test) {
$test->events[] = $e->getName();
};

Expand All @@ -618,7 +634,7 @@ public function testSharedEventManagerAttachReturnsCallbackHandler()
$callbackHandler = $shared->attach(
'foo',
'bar',
function($e) {
function ($e) {
return true;
}
);
Expand Down
10 changes: 5 additions & 5 deletions test/FilterChainTest.php
Expand Up @@ -74,14 +74,14 @@ public function testRetrievingAttachedFiltersShouldReturnEmptyArrayWhenNoFilters

public function testFilterChainShouldReturnLastResponse()
{
$this->filterchain->attach(function($context, $params, $chain) {
$this->filterchain->attach(function ($context, $params, $chain) {
if (isset($params['string'])) {
$params['string'] = trim($params['string']);
}
$return = $chain->next($context, $params, $chain);
return $return;
});
$this->filterchain->attach(function($context, array $params) {
$this->filterchain->attach(function ($context, array $params) {
$string = isset($params['string']) ? $params['string'] : '';
return str_rot13($string);
});
Expand All @@ -107,17 +107,17 @@ public function testInterceptingFilterShouldReceiveChain()

public function testFilteringStopsAsSoonAsAFilterFailsToCallNext()
{
$this->filterchain->attach(function($context, $params, $chain) {
$this->filterchain->attach(function ($context, $params, $chain) {
if (isset($params['string'])) {
$params['string'] = trim($params['string']);
}
return $chain->next($context, $params, $chain);
}, 10000);
$this->filterchain->attach(function($context, array $params) {
$this->filterchain->attach(function ($context, array $params) {
$string = isset($params['string']) ? $params['string'] : '';
return str_rot13($string);
}, 1000);
$this->filterchain->attach(function($context, $params, $chain) {
$this->filterchain->attach(function ($context, $params, $chain) {
$string = isset($params['string']) ? $params['string'] : '';
return hash('md5', $string);
}, 100);
Expand Down

0 comments on commit ba6e7b6

Please sign in to comment.