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 into cach…
Browse files Browse the repository at this point in the history
…e_plugin_priority

Conflicts:
	library/Zend/Cache/Storage/Adapter/AbstractAdapter.php
	library/Zend/Cache/Storage/Plugin/ClearByFactor.php
	library/Zend/Cache/Storage/Plugin/ExceptionHandler.php
	library/Zend/Cache/Storage/Plugin/IgnoreUserAbort.php
	library/Zend/Cache/Storage/Plugin/OptimizeByFactor.php
	library/Zend/Cache/Storage/Plugin/Serializer.php
  • Loading branch information
Show file tree
Hide file tree
Showing 24 changed files with 429 additions and 243 deletions.
5 changes: 2 additions & 3 deletions src/Event.php
Expand Up @@ -33,7 +33,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 Event implements EventDescription
class Event implements EventInterface
{
/**
* @var string Event name
Expand Down Expand Up @@ -114,8 +114,7 @@ public function setParams($params)
{
if (!is_array($params) && !is_object($params)) {
throw new Exception\InvalidArgumentException(sprintf(
'Event parameters must be an array or object; received "%s"',
(is_object($params) ? get_class($params) : gettype($params))
'Event parameters must be an array or object; received "%s"', gettype($params)
));
}

Expand Down
4 changes: 2 additions & 2 deletions src/EventDescription.php → src/EventInterface.php
Expand Up @@ -28,7 +28,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
*/
interface EventDescription
interface EventInterface
{
/**
* Get event name
Expand Down Expand Up @@ -94,7 +94,7 @@ public function setParams($params);
public function setParam($name, $value);

/**
* Indicate whether or not the parent EventCollection should stop propagating events
* Indicate whether or not the parent EventManagerInterface should stop propagating events
*
* @param bool $flag
* @return void
Expand Down
128 changes: 67 additions & 61 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 EventManagerInterface, SharedEventManagerAwareInterface
{
/**
* 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 SharedEventManagerInterface instance
* @var array
*/
protected $identifiers = array();

/**
* Static connections
* @var false|null|StaticEventCollection
* Shared event manager
* @var false|null|SharedEventManagerInterface
*/
protected $staticConnections = null;
protected $sharedManager = null;

/**
* Constructor
*
* Allows optionally specifying identifier(s) to use to pull signals from a
* StaticEventManager.
* SharedEventManagerInterface.
*
* @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 event manager
*
* @param null|StaticEventCollection $connections
* @param SharedEventManagerInterface $connections
* @return void
*/
public function setStaticConnections(StaticEventCollection $connections = null)
public function setSharedManager(SharedEventManagerInterface $sharedEventManager)
{
if (null === $connections) {
$this->staticConnections = false;
} else {
$this->staticConnections = $connections;
}
$this->sharedManager = $sharedEventManager;
return $this;
}

/**
* Get static connections container
* Remove any shared event manager currently attached
*
* @return void
*/
public function unsetSharedManager()
{
$this->sharedManager = false;
}

/**
* Get shared event manager
*
* @return false|StaticEventCollection
* @return false|SharedEventManagerInterface
*/
public function getStaticConnections()
public function getSharedManager()
{
if (null === $this->staticConnections) {
$this->setStaticConnections(StaticEventManager::getInstance());
if (null === $this->sharedManager) {
$this->setSharedManager(StaticEventManager::getInstance());
}
return $this->staticConnections;
return $this->sharedManager;
}

/**
Expand All @@ -132,7 +138,7 @@ public function getIdentifiers()
* Set the identifiers (overrides any currently set identifiers)
*
* @param string|int|array|Traversable $identifiers
* @return ModuleManager
* @return EventManager Provides a fluent interface
*/
public function setIdentifiers($identifiers)
{
Expand All @@ -148,7 +154,7 @@ public function setIdentifiers($identifiers)
* Add some identifier(s) (appends to any currently set identifiers)
*
* @param string|int|array|Traversable $identifiers
* @return ModuleManager
* @return EventManager Provides a fluent interface
*/
public function addIdentifiers($identifiers)
{
Expand All @@ -173,15 +179,15 @@ public function addIdentifiers($identifiers)
*/
public function trigger($event, $target = null, $argv = array(), $callback = null)
{
if ($event instanceof EventDescription) {
if ($event instanceof EventInterface) {
$e = $event;
$event = $e->getName();
$callback = $target;
} elseif ($target instanceof EventDescription) {
} elseif ($target instanceof EventInterface) {
$e = $target;
$e->setName($event);
$callback = $argv;
} elseif ($argv instanceof EventDescription) {
} elseif ($argv instanceof EventInterface) {
$e = $argv;
$e->setName($event);
$e->setTarget($target);
Expand Down Expand Up @@ -214,15 +220,15 @@ public function trigger($event, $target = null, $argv = array(), $callback = nul
*/
public function triggerUntil($event, $target, $argv = null, $callback = null)
{
if ($event instanceof EventDescription) {
if ($event instanceof EventInterface) {
$e = $event;
$event = $e->getName();
$callback = $target;
} elseif ($target instanceof EventDescription) {
} elseif ($target instanceof EventInterface) {
$e = $target;
$e->setName($event);
$callback = $argv;
} elseif ($argv instanceof EventDescription) {
} elseif ($argv instanceof EventInterface) {
$e = $argv;
$e->setName($event);
$e->setTarget($target);
Expand Down Expand Up @@ -254,15 +260,15 @@ public function triggerUntil($event, $target, $argv = null, $callback = null)
* You can specify "*" for the event name. In such cases, the listener will
* be triggered for every event.
*
* @param string|array|ListenerAggregate $event An event or array of event names. If a ListenerAggregate, proxies to {@link attachAggregate()}.
* @param callback|int $callback If string $event provided, expects PHP callback; for a ListenerAggregate $event, this will be the priority
* @param string|array|ListenerAggregateInterface $event An event or array of event names. If a ListenerAggregateInterface, proxies to {@link attachAggregate()}.
* @param callback|int $callback If string $event provided, expects PHP callback; for a ListenerAggregateInterface $event, this will be the priority
* @param int $priority If provided, the priority at which to register the callback
* @return CallbackHandler|mixed CallbackHandler if attaching callback (to allow later unsubscribe); mixed if attaching aggregate
*/
public function attach($event, $callback = null, $priority = 1)
{
// Proxy ListenerAggregate arguments to attachAggregate()
if ($event instanceof ListenerAggregate) {
// Proxy ListenerAggregateInterface arguments to attachAggregate()
if ($event instanceof ListenerAggregateInterface) {
return $this->attachAggregate($event, $callback);
}

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

/**
* Unsubscribe a listener from an event
*
* @param CallbackHandler|ListenerAggregate $listener
* @param CallbackHandler|ListenerAggregateInterface $listener
* @return bool Returns true if event and listener found, and unsubscribed; returns false if either event or listener not found
* @throws Exception\InvalidArgumentException if invalid listener provided
*/
public function detach($listener)
{
if ($listener instanceof ListenerAggregate) {
if ($listener instanceof ListenerAggregateInterface) {
return $this->detachAggregate($listener);
}

if (!$listener instanceof CallbackHandler) {
throw new Exception\InvalidArgumentException(sprintf(
'%s: expected a ListenerAggregate or CallbackHandler; received "%s"',
'%s: expected a ListenerAggregateInterface or CallbackHandler; received "%s"',
__METHOD__,
(is_object($listener) ? get_class($listener) : gettype($listener))
));
Expand All @@ -350,13 +356,13 @@ public function detach($listener)
/**
* Detach a listener aggregate
*
* Listener aggregates accept an EventCollection instance, and call detach()
* Listener aggregates accept an EventManagerInterface instance, and call detach()
* of all previously attached listeners.
*
* @param ListenerAggregate $aggregate
* @return mixed return value of {@link ListenerAggregate::detach()}
* @param ListenerAggregateInterface $aggregate
* @return mixed return value of {@link ListenerAggregateInterface::detach()}
*/
public function detachAggregate(ListenerAggregate $aggregate)
public function detachAggregate(ListenerAggregateInterface $aggregate)
{
return $aggregate->detach($this);
}
Expand Down Expand Up @@ -420,29 +426,29 @@ public function prepareArgs(array $args)
* delegate.
*
* @param string $event Event name
* @param EventDescription $e
* @param EventInterface $e
* @param null|callback $callback
* @return ResponseCollection
*/
protected function triggerListeners($event, EventDescription $e, $callback = null)
protected function triggerListeners($event, EventInterface $e, $callback = null)
{
$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 event manager for
* identifiers registered by this instance
*
* @param string $event
* @return array
*/
protected function getStaticListeners($event)
protected function getSharedListeners($event)
{
if (!$staticConnections = $this->getStaticConnections()) {
if (!$sharedManager = $this->getSharedManager()) {
return array();
}

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

foreach ($identifiers as $id) {
if (!$listeners = $staticConnections->getListeners($id, $event)) {
if (!$listeners = $sharedManager->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/EventManagerAwareInterface.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 EventManagerAwareInterface
{
/**
* Inject an EventManager instance
*
* @param EventManagerInterface $eventManager
* @return void
*/
public function setEventManager(EventManagerInterface $eventManager);
}

0 comments on commit 25e9a69

Please sign in to comment.