Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

SharedEventManager -> static instance

Due to a number of issues that have arisen, in many cases due to user
expectations, it makes sense to make the initial shared event manager instance
the seed for the StaticEventManager. As such, the following changes were made:

- StaticEventManager:
  - Adds setInstance() and hasInstance() methods; former takes a
    SharedEventManagerInterface, and latter indicates whether the instance is
    populated.
- EventManager:
  - setSharedManager() seeds the StaticEventManager
  - getSharedManager() now tests for an instance in StaticEventManager, using it
    if populated
- Updated the Forward MVC controller plugin tests to ensure the SEM instance is
  reset, ensuring tests run correctly as part of a suite.
  • Loading branch information...
commit 107aae55cee590af4d336d416e7f98fbdb7639c0 1 parent 30ee79b
Matthew Weier O'Phinney weierophinney authored
19 library/Zend/EventManager/EventManager.php
@@ -85,6 +85,7 @@ public function setEventClass($class)
85 85 public function setSharedManager(SharedEventManagerInterface $sharedEventManager)
86 86 {
87 87 $this->sharedManager = $sharedEventManager;
  88 + StaticEventManager::setInstance($sharedEventManager);
88 89 return $this;
89 90 }
90 91
@@ -101,13 +102,27 @@ public function unsetSharedManager()
101 102 /**
102 103 * Get shared event manager
103 104 *
  105 + * If one is not defined, but we have a static instance in
  106 + * StaticEventManager, that one will be used and set in this instance.
  107 + *
  108 + * If none is available in the StaticEventManager, a boolean false is
  109 + * returned.
  110 + *
104 111 * @return false|SharedEventManagerInterface
105 112 */
106 113 public function getSharedManager()
107 114 {
108   - if (null === $this->sharedManager) {
109   - $this->setSharedManager(StaticEventManager::getInstance());
  115 + if (false === $this->sharedManager
  116 + || $this->sharedManager instanceof SharedEventManagerInterface
  117 + ) {
  118 + return $this->sharedManager;
  119 + }
  120 +
  121 + if (!StaticEventManager::hasInstance()) {
  122 + return false;
110 123 }
  124 +
  125 + $this->sharedManager = StaticEventManager::getInstance();
111 126 return $this->sharedManager;
112 127 }
113 128
23 library/Zend/EventManager/StaticEventManager.php
@@ -51,12 +51,33 @@ private function __clone()
51 51 public static function getInstance()
52 52 {
53 53 if (null === static::$instance) {
54   - static::$instance = new static();
  54 + static::setInstance(new static());
55 55 }
56 56 return static::$instance;
57 57 }
58 58
59 59 /**
  60 + * Set the singleton to a specific SharedEventManagerInterface instance
  61 + *
  62 + * @param SharedEventManagerInterface $instance
  63 + * @return void
  64 + */
  65 + public static function setInstance(SharedEventManagerInterface $instance)
  66 + {
  67 + static::$instance = $instance;
  68 + }
  69 +
  70 + /**
  71 + * Is a singleton instance defined?
  72 + *
  73 + * @return bool
  74 + */
  75 + public static function hasInstance()
  76 + {
  77 + return (static::$instance instanceof SharedEventManagerInterface);
  78 + }
  79 +
  80 + /**
60 81 * Reset the singleton instance
61 82 *
62 83 * @return void
21 tests/Zend/EventManager/EventManagerTest.php
@@ -16,6 +16,7 @@
16 16 use Zend\EventManager\EventInterface;
17 17 use Zend\EventManager\EventManager;
18 18 use Zend\EventManager\ResponseCollection;
  19 +use Zend\EventManager\SharedEventManager;
19 20 use Zend\EventManager\StaticEventManager;
20 21 use Zend\Stdlib\CallbackHandler;
21 22
@@ -35,6 +36,12 @@ public function setUp()
35 36 unset($this->message);
36 37 }
37 38 $this->events = new EventManager;
  39 + StaticEventManager::resetInstance();
  40 + }
  41 +
  42 + public function tearDown()
  43 + {
  44 + StaticEventManager::resetInstance();
38 45 }
39 46
40 47 public function testAttachShouldReturnCallbackHandler()
@@ -596,4 +603,18 @@ public function testListenersAttachedWithWildcardAreTriggeredForAllEvents()
596 603 $this->assertContains($event, $test->events);
597 604 }
598 605 }
  606 +
  607 + public function testSettingSharedEventManagerSetsStaticEventManagerInstance()
  608 + {
  609 + $shared = new SharedEventManager();
  610 + $this->events->setSharedManager($shared);
  611 + $this->assertSame($shared, $this->events->getSharedManager());
  612 + $this->assertSame($shared, StaticEventManager::getInstance());
  613 + }
  614 +
  615 + public function testDoesNotCreateStaticInstanceIfNonePresent()
  616 + {
  617 + StaticEventManager::resetInstance();
  618 + $this->assertFalse($this->events->getSharedManager());
  619 + }
599 620 }
7 tests/Zend/Mvc/Controller/Plugin/ForwardTest.php
@@ -12,6 +12,7 @@
12 12
13 13 use PHPUnit_Framework_TestCase as TestCase;
14 14 use stdClass;
  15 +use Zend\EventManager\StaticEventManager;
15 16 use Zend\Http\Request;
16 17 use Zend\Http\Response;
17 18 use Zend\Mvc\Controller\Plugin\Forward as ForwardPlugin;
@@ -27,6 +28,7 @@ class ForwardTest extends TestCase
27 28 {
28 29 public function setUp()
29 30 {
  31 + StaticEventManager::resetInstance();
30 32 $event = new MvcEvent();
31 33 $event->setRequest(new Request());
32 34 $event->setResponse(new Response());
@@ -44,6 +46,11 @@ public function setUp()
44 46 $this->plugin = $this->controller->plugin('forward');
45 47 }
46 48
  49 + public function tearDown()
  50 + {
  51 + StaticEventManager::resetInstance();
  52 + }
  53 +
47 54 public function testPluginWithoutEventAwareControllerRaisesDomainException()
48 55 {
49 56 $controller = new UneventfulController();

0 comments on commit 107aae5

Please sign in to comment.
Something went wrong with that request. Please try again.