Browse Source

Add Controller::components() and addComponent()

These methods aim to increase cohesion and consistency between
Controller/View/Table classes by having similar methods with the same
purpose people have fewer conventions to learn.
mark_story 12 years ago
parent
commit
addaf620ad
2 changed files with 62 additions and 6 deletions
  1. 29 6
      src/Controller/Controller.php
  2. 33 0
      tests/TestCase/Controller/ControllerTest.php

+ 29 - 6
src/Controller/Controller.php

@@ -195,9 +195,9 @@ class Controller extends Object implements EventListener {
 /**
  * Instance of ComponentRegistry used to create Components
  *
- * @var ComponentRegistry
+ * @var \Cake\Controller\ComponentRegistry
  */
-	public $Components = null;
+	public $_components = null;
 
 /**
  * Array containing the names of components this controller uses. Component names
@@ -319,7 +319,29 @@ class Controller extends Object implements EventListener {
 		if ($response instanceof Response) {
 			$this->response = $response;
 		}
-		$this->Components = new ComponentRegistry($this);
+	}
+
+/**
+ * Get the component registry for this controller.
+ *
+ * @return \Cake\Controller\ComponentRegistry
+ */
+	public function components() {
+		if ($this->_components === null) {
+			$this->_components = new ComponentRegistry($this);
+		}
+		return $this->_components;
+	}
+
+/**
+ * Add a component to the controller's registry
+ *
+ * @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);
 	}
 
 /**
@@ -486,10 +508,11 @@ class Controller extends Object implements EventListener {
 		if (empty($this->components)) {
 			return;
 		}
-		$components = $this->Components->normalizeArray($this->components);
+		$registry = $this->components();
+		$components = $registry->normalizeArray($this->components);
 		foreach ($components as $properties) {
 			list(, $class) = pluginSplit($properties['class']);
-			$this->{$class} = $this->Components->load($properties['class'], $properties['settings']);
+			$this->{$class} = $registry->load($properties['class'], $properties['settings']);
 		}
 	}
 
@@ -677,7 +700,7 @@ class Controller extends Object implements EventListener {
 			}
 		}
 
-		$this->Paginator = $this->Components->load('Paginator');
+		$this->Paginator = $this->addComponent('Paginator');
 		if (
 			!in_array('Paginator', $this->helpers) &&
 			!array_key_exists('Paginator', $this->helpers)

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

@@ -855,4 +855,37 @@ class ControllerTest extends TestCase {
 		$this->assertEquals('Pages', $Controller->viewPath);
 	}
 
+/**
+ * Test the components() method.
+ *
+ * @return void
+ */
+	public function testComponents() {
+		$request = new Request('/');
+		$response = $this->getMock('Cake\Network\Response');
+
+		$controller = new TestController($request, $response);
+		$this->assertInstanceOf('Cake\Controller\ComponentRegistry', $controller->components());
+
+		$result = $controller->components();
+		$this->assertSame($result, $controller->components());
+	}
+
+/**
+ * Test adding a component
+ *
+ * @return void
+ */
+	public function testAddComponent() {
+		$request = new Request('/');
+		$response = $this->getMock('Cake\Network\Response');
+
+		$controller = new TestController($request, $response);
+		$result = $controller->addComponent('Paginator');
+		$this->assertInstanceOf('Cake\Controller\Component\PaginatorComponent', $result);
+
+		$registry = $controller->components();
+		$this->assertTrue(isset($registry->Paginator));
+	}
+
 }