Permalink
Browse files

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...
1 parent 30ee79b commit 107aae55cee590af4d336d416e7f98fbdb7639c0 @weierophinney weierophinney committed Jul 24, 2012
@@ -85,6 +85,7 @@ public function setEventClass($class)
public function setSharedManager(SharedEventManagerInterface $sharedEventManager)
{
$this->sharedManager = $sharedEventManager;
+ StaticEventManager::setInstance($sharedEventManager);
return $this;
}
@@ -101,13 +102,27 @@ public function unsetSharedManager()
/**
* Get shared event manager
*
+ * If one is not defined, but we have a static instance in
+ * StaticEventManager, that one will be used and set in this instance.
+ *
+ * If none is available in the StaticEventManager, a boolean false is
+ * returned.
+ *
* @return false|SharedEventManagerInterface
*/
public function getSharedManager()
{
- if (null === $this->sharedManager) {
- $this->setSharedManager(StaticEventManager::getInstance());
+ if (false === $this->sharedManager
+ || $this->sharedManager instanceof SharedEventManagerInterface
+ ) {
+ return $this->sharedManager;
+ }
+
+ if (!StaticEventManager::hasInstance()) {
+ return false;
}
+
+ $this->sharedManager = StaticEventManager::getInstance();
return $this->sharedManager;
}
@@ -51,12 +51,33 @@ private function __clone()
public static function getInstance()
{
if (null === static::$instance) {
- static::$instance = new static();
+ static::setInstance(new static());
}
return static::$instance;
}
/**
+ * Set the singleton to a specific SharedEventManagerInterface instance
+ *
+ * @param SharedEventManagerInterface $instance
+ * @return void
+ */
+ public static function setInstance(SharedEventManagerInterface $instance)
+ {
+ static::$instance = $instance;
+ }
+
+ /**
+ * Is a singleton instance defined?
+ *
+ * @return bool
+ */
+ public static function hasInstance()
+ {
+ return (static::$instance instanceof SharedEventManagerInterface);
+ }
+
+ /**
* Reset the singleton instance
*
* @return void
@@ -16,6 +16,7 @@
use Zend\EventManager\EventInterface;
use Zend\EventManager\EventManager;
use Zend\EventManager\ResponseCollection;
+use Zend\EventManager\SharedEventManager;
use Zend\EventManager\StaticEventManager;
use Zend\Stdlib\CallbackHandler;
@@ -35,6 +36,12 @@ public function setUp()
unset($this->message);
}
$this->events = new EventManager;
+ StaticEventManager::resetInstance();
+ }
+
+ public function tearDown()
+ {
+ StaticEventManager::resetInstance();
}
public function testAttachShouldReturnCallbackHandler()
@@ -596,4 +603,18 @@ public function testListenersAttachedWithWildcardAreTriggeredForAllEvents()
$this->assertContains($event, $test->events);
}
}
+
+ public function testSettingSharedEventManagerSetsStaticEventManagerInstance()
+ {
+ $shared = new SharedEventManager();
+ $this->events->setSharedManager($shared);
+ $this->assertSame($shared, $this->events->getSharedManager());
+ $this->assertSame($shared, StaticEventManager::getInstance());
+ }
+
+ public function testDoesNotCreateStaticInstanceIfNonePresent()
+ {
+ StaticEventManager::resetInstance();
+ $this->assertFalse($this->events->getSharedManager());
+ }
}
@@ -12,6 +12,7 @@
use PHPUnit_Framework_TestCase as TestCase;
use stdClass;
+use Zend\EventManager\StaticEventManager;
use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Mvc\Controller\Plugin\Forward as ForwardPlugin;
@@ -27,6 +28,7 @@ class ForwardTest extends TestCase
{
public function setUp()
{
+ StaticEventManager::resetInstance();
$event = new MvcEvent();
$event->setRequest(new Request());
$event->setResponse(new Response());
@@ -44,6 +46,11 @@ public function setUp()
$this->plugin = $this->controller->plugin('forward');
}
+ public function tearDown()
+ {
+ StaticEventManager::resetInstance();
+ }
+
public function testPluginWithoutEventAwareControllerRaisesDomainException()
{
$controller = new UneventfulController();

0 comments on commit 107aae5

Please sign in to comment.