Skip to content

Commit

Permalink
Update Controller to use MergeVariablesTrait
Browse files Browse the repository at this point in the history
Remove a test case that handled the many edge cases with controller
merge vars as those cases should be covered by tests for the trait now.
  • Loading branch information
markstory committed Oct 14, 2012
1 parent 933a7e6 commit f108089
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 279 deletions.
83 changes: 19 additions & 64 deletions lib/Cake/Controller/Controller.php
Expand Up @@ -8,12 +8,12 @@
*
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Controller
* @since CakePHP(tm) v 0.2.9
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

namespace Cake\Controller;

use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Object;
Expand All @@ -27,6 +27,7 @@
use Cake\Routing\Router;
use Cake\Utility\ClassRegistry;
use Cake\Utility\Inflector;
use Cake\Utility\MergeVariablesTrait;
use Cake\View\View;

/**
Expand Down Expand Up @@ -63,6 +64,8 @@
*/
class Controller extends Object implements EventListener {

use MergeVariablesTrait;

/**
* The name of this controller. Controller names are plural, named after the model they manipulate.
*
Expand Down Expand Up @@ -298,15 +301,6 @@ class Controller extends Object implements EventListener {
*/
public $validationErrors = null;

/**
* The class name of the parent class you wish to merge with.
* Typically this is AppController, but you may wish to merge vars with a different
* parent class.
*
* @var string
*/
protected $_mergeParent = null;

/**
* Instance of the Cake\Event\EventManager this controller is using
* to dispatch inner events.
Expand Down Expand Up @@ -539,71 +533,32 @@ protected function _getScaffold(Request $request) {
* @return void
*/
protected function _mergeControllerVars() {
$pluginController = $pluginDot = null;
$mergeParent = is_subclass_of($this, $this->_mergeParent);
$pluginVars = array();
$appVars = array();

$pluginDot = null;
if (!empty($this->plugin)) {
$pluginController = Plugin::getNamespace($this->plugin) . '\Controller\Controller';
if (!is_subclass_of($this, $pluginController)) {
$pluginController = null;
}
$pluginDot = $this->plugin . '.';
}

if ($pluginController) {
$merge = array('components', 'helpers');
$this->_mergeVars($merge, $pluginController);
}

if ($mergeParent || !empty($pluginController)) {
$appVars = get_class_vars($this->_mergeParent);
$merge = array('components', 'helpers');
$this->_mergeVars($merge, $this->_mergeParent, true);
}

if ($this->uses === null) {
$this->uses = false;
}
if ($this->uses === false) {
$this->uses = [];
$this->modelClass = '';
}
if ($this->uses === true) {
$this->uses = array($pluginDot . $this->modelClass);
}
if (isset($appVars['uses']) && $appVars['uses'] === $this->uses) {
$oldUses = $this->uses;
$this->_mergeVars(
['components', 'helpers', 'uses'],
[
'associative' => ['components', 'helpers'],
'reverse' => ['uses']
]
);
if ($this->uses === $oldUses) {
array_unshift($this->uses, $pluginDot . $this->modelClass);
}
if ($pluginController) {
$pluginVars = get_class_vars($pluginController);
}
if ($this->uses !== false) {
$this->_mergeUses($pluginVars);
$this->_mergeUses($appVars);
} else {
$this->uses = array();
$this->modelClass = '';
}
}

/**
* Helper method for merging the $uses property together.
*
* Merges the elements not already in $this->uses into
* $this->uses.
*
* @param array $merge The data to merge in.
* @return void
*/
protected function _mergeUses($merge) {
if (!isset($merge['uses'])) {
return;
}
if ($merge['uses'] === true) {
return;
}
$this->uses = array_merge(
$this->uses,
array_diff($merge['uses'], $this->uses)
);
$this->uses = array_unique($this->uses);
}

/**
Expand Down
1 change: 0 additions & 1 deletion lib/Cake/Test/TestApp/Controller/CommentsController.php
Expand Up @@ -23,5 +23,4 @@
*/
class CommentsController extends AppController {

protected $_mergeParent = 'ControllerTestAppController';
}
5 changes: 2 additions & 3 deletions lib/Cake/Test/TestCase/AllControllerTest.php
Expand Up @@ -26,7 +26,7 @@
*
* @package Cake.Test.Case
*/
class AllControllersTest extends \PHPUnit_Framework_TestSuite {
class AllControllerTest extends \PHPUnit_Framework_TestSuite {

/**
* suite method, defines tests for this suite.
Expand All @@ -40,7 +40,6 @@ public static function suite() {
$suite->addTestFile(CORE_TEST_CASES . DS . 'Controller/ScaffoldTest.php');
$suite->addTestFile(CORE_TEST_CASES . DS . 'Controller/PagesControllerTest.php');
$suite->addTestFile(CORE_TEST_CASES . DS . 'Controller/ComponentTest.php');
$suite->addTestFile(CORE_TEST_CASES . DS . 'Controller/ControllerMergeVarsTest.php');
return $suite;
}
}
}
171 changes: 0 additions & 171 deletions lib/Cake/Test/TestCase/Controller/ControllerMergeVarsTest.php

This file was deleted.

0 comments on commit f108089

Please sign in to comment.