Skip to content

Commit

Permalink
Add Controller::components() and addComponent()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
markstory committed Mar 26, 2014
1 parent b01727d commit addaf62
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/Controller/Controller.php
Expand Up @@ -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
Expand Down Expand Up @@ -319,7 +319,29 @@ public function __construct($request = null, $response = null) {
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);
}

/**
Expand Down Expand Up @@ -486,10 +508,11 @@ protected function _loadComponents() {
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']);
}
}

Expand Down Expand Up @@ -677,7 +700,7 @@ public function paginate($object = null) {
}
}

$this->Paginator = $this->Components->load('Paginator');
$this->Paginator = $this->addComponent('Paginator');
if (
!in_array('Paginator', $this->helpers) &&
!array_key_exists('Paginator', $this->helpers)
Expand Down
33 changes: 33 additions & 0 deletions tests/TestCase/Controller/ControllerTest.php
Expand Up @@ -855,4 +855,37 @@ public function testViewPathConventions() {
$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));
}

}

0 comments on commit addaf62

Please sign in to comment.