Skip to content

Commit

Permalink
Make addComponent() also set the property.
Browse files Browse the repository at this point in the history
This makes it a bit more useful than just being a proxy.
  • Loading branch information
markstory committed Mar 27, 2014
1 parent 8f3612e commit cd7cbe8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Controller/Controller.php
Expand Up @@ -334,14 +334,23 @@ public function components() {
}

/**
* Add a component to the controller's registry
* Add a component to the controller's registry.
*
* This method will also set the component to a property.
* For example:
*
* `$this->addComponent('DebugKit.Toolbar');`
*
* Will result in a `Toolbar` property being set.
*
* @param string $name The name of the component to load.
* @param array $config The config for the component.
* @return \Cake\Controller\Component
*/
public function addComponent($name, $config = []) {
return $this->components()->load($name, $config);
list(, $prop) = pluginSplit($name);
$this->{$prop} = $this->components()->load($name, $config);
return $this->{$prop};
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase/Controller/ControllerTest.php
Expand Up @@ -883,6 +883,7 @@ public function testAddComponent() {
$controller = new TestController($request, $response);
$result = $controller->addComponent('Paginator');
$this->assertInstanceOf('Cake\Controller\Component\PaginatorComponent', $result);
$this->assertSame($result, $controller->Paginator);

$registry = $controller->components();
$this->assertTrue(isset($registry->Paginator));
Expand Down

0 comments on commit cd7cbe8

Please sign in to comment.