Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'weierophinney/feature/eventmanager-shar…
Browse files Browse the repository at this point in the history
…edinstance'
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/EventManager.php
Expand Up @@ -85,6 +85,7 @@ public function setEventClass($class)
public function setSharedManager(SharedEventManagerInterface $sharedEventManager)
{
$this->sharedManager = $sharedEventManager;
StaticEventManager::setInstance($sharedEventManager);
return $this;
}

Expand All @@ -101,13 +102,28 @@ 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());
// "false" means "I do not want a shared manager; don't try and fetch one"
if (false === $this->sharedManager
|| $this->sharedManager instanceof SharedEventManagerInterface
) {
return $this->sharedManager;
}

if (!StaticEventManager::hasInstance()) {
return false;
}

$this->sharedManager = StaticEventManager::getInstance();
return $this->sharedManager;
}

Expand Down
23 changes: 22 additions & 1 deletion src/StaticEventManager.php
Expand Up @@ -51,11 +51,32 @@ 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
*
Expand Down
21 changes: 21 additions & 0 deletions test/EventManagerTest.php
Expand Up @@ -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;

Expand All @@ -35,6 +36,12 @@ public function setUp()
unset($this->message);
}
$this->events = new EventManager;
StaticEventManager::resetInstance();
}

public function tearDown()
{
StaticEventManager::resetInstance();
}

public function testAttachShouldReturnCallbackHandler()
Expand Down Expand Up @@ -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());
}
}

0 comments on commit a8b3425

Please sign in to comment.