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 10 changed files with 304 additions and 162 deletions.
78 changes: 42 additions & 36 deletions src/EventManager.php
Expand Up @@ -38,7 +38,7 @@
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class EventManager implements EventCollection
class EventManager implements EventCollection, SharedEventCollectionAware
{
/**
* Subscribed events and their listeners
Expand All @@ -52,22 +52,22 @@ class EventManager implements EventCollection
protected $eventClass = 'Zend\EventManager\Event';

/**
* Identifiers, used to pull static signals from StaticEventManager
* Identifiers, used to pull shared signals from SharedEventCollection instance
* @var array
*/
protected $identifiers = array();

/**
* Static connections
* @var false|null|StaticEventCollection
* Shared connections
* @var false|null|SharedEventCollection
*/
protected $staticConnections = null;
protected $sharedCollections = null;

/**
* Constructor
*
* Allows optionally specifying identifier(s) to use to pull signals from a
* StaticEventManager.
* SharedEventCollection.
*
* @param null|string|int|array|Traversable $identifiers
* @return void
Expand All @@ -90,32 +90,38 @@ public function setEventClass($class)
}

/**
* Set static connections container
* Set shared collections container
*
* @param null|StaticEventCollection $connections
* @param SharedEventCollection $connections
* @return void
*/
public function setStaticConnections(StaticEventCollection $connections = null)
public function setSharedCollections(SharedEventCollection $sharedEventCollection)
{
if (null === $connections) {
$this->staticConnections = false;
} else {
$this->staticConnections = $connections;
}
$this->sharedCollections = $sharedEventCollection;
return $this;
}

/**
* Get static connections container
* Remove any shared collections
*
* @return void
*/
public function unsetSharedCollections()
{
$this->sharedCollections = false;
}

/**
* Get shared collections container
*
* @return false|StaticEventCollection
* @return false|SharedEventCollection
*/
public function getStaticConnections()
public function getSharedCollections()
{
if (null === $this->staticConnections) {
$this->setStaticConnections(StaticEventManager::getInstance());
if (null === $this->sharedCollections) {
$this->setSharedCollections(StaticEventManager::getInstance());
}
return $this->staticConnections;
return $this->sharedCollections;
}

/**
Expand Down Expand Up @@ -429,20 +435,20 @@ protected function triggerListeners($event, EventDescription $e, $callback = nul
$responses = new ResponseCollection;
$listeners = $this->getListeners($event);

// Add static/wildcard listeners to the list of listeners,
// Add shared/wildcard listeners to the list of listeners,
// but don't modify the listeners object
$staticListeners = $this->getStaticListeners($event);
$staticWildcardListeners = $this->getStaticListeners('*');
$sharedListeners = $this->getSharedListeners($event);
$sharedWildcardListeners = $this->getSharedListeners('*');
$wildcardListeners = $this->getListeners('*');
if (count($staticListeners) || count($staticWildcardListeners) || count($wildcardListeners)) {
if (count($sharedListeners) || count($sharedWildcardListeners) || count($wildcardListeners)) {
$listeners = clone $listeners;
}

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

// Static wildcard listeners
$this->insertListeners($listeners, $staticWildcardListeners);
// Shared wildcard listeners
$this->insertListeners($listeners, $sharedWildcardListeners);

// Add wildcard listeners
$this->insertListeners($listeners, $wildcardListeners);
Expand Down Expand Up @@ -474,23 +480,23 @@ protected function triggerListeners($event, EventDescription $e, $callback = nul
}

/**
* Get list of all listeners attached to the static collection for
* Get list of all listeners attached to the shared collection for
* identifiers registered by this instance
*
* @param string $event
* @return array
*/
protected function getStaticListeners($event)
protected function getSharedListeners($event)
{
if (!$staticConnections = $this->getStaticConnections()) {
if (!$sharedCollections = $this->getSharedCollections()) {
return array();
}

$identifiers = $this->getIdentifiers();
$staticListeners = array();
$sharedListeners = array();

foreach ($identifiers as $id) {
if (!$listeners = $staticConnections->getListeners($id, $event)) {
if (!$listeners = $sharedCollections->getListeners($id, $event)) {
continue;
}

Expand All @@ -502,17 +508,17 @@ protected function getStaticListeners($event)
if (!$listener instanceof CallbackHandler) {
continue;
}
$staticListeners[] = $listener;
$sharedListeners[] = $listener;
}
}

return $staticListeners;
return $sharedListeners;
}

/**
* Add listeners to the master queue of listeners
*
* Used to inject static listeners and wildcard listeners.
* Used to inject shared listeners and wildcard listeners.
*
* @param PriorityQueue $masterListeners
* @param PriorityQueue $listeners
Expand Down
42 changes: 42 additions & 0 deletions src/EventManagerAware.php
@@ -0,0 +1,42 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_EventManager
* @subpackage UnitTest
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\EventManager;

/**
* Interface to automate setter injection for an EventManager instance
*
* @category Zend
* @package Zend_EventManager
* @subpackage UnitTest
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface EventManagerAware
{
/**
* Inject an EventManager instance
*
* @param EventCollection $eventManager
* @return void
*/
public function setEventManager(EventCollection $eventManager);
}
Expand Up @@ -21,14 +21,14 @@
namespace Zend\EventManager;

/**
* Interface for global (static) event listener collections
* Interface for shared event listener collections
*
* @category Zend
* @package Zend_EventManager
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface StaticEventCollection
interface SharedEventCollection
{
public function getListeners($id, $event);
}
42 changes: 42 additions & 0 deletions src/SharedEventCollectionAware.php
@@ -0,0 +1,42 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_EventManager
* @subpackage UnitTest
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\EventManager;

/**
* Interface to automate setter injection for a SharedEventCollection instance
*
* @category Zend
* @package Zend_EventManager
* @subpackage UnitTest
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface SharedEventCollectionAware
{
/**
* Inject an EventManager instance
*
* @param SharedEventCollection $sharedEventCollection
* @return SharedEventCollectionAware
*/
public function setSharedCollections(SharedEventCollection $sharedEventCollection);
}

0 comments on commit 8161582

Please sign in to comment.