diff --git a/src/Controller/ComponentRegistry.php b/src/Controller/ComponentRegistry.php index 415944653ee..b84d321b785 100644 --- a/src/Controller/ComponentRegistry.php +++ b/src/Controller/ComponentRegistry.php @@ -40,13 +40,12 @@ class ComponentRegistry extends ObjectRegistry implements EventDispatcherInterfa /** * Constructor. * - * @param \Cake\Controller\Controller $Controller Controller instance. + * @param \Cake\Controller\Controller $controller Controller instance. */ - public function __construct(Controller $Controller = null) + public function __construct(Controller $controller = null) { - if ($Controller) { - $this->_Controller = $Controller; - $this->eventManager($Controller->eventManager()); + if ($controller) { + $this->setController($controller); } } @@ -60,6 +59,18 @@ public function getController() return $this->_Controller; } + /** + * Set the controller associated with the collection. + * + * @param \Cake\Controller\Controller $controller Controller instance. + * @return void + */ + public function setController(Controller $controller) + { + $this->_Controller = $controller; + $this->eventManager($controller->eventManager()); + } + /** * Resolve a component classname. * diff --git a/src/Controller/Controller.php b/src/Controller/Controller.php index a90bbf326f3..31cfb283dc0 100644 --- a/src/Controller/Controller.php +++ b/src/Controller/Controller.php @@ -227,8 +227,9 @@ class Controller implements EventListenerInterface, EventDispatcherInterface * @param \Cake\Network\Response|null $response Response object for this controller. * @param string|null $name Override the name useful in testing when using mocks. * @param \Cake\Event\EventManager|null $eventManager The event manager. Defaults to a new instance. + * @param \Cake\Controller\ComponentRegistry|null $components The component registry. Defaults to a new instance. */ - public function __construct(Request $request = null, Response $response = null, $name = null, $eventManager = null) + public function __construct(Request $request = null, Response $response = null, $name = null, $eventManager = null, $components = null) { if ($name !== null) { $this->name = $name; @@ -254,6 +255,10 @@ public function __construct(Request $request = null, Response $response = null, $modelClass = ($this->plugin ? $this->plugin . '.' : '') . $this->name; $this->_setModelClass($modelClass); + if ($components !== null) { + $this->components($components); + } + $this->initialize(); $this->_mergeControllerVars(); @@ -276,13 +281,21 @@ public function initialize() /** * Get the component registry for this controller. * + * If called with the first parameter, it will be set as the controller $this->_components property + * + * @param \Cake\Controller\ComponentRegistry|null $components Component registry. + * * @return \Cake\Controller\ComponentRegistry */ - public function components() + public function components($components = null) { - if ($this->_components === null) { + if ($components === null && $this->_components === null) { $this->_components = new ComponentRegistry($this); } + if ($components !== null) { + $components->setController($this); + $this->_components = $components; + } return $this->_components; } diff --git a/tests/TestCase/Controller/ControllerTest.php b/tests/TestCase/Controller/ControllerTest.php index 4eeecd76ab1..09fcfcd6a67 100644 --- a/tests/TestCase/Controller/ControllerTest.php +++ b/tests/TestCase/Controller/ControllerTest.php @@ -911,6 +911,24 @@ public function testComponents() $this->assertSame($result, $controller->components()); } + /** + * Test the components() method with the custom ObjectRegistry. + * + * @return void + */ + public function testComponentsWithCustomRegistry() + { + $request = new Request('/'); + $response = $this->getMock('Cake\Network\Response'); + $componentRegistry = $this->getMock('Cake\Controller\ComponentRegistry', ['offsetGet']); + + $controller = new TestController($request, $response, null, null, $componentRegistry); + $this->assertInstanceOf(get_class($componentRegistry), $controller->components()); + + $result = $controller->components(); + $this->assertSame($result, $controller->components()); + } + /** * Test adding a component *