Skip to content

Commit 38a6dde

Browse files
committed
[Fixes #7300] Add ability to set custom ComponentRegistry to Controller object
1 parent cd1207b commit 38a6dde

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

src/Controller/ComponentRegistry.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@ class ComponentRegistry extends ObjectRegistry implements EventDispatcherInterfa
4040
/**
4141
* Constructor.
4242
*
43-
* @param \Cake\Controller\Controller $Controller Controller instance.
43+
* @param \Cake\Controller\Controller $controller Controller instance.
4444
*/
45-
public function __construct(Controller $Controller = null)
45+
public function __construct(Controller $controller = null)
4646
{
47-
if ($Controller) {
48-
$this->_Controller = $Controller;
49-
$this->eventManager($Controller->eventManager());
47+
if ($controller) {
48+
$this->setController($controller);
5049
}
5150
}
5251

@@ -60,6 +59,18 @@ public function getController()
6059
return $this->_Controller;
6160
}
6261

62+
/**
63+
* Set the controller associated with the collection.
64+
*
65+
* @param \Cake\Controller\Controller $controller Controller instance.
66+
* @return void
67+
*/
68+
public function setController(Controller $controller)
69+
{
70+
$this->_Controller = $controller;
71+
$this->eventManager($controller->eventManager());
72+
}
73+
6374
/**
6475
* Resolve a component classname.
6576
*

src/Controller/Controller.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,9 @@ class Controller implements EventListenerInterface, EventDispatcherInterface
227227
* @param \Cake\Network\Response|null $response Response object for this controller.
228228
* @param string|null $name Override the name useful in testing when using mocks.
229229
* @param \Cake\Event\EventManager|null $eventManager The event manager. Defaults to a new instance.
230+
* @param \Cake\Controller\ComponentRegistry|null $components The component registry. Defaults to a new instance.
230231
*/
231-
public function __construct(Request $request = null, Response $response = null, $name = null, $eventManager = null)
232+
public function __construct(Request $request = null, Response $response = null, $name = null, $eventManager = null, $components = null)
232233
{
233234
if ($name !== null) {
234235
$this->name = $name;
@@ -254,6 +255,10 @@ public function __construct(Request $request = null, Response $response = null,
254255
$modelClass = ($this->plugin ? $this->plugin . '.' : '') . $this->name;
255256
$this->_setModelClass($modelClass);
256257

258+
if ($components !== null) {
259+
$this->components($components);
260+
}
261+
257262
$this->initialize();
258263

259264
$this->_mergeControllerVars();
@@ -276,13 +281,21 @@ public function initialize()
276281
/**
277282
* Get the component registry for this controller.
278283
*
284+
* If called with the first parameter, it will be set as the controller $this->_components property
285+
*
286+
* @param \Cake\Controller\ComponentRegistry|null $components Component registry.
287+
*
279288
* @return \Cake\Controller\ComponentRegistry
280289
*/
281-
public function components()
290+
public function components($components = null)
282291
{
283-
if ($this->_components === null) {
292+
if ($components === null && $this->_components === null) {
284293
$this->_components = new ComponentRegistry($this);
285294
}
295+
if ($components !== null) {
296+
$components->setController($this);
297+
$this->_components = $components;
298+
}
286299
return $this->_components;
287300
}
288301

tests/TestCase/Controller/ControllerTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,24 @@ public function testComponents()
911911
$this->assertSame($result, $controller->components());
912912
}
913913

914+
/**
915+
* Test the components() method with the custom ObjectRegistry.
916+
*
917+
* @return void
918+
*/
919+
public function testComponentsWithCustomRegistry()
920+
{
921+
$request = new Request('/');
922+
$response = $this->getMock('Cake\Network\Response');
923+
$componentRegistry = $this->getMock('Cake\Controller\ComponentRegistry', ['offsetGet']);
924+
925+
$controller = new TestController($request, $response, null, null, $componentRegistry);
926+
$this->assertInstanceOf(get_class($componentRegistry), $controller->components());
927+
928+
$result = $controller->components();
929+
$this->assertSame($result, $controller->components());
930+
}
931+
914932
/**
915933
* Test adding a component
916934
*

0 commit comments

Comments
 (0)