Kaynağa Gözat

Backported #944 from ZF2

- see https://github.com/zendframework/zf2/pull/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
matthew 14 yıl önce
ebeveyn
işleme
d72df5fc1d

+ 25 - 25
library/Zend/EventManager/EventManager.php

@@ -59,7 +59,7 @@ class Zend_EventManager_EventManager implements Zend_EventManager_EventCollectio
      * Static connections
      * @var false|null|Zend_EventManager_StaticEventCollection
      */
-    protected $staticConnections = null;
+    protected $sharedConnections = null;
 
     /**
      * Constructor
@@ -93,12 +93,12 @@ class Zend_EventManager_EventManager implements Zend_EventManager_EventCollectio
      * @param  null|Zend_EventManager_StaticEventCollection $connections
      * @return void
      */
-    public function setStaticConnections(Zend_EventManager_StaticEventCollection $connections = null)
+    public function setSharedConnections(Zend_EventManager_SharedEventCollection $connections = null)
     {
         if (null === $connections) {
-            $this->staticConnections = false;
+            $this->sharedConnections = false;
         } else {
-            $this->staticConnections = $connections;
+            $this->sharedConnections = $connections;
         }
         return $this;
     }
@@ -106,14 +106,14 @@ class Zend_EventManager_EventManager implements Zend_EventManager_EventCollectio
     /**
      * Get static connections container
      *
-     * @return false|Zend_EventManager_StaticEventCollection
+     * @return false|Zend_EventManager_SharedEventCollection
      */
-    public function getStaticConnections()
+    public function getSharedConnections()
     {
-        if (null === $this->staticConnections) {
-            $this->setStaticConnections(Zend_EventManager_StaticEventManager::getInstance());
+        if (null === $this->sharedConnections) {
+            $this->setSharedConnections(Zend_EventManager_StaticEventManager::getInstance());
         }
-        return $this->staticConnections;
+        return $this->sharedConnections;
     }
 
     /**
@@ -431,20 +431,20 @@ class Zend_EventManager_EventManager implements Zend_EventManager_EventCollectio
         $responses = new Zend_EventManager_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);
@@ -476,23 +476,23 @@ class Zend_EventManager_EventManager implements Zend_EventManager_EventCollectio
     }
 
     /**
-     * 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 (!$sharedConnections = $this->getSharedConnections()) {
             return array();
         }
 
         $identifiers     = $this->getIdentifiers();
-        $staticListeners = array();
+        $sharedListeners = array();
 
         foreach ($identifiers as $id) {
-            if (!$listeners = $staticConnections->getListeners($id, $event)) {
+            if (!$listeners = $sharedConnections->getListeners($id, $event)) {
                 continue;
             }
 
@@ -504,17 +504,17 @@ class Zend_EventManager_EventManager implements Zend_EventManager_EventCollectio
                 if (!$listener instanceof Zend_Stdlib_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  Zend_Stdlib_PriorityQueue $masterListeners 
      * @param  Zend_Stdlib_PriorityQueue $listeners 

+ 40 - 0
library/Zend/EventManager/EventManagerAware.php

@@ -0,0 +1,40 @@
+<?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
+ */
+
+/**
+ * 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 Zend_EventManager_EventManagerAware
+{
+    /**
+     * Inject an EventManager instance
+     * 
+     * @param  Zend_EventManager_EventCollection $eventManager 
+     * @return void
+     */
+    public function setEventManager(Zend_EventManager_EventCollection $eventManager);
+}

+ 2 - 2
library/Zend/EventManager/StaticEventCollection.php → library/Zend/EventManager/SharedEventCollection.php

@@ -19,14 +19,14 @@
  */
 
 /**
- * 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 Zend_EventManager_StaticEventCollection
+interface Zend_EventManager_SharedEventCollection
 {
     public function getListeners($id, $event);
 }

+ 148 - 0
library/Zend/EventManager/SharedEventManager.php

@@ -0,0 +1,148 @@
+<?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
+ */
+
+require_once 'Zend/EventManager/EventManager.php';
+require_once 'Zend/EventManager/SharedEventCollection.php';
+
+/**
+ * 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 Zend_EventManager_SharedEventManager implements Zend_EventManager_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 Zend_EventManager_EventManager();
+            }
+            $this->identifiers[$id]->attach($event, $callback, $priority);
+        }
+    }
+
+    /**
+     * Detach a listener from an event offered by a given resource
+     * 
+     * @param  string|int $id
+     * @param  Zend_Stdlib_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, Zend_Stdlib_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);
+    }
+}

+ 40 - 0
library/Zend/EventManager/SharedEventManagerAware.php

@@ -0,0 +1,40 @@
+<?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
+ */
+
+/**
+ * Interface to automate setter injection for a SharedEventManager 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 Zend_EventManager_SharedEventManagerAware
+{
+    /**
+     * Inject an EventManager instance
+     * 
+     * @param  Zend_EventManager_SharedEventCollection $sharedEventManager 
+     * @return Zend_EventManager_SharedEventManagerAware
+     */
+    public function setSharedConnections(Zend_EventManager_SharedEventCollection $sharedEventManager);
+}

+ 2 - 116
library/Zend/EventManager/StaticEventManager.php

@@ -19,7 +19,7 @@
  */
 
 require_once 'Zend/EventManager/EventManager.php';
-require_once 'Zend/EventManager/StaticEventCollection.php';
+require_once 'Zend/EventManager/SharedEventManager.php';
 require_once 'Zend/Stdlib/CallbackHandler.php';
 
 /**
@@ -30,7 +30,7 @@ require_once 'Zend/Stdlib/CallbackHandler.php';
  * @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 Zend_EventManager_StaticEventManager implements Zend_EventManager_StaticEventCollection
+class Zend_EventManager_StaticEventManager extends Zend_EventManager_SharedEventManager
 {
     /**
      * @var Zend_EventManager_StaticEventManager
@@ -38,12 +38,6 @@ class Zend_EventManager_StaticEventManager implements Zend_EventManager_StaticEv
     protected static $instance;
 
     /**
-     * Identifiers with event connections
-     * @var array
-     */
-    protected $identifiers = array();
-
-    /**
      * Singleton
      * 
      * @return void
@@ -83,112 +77,4 @@ class Zend_EventManager_StaticEventManager implements Zend_EventManager_StaticEv
     {
         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>
-     * Zend_EventManager_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>
-     *
-     * Note: a PHP 5.3 closure is used in this example only for brevity; you 
-     * may pass any valid PHP callback as a listener.
-     * 
-     * @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 Zend_EventManager_EventManager();
-            }
-            $this->identifiers[$id]->attach($event, $callback, $priority);
-        }
-    }
-
-    /**
-     * Detach a listener from an event offered by a given resource
-     * 
-     * @param  string|int $id
-     * @param  Zend_Stdlib_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, Zend_Stdlib_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);
-    }
 }

+ 5 - 5
tests/Zend/EventManager/StaticIntegrationTest.php

@@ -93,7 +93,7 @@ class Zend_EventManager_StaticIntegrationTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(array('local3', 'static', 'local2', 'local'), $this->test->results);
     }
 
-    public function testPassingNullValueToSetStaticConnectionsDisablesStaticConnections()
+    public function testPassingNullValueToSetSharedConnectionsDisablesSharedConnections()
     {
         $this->counter = (object) array('count' => 0);
         Zend_EventManager_StaticEventManager::getInstance()->attach(
@@ -102,12 +102,12 @@ class Zend_EventManager_StaticIntegrationTest extends PHPUnit_Framework_TestCase
             array($this, 'advanceCounter')
         );
         $class = new Zend_EventManager_TestAsset_ClassWithEvents();
-        $class->events()->setStaticConnections(null);
+        $class->events()->setSharedConnections(null);
         $class->foo();
         $this->assertEquals(0, $this->counter->count);
     }
 
-    public function testCanPassAlternateStaticConnectionsHolder()
+    public function testCanPassAlternateSharedConnectionsHolder()
     {
         $this->counter = (object) array('count' => 0);
         Zend_EventManager_StaticEventManager::getInstance()->attach(
@@ -117,8 +117,8 @@ class Zend_EventManager_StaticIntegrationTest extends PHPUnit_Framework_TestCase
         );
         $mockStaticEvents = new Zend_EventManager_TestAsset_StaticEventsMock();
         $class = new Zend_EventManager_TestAsset_ClassWithEvents();
-        $class->events()->setStaticConnections($mockStaticEvents);
-        $this->assertSame($mockStaticEvents, $class->events()->getStaticConnections());
+        $class->events()->setSharedConnections($mockStaticEvents);
+        $this->assertSame($mockStaticEvents, $class->events()->getSharedConnections());
         $class->foo();
         $this->assertEquals(0, $this->counter->count);
     }

+ 2 - 2
tests/Zend/EventManager/TestAsset/StaticEventsMock.php

@@ -19,7 +19,7 @@
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  */
 
-require_once 'Zend/EventManager/StaticEventCollection.php';
+require_once 'Zend/EventManager/SharedEventCollection.php';
 
 /**
  * @category   Zend
@@ -29,7 +29,7 @@ require_once 'Zend/EventManager/StaticEventCollection.php';
  * @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 Zend_EventManager_TestAsset_StaticEventsMock implements Zend_EventManager_StaticEventCollection
+class Zend_EventManager_TestAsset_StaticEventsMock implements Zend_EventManager_SharedEventCollection
 {
     public function getListeners($id, $event)
     {