Browse Source

Make addComponent() also set the property.

This makes it a bit more useful than just being a proxy.
mark_story 12 years ago
parent
commit
cd7cbe869a
2 changed files with 12 additions and 2 deletions
  1. 11 2
      src/Controller/Controller.php
  2. 1 0
      tests/TestCase/Controller/ControllerTest.php

+ 11 - 2
src/Controller/Controller.php

@@ -334,14 +334,23 @@ class Controller extends Object implements EventListener {
 	}
 
 /**
- * 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};
 	}
 
 /**

+ 1 - 0
tests/TestCase/Controller/ControllerTest.php

@@ -883,6 +883,7 @@ class ControllerTest extends TestCase {
 		$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));