Skip to content

Commit

Permalink
[Fixes #7300] Add ability to set custom ComponentRegistry to Controll…
Browse files Browse the repository at this point in the history
…er object
  • Loading branch information
korotovsky committed Aug 25, 2015
1 parent cd1207b commit 38a6dde
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
21 changes: 16 additions & 5 deletions src/Controller/ComponentRegistry.php
Expand Up @@ -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);
}
}

Expand All @@ -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.
*
Expand Down
19 changes: 16 additions & 3 deletions src/Controller/Controller.php
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/TestCase/Controller/ControllerTest.php
Expand Up @@ -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
*
Expand Down

0 comments on commit 38a6dde

Please sign in to comment.