Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Feature/eventmanager di usability #944

Merged
merged 6 commits into from

3 participants

Matthew Weier O'Phinney Marco Pivetta Rob Allen
Matthew Weier O'Phinney

Shared Collections support for EventManager

This is a solution for usage of the StaticEventManager. Basically, it
will instead expect the following:

  • Classes composing an EventManager will implement EventManagerAware, and thus opt-in to setter injection of an EM instance
    • EM instances will not be shared between classes
  • The EM implements the SharedEventManagerAware interface, indicating it opts-in to setter injection of a SharedEventManager (note: not static)
    • A single "SharedEventManager" will be shared between all instances of EventManager

This commit does the following:

  • Renames StaticEventCollection to SharedEventCollection
  • Creates SharedEventManager, which implements all the functionality of StaticEventManager except the singleton aspects.
  • StaticEventManager now extends SharedEventManager, and simply implements a singleton pattern.
  • Introduced EventManagerAware and SharedEventManagerAware interfaces; EventManager implements the latter.
  • Adds configuration to the Bootstrap in setupLocator() to mark the EventManager as an unshared instance.

One issue is known, however:

  • Zend\Module\Listener\LocatorRegistration currently fails, as it assumes a StaticEventManager instance. As such, it cannot connect to the bootstrap event. I'm not sure what the solution for this will be -- I assume we'll need to instantiate a SharedEventManager and pass it into the Module\Manager, Mvc\Bootstrap, and potentially Mvc\Application. This will require documentation, and coordination with the skeleton application. Another alternative is to have this listener instead attach directly to the Bootstrap, and loop through the module manager. Ideas are welcome.
Marco Pivetta
Collaborator

Hey matt,
somehow missed this PR before (I'm really transient these weeks), but here I still see a big problem that can't be solved easily anyway, which is the singleton pattern.

Here's my use-case in short:

  1. Bootstrap "admin wrapper" application, run it
  2. Load ACL/whatever in "dispatch"
  3. Bootstrap sub-application (within dispatch) and inject ACL/whatever in it's service locator
  4. Run sub-application
  5. Use the response of the sub-application as response for the "admin wrapper" application

So the problem is that using a singleton would obviously re-triggering the dispatch (and the other MVC events) from the sub-application would affect the main application. Maybe I'm approaching the problem incorrectly, but the main issue still remains... Or am I getting this wrong?

Matthew Weier O'Phinney
Marco Pivetta
Collaborator

Aha, then I'll have to read through it with more care when I got some spare time... Thank you :)

Matthew Weier O'Phinney

@Ocramius So, I was replying from my phone over the weekend. Here's a better explanation. :)

The current architecture is that each class composing an EventManager receives a unique EventManager instance. This allows you to attach listeners locally, and they will only be notified if this EM instance triggers that event. Additionally, we have the concept of a "SharedEventManager". This is essentially simply a collection of listeners on events on specific contexts -- which are typically class names. The same instance of a SharedEventManager is injected into all EventManager instances (a capability recently made possible with the DIC). What this allows you to do is to listen to an event when you do not have direct access to the object or EM -- e.g., because it may not exist yet. When the EM triggers an event, it queries not only for local listeners on that event, but also those on the SharedEventManager, if any is composed.

(The StaticEventManager, as of this PR, is simply an extension of the SharedEventManager that implements a singleton interface; we would not be using it internally in the framework, but only providing it for those who want to use it.)

The key part of the SharedEventManager to note is that it has both the concept of an event and a context -- the latter allows fine-grained attachment of listeners -- so that if you want a "dispatch" listener on controllers, but not the application, you can use the context to make this happen. (This has been true of the StaticEventManager previous to this commit as well.)

Marco Pivetta
Collaborator

Thank you for explaining that :) I still have to wrap my mind around it, still haven't got any free time by now :\

Rob Allen
Collaborator

Update for clean merge and docs to be added by @weierophinney

Rob Allen akrabat was assigned
weierophinney added some commits
Matthew Weier O'Phinney weierophinney Shared Collections support for EventManager
This is a solution for usage of the StaticEventManager. Basically, it
will instead expect the following:

- Classes composing an EventManager will implement EventManagerAware,
  and thus opt-in to setter injection of an EM instance
  - EM instances will _not_ be shared between classes
- The EM implements the SharedEventManagerAware interface, indicating it
  opts-in to setter injection of a SharedEventManager (note: not static)
  - A single "SharedEventManager" will be shared between all instances of
    EventManager

This commit does the following:

- Renames StaticEventCollection to SharedEventCollection
- Creates SharedEventManager, which implements all the functionality of
  StaticEventManager except the singleton aspects.
- StaticEventManager now extends SharedEventManager, and simply
  implements a singleton pattern.
- Introduced EventManagerAware and SharedEventManagerAware interfaces;
  EventManager implements the latter.
- Adds configuration to the Bootstrap in setupLocator() to mark the EventManager
  as an unshared instance.

It depends on work @ralphschindler is doing in his
feature/di-share-configuration branch.
2f06e47
Matthew Weier O'Phinney weierophinney Updated events-aware classes to implement EventManagerAware
- Updated classes to implement EventManagerAware
- Updated tests to use SharedEventManager instead of StaticEventManager
f9ebfe7
Matthew Weier O'Phinney weierophinney Renames for consistency
- s/SharedEventManagerAware/SharedEventCollectionAware/, as the
  interface was typehinting on the latter.
- s/setSharedConnections/setSharedCollections/, as the latter is the
  terminology in the interface
- s/sharedConnections/sharedCollections/, for the same reasons as the
  previous point
674881a
Matthew Weier O'Phinney

Will now merge cleanly; need to (a) update docs, and (b) re-enable StaticEventManager awareness temporarily.

weierophinney added some commits
Matthew Weier O'Phinney weierophinney [#944] re-enable StaticEventManager awareness
- Re-enables StaticEventManager awareness temporarily until
  Application/Bootstrap is refactored to allow sharing a
  SharedEventManager instance and injecting into many discrete
  EventManager instances.
ac5535c
Matthew Weier O'Phinney weierophinney [#944] Updated documentation
- Updated documentation to reference SharedEventCollection and SharedEventManager
- Made a note about StaticEventManager and that it's deprecated for use inside the framework
ef5fe43
Matthew Weier O'Phinney

@akrabat Docs are updated, StaticEventManager usage (temporarily) reinstated, and merge conflicts handled. Ready to go!

Matthew Weier O'Phinney weierophinney [#944] Fix failing tests due to EM changes
- Removed setEventManager() from ModuleHandler interface (cannot
  redefine methods when extending interfaces)
- s/(setSharedCo)ll(ections)/\1nn\2/ in MVC tests
0c5f6d8
Rob Allen akrabat merged commit 0c5f6d8 into from
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney [#944] re-enable StaticEventManager awareness
- Re-enables StaticEventManager awareness temporarily until
  Application/Bootstrap is refactored to allow sharing a
  SharedEventManager instance and injecting into many discrete
  EventManager instances.
a0105cf
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney [#944] Updated documentation
- Updated documentation to reference SharedEventCollection and SharedEventManager
- Made a note about StaticEventManager and that it's deprecated for use inside the framework
a78b566
Deleted user Unknown referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney [#944] Fix failing tests due to EM changes
- Removed setEventManager() from ModuleHandler interface (cannot
  redefine methods when extending interfaces)
- s/(setSharedCo)ll(ections)/\1nn\2/ in MVC tests
e512728
Henrik Hautakoski pnx referenced this pull request from a commit in pnx/zf1
matthew Backported #944 from ZF2
- see zendframework/zf2#944
- renames StaticEventCollection to SharedEventCollection
- creates SharedEventManager, which implements SharedEventCollection, and is not
  a singleton
- StaticEventManager now extends SharedEventManager and implements a singleton

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24700 44c647ce-9c0f-0410-b52a-842ac1e357ba
d72df5f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 19, 2012
  1. Matthew Weier O'Phinney

    Shared Collections support for EventManager

    weierophinney authored
    This is a solution for usage of the StaticEventManager. Basically, it
    will instead expect the following:
    
    - Classes composing an EventManager will implement EventManagerAware,
      and thus opt-in to setter injection of an EM instance
      - EM instances will _not_ be shared between classes
    - The EM implements the SharedEventManagerAware interface, indicating it
      opts-in to setter injection of a SharedEventManager (note: not static)
      - A single "SharedEventManager" will be shared between all instances of
        EventManager
    
    This commit does the following:
    
    - Renames StaticEventCollection to SharedEventCollection
    - Creates SharedEventManager, which implements all the functionality of
      StaticEventManager except the singleton aspects.
    - StaticEventManager now extends SharedEventManager, and simply
      implements a singleton pattern.
    - Introduced EventManagerAware and SharedEventManagerAware interfaces;
      EventManager implements the latter.
    - Adds configuration to the Bootstrap in setupLocator() to mark the EventManager
      as an unshared instance.
    
    It depends on work @ralphschindler is doing in his
    feature/di-share-configuration branch.
  2. Matthew Weier O'Phinney

    Updated events-aware classes to implement EventManagerAware

    weierophinney authored
    - Updated classes to implement EventManagerAware
    - Updated tests to use SharedEventManager instead of StaticEventManager
  3. Matthew Weier O'Phinney

    Renames for consistency

    weierophinney authored
    - s/SharedEventManagerAware/SharedEventCollectionAware/, as the
      interface was typehinting on the latter.
    - s/setSharedConnections/setSharedCollections/, as the latter is the
      terminology in the interface
    - s/sharedConnections/sharedCollections/, for the same reasons as the
      previous point
  4. Matthew Weier O'Phinney

    [#944] re-enable StaticEventManager awareness

    weierophinney authored
    - Re-enables StaticEventManager awareness temporarily until
      Application/Bootstrap is refactored to allow sharing a
      SharedEventManager instance and injecting into many discrete
      EventManager instances.
  5. Matthew Weier O'Phinney

    [#944] Updated documentation

    weierophinney authored
    - Updated documentation to reference SharedEventCollection and SharedEventManager
    - Made a note about StaticEventManager and that it's deprecated for use inside the framework
Commits on Apr 20, 2012
  1. Matthew Weier O'Phinney

    [#944] Fix failing tests due to EM changes

    weierophinney authored
    - Removed setEventManager() from ModuleHandler interface (cannot
      redefine methods when extending interfaces)
    - s/(setSharedCo)ll(ections)/\1nn\2/ in MVC tests
This page is out of date. Refresh to see the latest.
Showing with 435 additions and 270 deletions.
  1. +79 −55 documentation/manual/en/module_specs/Zend_EventManager-EventManager.xml
  2. +3 −2 library/Zend/Cache/Storage/Adapter/AbstractAdapter.php
  3. +42 −36 library/Zend/EventManager/EventManager.php
  4. +42 −0 library/Zend/EventManager/EventManagerAware.php
  5. +2 −2 library/Zend/EventManager/{StaticEventCollection.php → SharedEventCollection.php}
  6. +42 −0 library/Zend/EventManager/SharedEventCollectionAware.php
  7. +149 −0 library/Zend/EventManager/SharedEventManager.php
  8. +1 −112 library/Zend/EventManager/StaticEventManager.php
  9. +2 −10 library/Zend/Module/ModuleHandler.php
  10. +2 −10 library/Zend/Mvc/AppContext.php
  11. +13 −2 library/Zend/Mvc/Bootstrap.php
  12. +2 −1  library/Zend/Mvc/Controller/ActionController.php
  13. +2 −1  library/Zend/Mvc/Controller/RestfulController.php
  14. +2 −1  library/Zend/View/View.php
  15. +3 −0  tests/Zend/EventManager/EventManagerTest.php
  16. +5 −2 tests/Zend/EventManager/StaticEventManagerTest.php
  17. +16 −8 tests/Zend/EventManager/StaticIntegrationTest.php
  18. +2 −2 tests/Zend/EventManager/TestAsset/StaticEventsMock.php
  19. +12 −14 tests/Zend/Mvc/ApplicationTest.php
  20. +7 −6 tests/Zend/Mvc/Controller/ActionControllerTest.php
  21. +7 −6 tests/Zend/Mvc/Controller/RestfulControllerTest.php
134 documentation/manual/en/module_specs/Zend_EventManager-EventManager.xml
View
@@ -32,9 +32,9 @@
</itemizedlist>
<para>
- The basic architecture allows you to attach and detach listeners to named events, both on
- a per-instance basis as well as statically; trigger events; and interrupt execution of
- listeners.
+ The basic architecture allows you to attach and detach listeners to named events, both
+ on a per-instance basis as well as via shared collections; trigger events; and interrupt
+ execution of listeners.
</para>
</section>
@@ -47,18 +47,27 @@
<programlisting language="php"><![CDATA[
use Zend\EventManager\EventCollection,
- Zend\EventManager\EventManager;
+use Zend\EventManager\EventManager;
+use Zend\EventManager\EventManagerAware;
-class Foo
+class Foo implements EventManagerAware
{
protected $events;
- public function events(EventCollection $events = null)
+ public function setEventManager(EventCollection $events)
+ {
+ $events->setIdentifiers(array(
+ __CLASS__,
+ get_called_class(),
+ ));
+ $this->events = $events;
+ return $this;
+ }
+
+ public function events()
{
- if (null !== $events) {
- $this->events = $events;
- } elseif (null === $this->events) {
- $this->events = new EventManager(__CLASS__);
+ if (null === $this->events) {
+ $this->setEventManager(new EventManager());
}
return $this->events;
}
@@ -133,23 +142,30 @@ $foo->bar('baz', 'bat');
<para>
Sometimes you may want to specify listeners without yet having an object instance of the
- class composing an <classname>EventManager</classname>. The
- <classname>StaticEventManager</classname> allows you to do this. The call to
+ class composing an <classname>EventManager</classname>. Zend Framework enables this
+ through the concept of a <interfacename>SharedEventCollection</interfacename>. Simply
+ put, you can inject individual <classname>EventManager</classname> instances with a
+ well-known <interfacename>SharedEventCollection</interfacename>, and the
+ <classname>EventManager</classname> instance will query it for additional listeners.
+ Listeners attach to a <interfacename>SharedEventCollection</interfacename> in roughly
+ the same way the do normal event managers; the call to
<methodname>attach</methodname> is identical to the <classname>EventManager</classname>,
but expects an additional parameter at the beginning: a named instance. Remember the
example of composing an <classname>EventManager</classname>, how we passed it
<constant>__CLASS__</constant>? That value, or any strings you provide in an array to
- the constructor, may be used to identify an instance when using the
- <classname>StaticEventManager</classname>. As an example, we could change the above
- example to attach statically:
+ the constructor, may be used to identify an instance when using a
+ <interfacename>SharedEventCollection</interfacename>. As an example, assuming we have a
+ <classname>SharedEventManager</classname> instance that we know has been injected in our
+ <classname>EventManager</classname> instances (for instance, via dependency injection),
+ we could change the above example to attach via the shared collection:
</para>
<programlisting language="php"><![CDATA[
-use Zend\EventManager\StaticEventManager,
- Zend\Log\Factory as LogFactory;
+use Zend\Log\Factory as LogFactory;
+
+// Assume $events is a Zend\EventManager\SharedEventManager instance
$log = LogFactory($someConfig);
-$events = StaticEventManager::getInstance();
$events->attach('Foo', 'bar', function ($e) use ($log) {
$event = $e->getName();
$target = get_class($e->getTarget());
@@ -165,6 +181,7 @@ $events->attach('Foo', 'bar', function ($e) use ($log) {
// Later, instantiate Foo:
$foo = new Foo();
+$foo->events()->setSharedEventCollection($events);
// And we can still trigger the above event:
$foo->bar('baz', 'bat');
@@ -172,6 +189,25 @@ $foo->bar('baz', 'bat');
// bar called on Foo, using params {"baz" : "baz", "bat" : "bat"}"
]]></programlisting>
+ <note>
+ <info><title>StaticEventManager</title></info>
+
+ <para>
+ As of 2.0.0beta3, you can use the <classname>StaticEventManager</classname>
+ singleton as a <interfacename>SharedEventCollection</interfacename>. As such, you do
+ not need to worry about where and how to get access to the
+ <interfacename>SharedEventCollection</interfacename>; it's globally available by
+ simply calling <code>StaticEventManager::getInstance()</code>.
+ </para>
+
+ <para>
+ Be aware, however, that its usage is deprecated within the framework, and starting
+ with 2.0.0beta4, you will instead configure a
+ <classname>SharedEventManager</classname> instance that will be injected by the
+ framework into individual <classname>EventManager</classname> instances.
+ </para>
+ </note>
+
<para>
The <classname>EventManager</classname> also provides the ability to detach listeners,
short-circuit execution of an event either from within a listener or by testing return
@@ -184,8 +220,8 @@ $foo->bar('baz', 'bat');
<para>
Sometimes you'll want to attach the same listener to many events or to all events of
- a given instance -- or potentially, with the static manager, many contexts, and many
- events. The <classname>EventManager</classname> component allows for this.
+ a given instance -- or potentially, with a shared event collection, many contexts,
+ and many events. The <classname>EventManager</classname> component allows for this.
</para>
<example xml:id="zend.event-manager.event-manager.quick-start.wildcard.many">
@@ -221,11 +257,11 @@ $events->attach('*', $callback);
</para>
</example>
- <example xml:id="zend.event-manager.event-manager.quick-start.wildcard.static-many">
- <title>Attaching to many events at once via the StaticEventManager</title>
+ <example xml:id="zend.event-manager.event-manager.quick-start.wildcard.shared-many">
+ <title>Attaching to many events at once via a SharedEventManager</title>
<programlisting language="php"><![CDATA[
-$events = StaticEventManager::getInstance();
+$events = new SharedEventManager();
// Attach to many events on the context "foo"
$events->attach('foo', array('these', 'are', 'event', 'names'), $callback);
@@ -239,11 +275,11 @@ $events->attach(array('foo', 'bar'), array('these', 'are', 'event', 'names'), $c
</para>
</example>
- <example xml:id="zend.event-manager.event-manager.quick-start.wildcard.static-wildcard">
- <title>Attaching to many events at once via the StaticEventManager</title>
+ <example xml:id="zend.event-manager.event-manager.quick-start.wildcard.shared-wildcard">
+ <title>Attaching to many events at once via a SharedEventManager</title>
<programlisting language="php"><![CDATA[
-$events = StaticEventManager::getInstance();
+$events = new SharedEventManager();
// Attach to all events on the context "foo"
$events->attach('foo', '*', $callback);
@@ -277,7 +313,7 @@ $events->attach(array('foo', 'bar'), '*', $callback);
<para>
A string or array of strings to which the given
<classname>EventManager</classname> instance can answer when accessed via
- the <classname>StaticEventManager</classname>.
+ a <interfacename>SharedEventManager</interfacename>.
</para>
</listitem>
</varlistentry>
@@ -294,16 +330,12 @@ $events->attach(array('foo', 'bar'), '*', $callback);
</varlistentry>
<varlistentry>
- <term>static_connections</term>
+ <term>shared_collections</term>
<listitem>
<para>
- An instance of a <interfacename>StaticEventCollection</interfacename>
- instance to use when triggering events. By default, this will use
- the global <classname>StaticEventManager</classname> instance, but that can
- be overridden by passing a value to this method. A <constant>null</constant>
- value will prevent the instance from triggering any further statically
- attached listeners.
+ An instance of a <interfacename>SharedEventCollection</interfacename>
+ instance to use when triggering events.
</para>
</listitem>
</varlistentry>
@@ -325,7 +357,7 @@ $events->attach(array('foo', 'bar'), '*', $callback);
</methodsynopsis>
<para>
Constructs a new <classname>EventManager</classname> instance, using the
- given identifier, if provided, for purposes of static attachment.
+ given identifier, if provided, for purposes of shared collections.
</para>
</listitem>
</varlistentry>
@@ -346,42 +378,34 @@ $events->attach(array('foo', 'bar'), '*', $callback);
</listitem>
</varlistentry>
- <varlistentry xml:id="zend.event-manager.event-manager.methods.set-static-connections">
- <term>setStaticConnections</term>
+ <varlistentry xml:id="zend.event-manager.event-manager.methods.set-shared-collections">
+ <term>setSharedCollections</term>
<listitem>
<methodsynopsis>
- <methodname>setStaticConnections</methodname>
+ <methodname>setSharedCollections</methodname>
<methodparam>
- <funcparams>StaticEventCollection $connections = null</funcparams>
+ <funcparams>SharedEventCollection $collections = null</funcparams>
</methodparam>
</methodsynopsis>
<para>
- An instance of a <interfacename>StaticEventCollection</interfacename>
- instance to use when triggering events. By default, this will use
- the global <classname>StaticEventManager</classname> instance, but that can
- be overridden by passing a value to this method. A <constant>null</constant>
- value will prevent the instance from triggering any further statically
- attached listeners.
+ An instance of a <interfacename>SharedEventCollection</interfacename>
+ instance to use when triggering events.
</para>
</listitem>
</varlistentry>
- <varlistentry xml:id="zend.event-manager.event-manager.methods.get-static-connections">
- <term>getStaticConnections</term>
+ <varlistentry xml:id="zend.event-manager.event-manager.methods.get-shared-collections">
+ <term>getSharedCollections</term>
<listitem>
<methodsynopsis>
- <methodname>getStaticConnections</methodname>
+ <methodname>getSharedCollections</methodname>
<void/>
</methodsynopsis>
<para>
Returns the currently attached
- <interfacename>StaticEventCollection</interfacename> instance, lazily
- retrieving the global <classname>StaticEventManager</classname> instance if
- none is attached and usage of static listeners hasn't been disabled by
- passing a <constant>null</constant> value to <link
- linkend="zend.event-manager.event-manager.methods.set-static-connections">setStaticConnections()</link>.
- Returns either a boolean <constant>false</constant> if static listeners are
- disabled, or a <interfacename>StaticEventCollection</interfacename> instance
+ <interfacename>SharedEventCollection</interfacename> instance.
+ Returns either a <constant>null</constant> if no collection is attached,
+ or a <interfacename>SharedEventCollection</interfacename> instance
otherwise.
</para>
</listitem>
5 library/Zend/Cache/Storage/Adapter/AbstractAdapter.php
View
@@ -33,7 +33,8 @@
Zend\Cache\Storage\PostEvent,
Zend\Cache\Storage\Plugin,
Zend\EventManager\EventCollection,
- Zend\EventManager\EventManager;
+ Zend\EventManager\EventManager,
+ Zend\EventManager\EventManagerAware;
/**
* @category Zend
@@ -42,7 +43,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
*/
-abstract class AbstractAdapter implements Adapter
+abstract class AbstractAdapter implements Adapter, EventManagerAware
{
/**
* The used EventManager if any
78 library/Zend/EventManager/EventManager.php
View
@@ -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
@@ -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
@@ -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;
}
/**
@@ -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);
@@ -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;
}
@@ -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
42 library/Zend/EventManager/EventManagerAware.php
View
@@ -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);
+}
4 library/Zend/EventManager/StaticEventCollection.php → library/Zend/EventManager/SharedEventCollection.php
View
@@ -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 library/Zend/EventManager/SharedEventCollectionAware.php
View
@@ -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);
+}
149 library/Zend/EventManager/SharedEventManager.php
View
@@ -0,0 +1,149 @@
+<?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
+ * @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;
+
+use Zend\Stdlib\CallbackHandler;
+
+/**
+ * Shared/contextual EventManager
+ *
+ * Allows attaching to EMs composed by other classes without having an instance first.
+ * The assumption is that the SharedEventManager will be injected into EventManager
+ * instances, and then queried for additional listeners when triggering an event.
+ *
+ * @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
+ */
+class SharedEventManager implements SharedEventCollection
+{
+ /**
+ * Identifiers with event connections
+ * @var array
+ */
+ protected $identifiers = array();
+
+ /**
+ * Attach a listener to an event
+ *
+ * Allows attaching a callback to an event offerred by one or more
+ * identifying components. As an example, the following connects to the
+ * "getAll" event of both an AbstractResource and EntityResource:
+ *
+ * <code>
+ * SharedEventManager::getInstance()->connect(
+ * array('My\Resource\AbstractResource', 'My\Resource\EntityResource'),
+ * 'getOne',
+ * function ($e) use ($cache) {
+ * if (!$id = $e->getParam('id', false)) {
+ * return;
+ * }
+ * if (!$data = $cache->load(get_class($resource) . '::getOne::' . $id )) {
+ * return;
+ * }
+ * return $data;
+ * }
+ * );
+ * </code>
+ *
+ * @param string|array $id Identifier(s) for event emitting component(s)
+ * @param string $event
+ * @param callback $callback PHP Callback
+ * @param int $priority Priority at which listener should execute
+ * @return void
+ */
+ public function attach($id, $event, $callback, $priority = 1)
+ {
+ $ids = (array) $id;
+ foreach ($ids as $id) {
+ if (!array_key_exists($id, $this->identifiers)) {
+ $this->identifiers[$id] = new EventManager();
+ }
+ $this->identifiers[$id]->attach($event, $callback, $priority);
+ }
+ }
+
+ /**
+ * Detach a listener from an event offered by a given resource
+ *
+ * @param string|int $id
+ * @param CallbackHandler $listener
+ * @return bool Returns true if event and listener found, and unsubscribed; returns false if either event or listener not found
+ */
+ public function detach($id, CallbackHandler $listener)
+ {
+ if (!array_key_exists($id, $this->identifiers)) {
+ return false;
+ }
+ return $this->identifiers[$id]->detach($listener);
+ }
+
+ /**
+ * Retrieve all registered events for a given resource
+ *
+ * @param string|int $id
+ * @return array
+ */
+ public function getEvents($id)
+ {
+ if (!array_key_exists($id, $this->identifiers)) {
+ return false;
+ }
+ return $this->identifiers[$id]->getEvents();
+ }
+
+ /**
+ * Retrieve all listeners for a given identifier and event
+ *
+ * @param string|int $id
+ * @param string|int $event
+ * @return false|\Zend\Stdlib\PriorityQueue
+ */
+ public function getListeners($id, $event)
+ {
+ if (!array_key_exists($id, $this->identifiers)) {
+ return false;
+ }
+ return $this->identifiers[$id]->getListeners($event);
+ }
+
+ /**
+ * Clear all listeners for a given identifier, optionally for a specific event
+ *
+ * @param string|int $id
+ * @param null|string $event
+ * @return bool
+ */
+ public function clearListeners($id, $event = null)
+ {
+ if (!array_key_exists($id, $this->identifiers)) {
+ return false;
+ }
+
+ if (null === $event) {
+ unset($this->identifiers[$id]);
+ return true;
+ }
+
+ return $this->identifiers[$id]->clearListeners($event);
+ }
+}
113 library/Zend/EventManager/StaticEventManager.php
View
@@ -30,7 +30,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 StaticEventManager implements StaticEventCollection
+class StaticEventManager extends SharedEventManager
{
/**
* @var StaticEventManager
@@ -38,12 +38,6 @@ class StaticEventManager implements StaticEventCollection
protected static $instance;
/**
- * Identifiers with event connections
- * @var array
- */
- protected $identifiers = array();
-
- /**
* Singleton
*
* @return void
@@ -83,109 +77,4 @@ public static function resetInstance()
{
self::$instance = null;
}
-
- /**
- * Attach a listener to an event
- *
- * Allows attaching a callback to an event offerred by one or more
- * identifying components. As an example, the following connects to the
- * "getAll" event of both an AbstractResource and EntityResource:
- *
- * <code>
- * StaticEventManager::getInstance()->connect(
- * array('My\Resource\AbstractResource', 'My\Resource\EntityResource'),
- * 'getOne',
- * function ($e) use ($cache) {
- * if (!$id = $e->getParam('id', false)) {
- * return;
- * }
- * if (!$data = $cache->load(get_class($resource) . '::getOne::' . $id )) {
- * return;
- * }
- * return $data;
- * }
- * );
- * </code>
- *
- * @param string|array $id Identifier(s) for event emitting component(s)
- * @param string $event
- * @param callback $callback PHP Callback
- * @param int $priority Priority at which listener should execute
- * @return void
- */
- public function attach($id, $event, $callback, $priority = 1)
- {
- $ids = (array) $id;
- foreach ($ids as $id) {
- if (!array_key_exists($id, $this->identifiers)) {
- $this->identifiers[$id] = new EventManager();
- }
- $this->identifiers[$id]->attach($event, $callback, $priority);
- }
- }
-
- /**
- * Detach a listener from an event offered by a given resource
- *
- * @param string|int $id
- * @param CallbackHandler $listener
- * @return bool Returns true if event and listener found, and unsubscribed; returns false if either event or listener not found
- */
- public function detach($id, CallbackHandler $listener)
- {
- if (!array_key_exists($id, $this->identifiers)) {
- return false;
- }
- return $this->identifiers[$id]->detach($listener);
- }
-
- /**
- * Retrieve all registered events for a given resource
- *
- * @param string|int $id
- * @return array
- */
- public function getEvents($id)
- {
- if (!array_key_exists($id, $this->identifiers)) {
- return false;
- }
- return $this->identifiers[$id]->getEvents();
- }
-
- /**
- * Retrieve all listeners for a given identifier and event
- *
- * @param string|int $id
- * @param string|int $event
- * @return false|\Zend\Stdlib\PriorityQueue
- */
- public function getListeners($id, $event)
- {
- if (!array_key_exists($id, $this->identifiers)) {
- return false;
- }
- return $this->identifiers[$id]->getListeners($event);
- }
-
- /**
- * Clear all listeners for a given identifier, optionally for a specific event
- *
- * @param string|int $id
- * @param null|string $event
- * @return bool
- */
- public function clearListeners($id, $event = null)
- {
- if (!array_key_exists($id, $this->identifiers)) {
- return false;
- }
-
- if (null === $event) {
- unset($this->identifiers[$id]);
- return true;
- }
-
- return $this->identifiers[$id]->clearListeners($event);
- }
}
12 library/Zend/Module/ModuleHandler.php
View
@@ -2,9 +2,9 @@
namespace Zend\Module;
-use Zend\EventManager\EventCollection;
+use Zend\EventManager\EventManagerAware;
-interface ModuleHandler
+interface ModuleHandler extends EventManagerAware
{
/**
* Load the provided modules.
@@ -45,14 +45,6 @@ public function getModules();
public function setModules($modules);
/**
- * Set the event manager instance used by this module manager.
- *
- * @param EventCollection $events
- * @return ModuleHandler
- */
- public function setEventManager(EventCollection $events);
-
- /**
* Retrieve the event manager
*
* Lazy-loads an EventManager instance if none registered.
12 library/Zend/Mvc/AppContext.php
View
@@ -3,21 +3,13 @@
namespace Zend\Mvc;
use Zend\Di\Locator,
- Zend\EventManager\EventCollection,
+ Zend\EventManager\EventManagerAware,
Zend\Stdlib\RequestDescription as Request,
Zend\Stdlib\ResponseDescription as Response;
-interface AppContext
+interface AppContext extends EventManagerAware
{
/**
- * Set the event manager instance used by this context
- *
- * @param EventCollection $events
- * @return AppContext
- */
- public function setEventManager(EventCollection $events);
-
- /**
* Set a service locator/DI object
*
* @param Locator $locator
15 library/Zend/Mvc/Bootstrap.php
View
@@ -6,10 +6,11 @@
Zend\Config\Config,
Zend\EventManager\EventCollection as Events,
Zend\EventManager\EventManager,
+ Zend\EventManager\EventManagerAware,
Zend\EventManager\StaticEventManager,
Zend\Mvc\Router\Http\TreeRouteStack as Router;
-class Bootstrap implements Bootstrapper
+class Bootstrap implements Bootstrapper, EventManagerAware
{
/**
* @var \Zend\Config\Config
@@ -217,7 +218,17 @@ protected function setupLocator(AppContext $application)
),
),
),
- ))));
+ )), 'instance' => array(
+ 'preferences' => array(
+ // Use EventManager for EventCollection
+ 'Zend\EventManager\EventCollection' => 'Zend\EventManager\EventManager',
+ // Use SharedEventManager for SharedEventCollection
+ 'Zend\EventManager\SharedEventCollection' => 'Zend\EventManager\SharedEventManager',
+ ),
+ 'Zend\EventManager\EventManager' => array(
+ 'shared' => false, // new instance per class needing an instance
+ ),
+ )));
$diConfig->configure($di);
$config = new DiConfiguration($this->config->di);
3  library/Zend/Mvc/Controller/ActionController.php
View
@@ -6,6 +6,7 @@
Zend\EventManager\EventCollection,
Zend\EventManager\EventDescription as Event,
Zend\EventManager\EventManager,
+ Zend\EventManager\EventManagerAware,
Zend\Http\PhpEnvironment\Response as HttpResponse,
Zend\Loader\Broker,
Zend\Loader\Pluggable,
@@ -21,7 +22,7 @@
/**
* Basic action controller
*/
-abstract class ActionController implements Dispatchable, InjectApplicationEvent, LocatorAware, Pluggable
+abstract class ActionController implements Dispatchable, EventManagerAware, InjectApplicationEvent, LocatorAware, Pluggable
{
//use \Zend\EventManager\ProvidesEvents;
3  library/Zend/Mvc/Controller/RestfulController.php
View
@@ -6,6 +6,7 @@
Zend\EventManager\EventCollection,
Zend\EventManager\EventDescription as Event,
Zend\EventManager\EventManager,
+ Zend\EventManager\EventManagerAware,
Zend\Http\Request as HttpRequest,
Zend\Http\PhpEnvironment\Response as HttpResponse,
Zend\Loader\Broker,
@@ -21,7 +22,7 @@
/**
* Abstract RESTful controller
*/
-abstract class RestfulController implements Dispatchable, InjectApplicationEvent, LocatorAware, Pluggable
+abstract class RestfulController implements Dispatchable, EventManagerAware, InjectApplicationEvent, LocatorAware, Pluggable
{
protected $broker;
protected $request;
3  library/Zend/View/View.php
View
@@ -22,6 +22,7 @@
use Zend\EventManager\EventCollection,
Zend\EventManager\EventManager,
+ Zend\EventManager\EventManagerAware,
Zend\Mvc\MvcEvent,
Zend\Stdlib\RequestDescription as Request,
Zend\Stdlib\ResponseDescription as Response;
@@ -32,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 View
+class View implements EventManagerAware
{
/**
* @var EventCollection
3  tests/Zend/EventManager/EventManagerTest.php
View
@@ -27,6 +27,7 @@
Zend\EventManager\EventDescription,
Zend\EventManager\EventManager,
Zend\EventManager\ResponseCollection,
+ Zend\EventManager\StaticEventManager,
Zend\Stdlib\CallbackHandler;
/**
@@ -41,6 +42,8 @@ class EventManagerTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
+ StaticEventManager::resetInstance();
+
if (isset($this->message)) {
unset($this->message);
}
7 tests/Zend/EventManager/StaticEventManagerTest.php
View
@@ -165,6 +165,7 @@ public function testListenersAttachedUsingWildcardEventWillBeTriggeredByResource
$staticEvents->attach('bar', '*', $callback);
$events = new EventManager('bar');
+ $events->setSharedCollections($staticEvents);
foreach (array('foo', 'bar', 'baz') as $event) {
$events->trigger($event);
@@ -234,9 +235,11 @@ public function testCanPassArrayOfIdentifiersToConstructor()
public function testListenersAttachedToAnyIdentifierProvidedToEventManagerWillBeTriggered()
{
$identifiers = array('foo', 'bar');
- $manager = new EventManager($identifiers);
$events = StaticEventManager::getInstance();
- $test = new \stdClass;
+ $manager = new EventManager($identifiers);
+ $manager->setSharedCollections($events);
+
+ $test = new \stdClass;
$test->triggered = 0;
$events->attach('foo', 'bar', function($e) use ($test) {
$test->triggered++;
24 tests/Zend/EventManager/StaticIntegrationTest.php
View
@@ -43,7 +43,8 @@ public function setUp()
public function testCanConnectStaticallyToClassWithEvents()
{
$counter = (object) array('count' => 0);
- StaticEventManager::getInstance()->attach(
+ $events = StaticEventManager::getInstance();
+ $events->attach(
'ZendTest\EventManager\TestAsset\ClassWithEvents',
'foo',
function ($e) use ($counter) {
@@ -51,6 +52,7 @@ function ($e) use ($counter) {
}
);
$class = new TestAsset\ClassWithEvents();
+ $class->events()->setSharedCollections($events);
$class->foo();
$this->assertEquals(1, $counter->count);
}
@@ -58,7 +60,8 @@ function ($e) use ($counter) {
public function testLocalHandlersAreExecutedPriorToStaticHandlersWhenSetWithSamePriority()
{
$test = (object) array('results' => array());
- StaticEventManager::getInstance()->attach(
+ $events = StaticEventManager::getInstance();
+ $events->attach(
'ZendTest\EventManager\TestAsset\ClassWithEvents',
'foo',
function ($e) use ($test) {
@@ -69,6 +72,7 @@ function ($e) use ($test) {
$class->events()->attach('foo', function ($e) use ($test) {
$test->results[] = 'local';
});
+ $class->events()->setSharedCollections($events);
$class->foo();
$this->assertEquals(array('local', 'static'), $test->results);
}
@@ -76,7 +80,8 @@ function ($e) use ($test) {
public function testLocalHandlersAreExecutedInPriorityOrderRegardlessOfStaticOrLocalRegistration()
{
$test = (object) array('results' => array());
- StaticEventManager::getInstance()->attach(
+ $events = StaticEventManager::getInstance();
+ $events->attach(
'ZendTest\EventManager\TestAsset\ClassWithEvents',
'foo',
function ($e) use ($test) {
@@ -94,11 +99,12 @@ function ($e) use ($test) {
$class->events()->attach('foo', function ($e) use ($test) {
$test->results[] = 'local3';
}, 15000); // highest priority
+ $class->events()->setSharedCollections($events);
$class->foo();
$this->assertEquals(array('local3', 'static', 'local2', 'local'), $test->results);
}
- public function testPassingNullValueToSetStaticConnectionsDisablesStaticConnections()
+ public function testCallingUnsetSharedCollectionsDisablesStaticCollections()
{
$counter = (object) array('count' => 0);
StaticEventManager::getInstance()->attach(
@@ -109,7 +115,7 @@ function ($e) use ($counter) {
}
);
$class = new TestAsset\ClassWithEvents();
- $class->events()->setStaticConnections(null);
+ $class->events()->unsetSharedCollections();
$class->foo();
$this->assertEquals(0, $counter->count);
}
@@ -126,8 +132,8 @@ function ($e) use ($counter) {
);
$mockStaticEvents = new TestAsset\StaticEventsMock();
$class = new TestAsset\ClassWithEvents();
- $class->events()->setStaticConnections($mockStaticEvents);
- $this->assertSame($mockStaticEvents, $class->events()->getStaticConnections());
+ $class->events()->setSharedCollections($mockStaticEvents);
+ $this->assertSame($mockStaticEvents, $class->events()->getSharedCollections());
$class->foo();
$this->assertEquals(0, $counter->count);
}
@@ -135,7 +141,8 @@ function ($e) use ($counter) {
public function testTriggerMergesPrioritiesOfStaticAndInstanceListeners()
{
$test = (object) array('results' => array());
- StaticEventManager::getInstance()->attach(
+ $events = StaticEventManager::getInstance();
+ $events->attach(
'ZendTest\EventManager\TestAsset\ClassWithEvents',
'foo',
function ($e) use ($test) {
@@ -147,6 +154,7 @@ function ($e) use ($test) {
$class->events()->attach('foo', function ($e) use ($test) {
$test->results[] = 'local';
}, -100);
+ $class->events()->setSharedCollections($events);
$class->foo();
$this->assertEquals(array('static', 'local'), $test->results);
}
4 tests/Zend/EventManager/TestAsset/StaticEventsMock.php
View
@@ -21,7 +21,7 @@
namespace ZendTest\EventManager\TestAsset;
-use Zend\EventManager\StaticEventCollection;
+use Zend\EventManager\SharedEventCollection;
/**
* @category Zend
@@ -31,7 +31,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 StaticEventsMock implements StaticEventCollection
+class StaticEventsMock implements SharedEventCollection
{
public function getListeners($id, $event)
{
26 tests/Zend/Mvc/ApplicationTest.php
View
@@ -8,7 +8,7 @@
Zend\Di\Di as DependencyInjector,
Zend\Di\ServiceLocator,
Zend\EventManager\EventManager,
- Zend\EventManager\StaticEventManager,
+ Zend\EventManager\SharedEventManager,
Zend\Http\Request,
Zend\Http\PhpEnvironment\Response,
Zend\Mvc\Application,
@@ -17,11 +17,6 @@
class ApplicationTest extends TestCase
{
- public function setUp()
- {
- StaticEventManager::resetInstance();
- }
-
public function testEventManagerIsLazyLoaded()
{
$app = new Application();
@@ -405,14 +400,16 @@ public function testDispatchingInjectsLocatorInLocatorAwareControllers()
$router = $app->getRouter();
$router->addRoute('locator-aware', $route);
+ $events = new SharedEventManager();
$locator = new TestAsset\Locator();
- $locator->add('locator-aware', function() {
- return new TestAsset\LocatorAwareController;
+ $locator->add('locator-aware', function() use ($events) {
+ $controller = new TestAsset\LocatorAwareController;
+ $controller->events()->setSharedCollections($events);
+ return $controller;
});
$app->setLocator($locator);
$storage = new ArrayObject();
- $events = StaticEventManager::getInstance();
$events->attach('ZendTest\Mvc\TestAsset\LocatorAwareController', 'dispatch', function ($e) use ($storage) {
$controller = $e->getTarget();
$storage['locator'] = $controller->getLocator();
@@ -436,8 +433,10 @@ public function testFinishEventIsTriggeredAfterDispatching()
public function testCanProvideAlternateEventManagerToDisableDefaultRouteAndDispatchEventListeners()
{
- $app = $this->setupActionController();
- $events = new EventManager();
+ $app = $this->setupActionController();
+ $events = new EventManager();
+ $sharedEvents = new SharedEventManager();
+ $events->setSharedCollections($sharedEvents);
$app->setEventManager($events);
$listener1 = function($e) {
@@ -452,9 +451,8 @@ public function testCanProvideAlternateEventManagerToDisableDefaultRouteAndDispa
$content = (empty($content) ? 'listener2' : $content . '::' . 'listener2');
$response->setContent($content);
};
- $events = StaticEventManager::getInstance();
- $events->attach('ZendTest\Mvc\Controller\TestAsset\SampleController', 'dispatch', $listener1, 10);
- $events->attach('ZendTest\Mvc\Controller\TestAsset\SampleController', 'dispatch', $listener2, -10);
+ $sharedEvents->attach('ZendTest\Mvc\Controller\TestAsset\SampleController', 'dispatch', $listener1, 10);
+ $sharedEvents->attach('ZendTest\Mvc\Controller\TestAsset\SampleController', 'dispatch', $listener2, -10);
$app->run();
$response = $app->getResponse();
13 tests/Zend/Mvc/Controller/ActionControllerTest.php
View
@@ -3,7 +3,7 @@
namespace ZendTest\Mvc\Controller;
use PHPUnit_Framework_TestCase as TestCase,
- Zend\EventManager\StaticEventManager,
+ Zend\EventManager\SharedEventManager,
Zend\Http\Request,
Zend\Http\Response,
Zend\Mvc\Controller\PluginBroker,
@@ -25,8 +25,6 @@ public function setUp()
$this->event = new MvcEvent();
$this->event->setRouteMatch($this->routeMatch);
$this->controller->setEvent($this->event);
-
- StaticEventManager::resetInstance();
}
public function testDispatchInvokesNotFoundActionWhenNoActionPresentInRouteMatch()
@@ -96,10 +94,11 @@ public function testEventManagerListensOnDispatchableInterfaceByDefault()
{
$response = new Response();
$response->setContent('short circuited!');
- $events = StaticEventManager::getInstance();
+ $events = new SharedEventManager();
$events->attach('Zend\Stdlib\Dispatchable', 'dispatch', function($e) use ($response) {
return $response;
}, 10);
+ $this->controller->events()->setSharedCollections($events);
$result = $this->controller->dispatch($this->request, $this->response);
$this->assertSame($response, $result);
}
@@ -108,10 +107,11 @@ public function testEventManagerListensOnActionControllerClassByDefault()
{
$response = new Response();
$response->setContent('short circuited!');
- $events = StaticEventManager::getInstance();
+ $events = new SharedEventManager();
$events->attach('Zend\Mvc\Controller\ActionController', 'dispatch', function($e) use ($response) {
return $response;
}, 10);
+ $this->controller->events()->setSharedCollections($events);
$result = $this->controller->dispatch($this->request, $this->response);
$this->assertSame($response, $result);
}
@@ -120,10 +120,11 @@ public function testEventManagerListensOnClassNameByDefault()
{
$response = new Response();
$response->setContent('short circuited!');
- $events = StaticEventManager::getInstance();
+ $events = new SharedEventManager();
$events->attach(get_class($this->controller), 'dispatch', function($e) use ($response) {
return $response;
}, 10);
+ $this->controller->events()->setSharedCollections($events);
$result = $this->controller->dispatch($this->request, $this->response);
$this->assertSame($response, $result);
}
13 tests/Zend/Mvc/Controller/RestfulControllerTest.php
View
@@ -4,7 +4,7 @@
use PHPUnit_Framework_TestCase as TestCase,
stdClass,
- Zend\EventManager\StaticEventManager,
+ Zend\EventManager\SharedEventManager,
Zend\Http\Request,
Zend\Http\Response,
Zend\Mvc\MvcEvent,
@@ -26,8 +26,6 @@ public function setUp()
$this->event = new MvcEvent;
$this->event->setRouteMatch($this->routeMatch);
$this->controller->setEvent($this->event);
-
- StaticEventManager::resetInstance();
}
public function testDispatchInvokesListWhenNoActionPresentAndNoIdentifierOnGet()
@@ -135,10 +133,11 @@ public function testEventManagerListensOnDispatchableInterfaceByDefault()
{
$response = new Response();
$response->setContent('short circuited!');
- $events = StaticEventManager::getInstance();
+ $events = new SharedEventManager();
$events->attach('Zend\Stdlib\Dispatchable', 'dispatch', function($e) use ($response) {
return $response;
}, 10);
+ $this->controller->events()->setSharedCollections($events);
$result = $this->controller->dispatch($this->request, $this->response);
$this->assertSame($response, $result);
}
@@ -147,10 +146,11 @@ public function testEventManagerListensOnRestfulControllerClassByDefault()
{
$response = new Response();
$response->setContent('short circuited!');
- $events = StaticEventManager::getInstance();
+ $events = new SharedEventManager();
$events->attach('Zend\Mvc\Controller\RestfulController', 'dispatch', function($e) use ($response) {
return $response;
}, 10);
+ $this->controller->events()->setSharedCollections($events);
$result = $this->controller->dispatch($this->request, $this->response);
$this->assertSame($response, $result);
}
@@ -159,10 +159,11 @@ public function testEventManagerListensOnClassNameByDefault()
{
$response = new Response();
$response->setContent('short circuited!');
- $events = StaticEventManager::getInstance();
+ $events = new SharedEventManager();
$events->attach(get_class($this->controller), 'dispatch', function($e) use ($response) {
return $response;
}, 10);
+ $this->controller->events()->setSharedCollections($events);
$result = $this->controller->dispatch($this->request, $this->response);
$this->assertSame($response, $result);
}
Something went wrong with that request. Please try again.