Skip to content

Commit

Permalink
[zendframework#3650] Do not re-inject EM instance
Browse files Browse the repository at this point in the history
- If the controller already composes an EM instance, do not re-inject
  it. This allows factories to inject an EM instance and attach
  listeners.
  • Loading branch information
weierophinney committed Feb 1, 2013
1 parent 1c3a27e commit a4353b8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
13 changes: 12 additions & 1 deletion library/Zend/Mvc/Controller/ControllerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Zend\Mvc\Controller;

use Zend\EventManager\EventManagerAwareInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\Exception;
use Zend\ServiceManager\AbstractPluginManager;
use Zend\ServiceManager\ConfigInterface;
Expand Down Expand Up @@ -66,7 +67,17 @@ public function injectControllerDependencies($controller, ServiceLocatorInterfac
}

if ($controller instanceof EventManagerAwareInterface) {
$controller->setEventManager($parentLocator->get('EventManager'));
// If we have an event manager composed already, make sure it gets
// injected with the shared event manager.
// The AbstractController lazy-instantiates an EM instance, which
// is why the shared EM injection needs to happen; the conditional
// will always pass.
$events = $controller->getEventManager();
if (!$events instanceof EventManagerInterface) {
$controller->setEventManager($parentLocator->get('EventManager'));
} else {
$events->setSharedManager($parentLocator->get('SharedEventManager'));
}
}

if (method_exists($controller, 'setPluginManager')) {
Expand Down
62 changes: 62 additions & 0 deletions tests/ZendTest/Mvc/Controller/ControllerManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Mvc\Controller;

use PHPUnit_Framework_TestCase as TestCase;
use Zend\EventManager\EventManager;
use Zend\EventManager\SharedEventManager;
use Zend\Mvc\Controller\ControllerManager;
use Zend\Mvc\Controller\PluginManager as ControllerPluginManager;
use Zend\ServiceManager\ServiceManager;

class ControllerManagerTest extends TestCase
{
public function setUp()
{
$this->events = new EventManager();
$this->sharedEvents = new SharedEventManager;
$this->events->setSharedManager($this->sharedEvents);

$this->plugins = new ControllerPluginManager();
$this->services = new ServiceManager();
$this->services->setService('Zend\ServiceManager\ServiceLocatorInterface', $this->services);
$this->services->setService('EventManager', $this->events);
$this->services->setService('SharedEventManager', $this->sharedEvents);
$this->services->setService('ControllerPluginManager', $this->plugins);

$this->controllers = new ControllerManager();
$this->controllers->setServiceLocator($this->services);
}

public function testInjectControllerDependenciesInjectsExpectedDependencies()
{
$controller = new TestAsset\SampleController();
$this->controllers->injectControllerDependencies($controller, $this->controllers);
$this->assertSame($this->services, $controller->getServiceLocator());
$this->assertSame($this->plugins, $controller->getPluginManager());

// The default AbstractController implementation lazy instantiates an EM
// instance, which means we need to check that that instance gets injected
// with the shared EM instance.
$events = $controller->getEventManager();
$this->assertInstanceOf('Zend\EventManager\EventManagerInterface', $events);
$this->assertSame($this->sharedEvents, $events->getSharedManager());
}

public function testInjectControllerDependenciesWillNotOverwriteExistingEventManager()
{
$events = new EventManager();
$controller = new TestAsset\SampleController();
$controller->setEventManager($events);
$this->controllers->injectControllerDependencies($controller, $this->controllers);
$this->assertSame($events, $controller->getEventManager());
$this->assertSame($this->sharedEvents, $events->getSharedManager());
}
}

0 comments on commit a4353b8

Please sign in to comment.