mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Adding a reference of the controller to the component collection, so components can easily access the controller in their constructor.
This commit is contained in:
parent
7a14d3a8f2
commit
0ef76eb69a
2 changed files with 31 additions and 0 deletions
|
@ -20,6 +20,13 @@ App::import('Core', 'ObjectCollection');
|
|||
|
||||
class ComponentCollection extends ObjectCollection {
|
||||
|
||||
/**
|
||||
* The controller that this collection was initialized with.
|
||||
*
|
||||
* @var Controller
|
||||
*/
|
||||
protected $_Controller = null;
|
||||
|
||||
/**
|
||||
* Initializes all the Components for a controller.
|
||||
* Attaches a reference of each component to the Controller.
|
||||
|
@ -31,12 +38,22 @@ class ComponentCollection extends ObjectCollection {
|
|||
if (empty($Controller->components)) {
|
||||
return;
|
||||
}
|
||||
$this->_Controller = $Controller;
|
||||
$components = ComponentCollection::normalizeObjectArray($Controller->components);
|
||||
foreach ($components as $name => $properties) {
|
||||
$Controller->{$name} = $this->load($properties['class'], $properties['settings']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the controller associated with the collection.
|
||||
*
|
||||
* @return Controller.
|
||||
*/
|
||||
public function getController() {
|
||||
return $this->_Controller;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads/constructs a component. Will return the instance in the registry if it already exists.
|
||||
*
|
||||
|
|
|
@ -274,4 +274,18 @@ class ComponentCollectionTest extends CakeTestCase {
|
|||
);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* test getting the controller out of the collection
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function testGetController() {
|
||||
$controller = $this->getMock('Controller');
|
||||
$controller->components = array('Security');
|
||||
$this->Components->init($controller);
|
||||
$result = $this->Components->getController();
|
||||
|
||||
$this->assertSame($controller, $result);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue