Skip to content

Commit f108089

Browse files
committed
Update Controller to use MergeVariablesTrait
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.
1 parent 933a7e6 commit f108089

File tree

5 files changed

+38
-279
lines changed

5 files changed

+38
-279
lines changed

lib/Cake/Controller/Controller.php

Lines changed: 19 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
*
99
* @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
1010
* @link http://cakephp.org CakePHP(tm) Project
11-
* @package Cake.Controller
1211
* @since CakePHP(tm) v 0.2.9
1312
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
1413
*/
1514

1615
namespace Cake\Controller;
16+
1717
use Cake\Core\App;
1818
use Cake\Core\Configure;
1919
use Cake\Core\Object;
@@ -27,6 +27,7 @@
2727
use Cake\Routing\Router;
2828
use Cake\Utility\ClassRegistry;
2929
use Cake\Utility\Inflector;
30+
use Cake\Utility\MergeVariablesTrait;
3031
use Cake\View\View;
3132

3233
/**
@@ -63,6 +64,8 @@
6364
*/
6465
class Controller extends Object implements EventListener {
6566

67+
use MergeVariablesTrait;
68+
6669
/**
6770
* The name of this controller. Controller names are plural, named after the model they manipulate.
6871
*
@@ -298,15 +301,6 @@ class Controller extends Object implements EventListener {
298301
*/
299302
public $validationErrors = null;
300303

301-
/**
302-
* The class name of the parent class you wish to merge with.
303-
* Typically this is AppController, but you may wish to merge vars with a different
304-
* parent class.
305-
*
306-
* @var string
307-
*/
308-
protected $_mergeParent = null;
309-
310304
/**
311305
* Instance of the Cake\Event\EventManager this controller is using
312306
* to dispatch inner events.
@@ -539,71 +533,32 @@ protected function _getScaffold(Request $request) {
539533
* @return void
540534
*/
541535
protected function _mergeControllerVars() {
542-
$pluginController = $pluginDot = null;
543-
$mergeParent = is_subclass_of($this, $this->_mergeParent);
544-
$pluginVars = array();
545-
$appVars = array();
546-
536+
$pluginDot = null;
547537
if (!empty($this->plugin)) {
548-
$pluginController = Plugin::getNamespace($this->plugin) . '\Controller\Controller';
549-
if (!is_subclass_of($this, $pluginController)) {
550-
$pluginController = null;
551-
}
552538
$pluginDot = $this->plugin . '.';
553539
}
554-
555-
if ($pluginController) {
556-
$merge = array('components', 'helpers');
557-
$this->_mergeVars($merge, $pluginController);
558-
}
559-
560-
if ($mergeParent || !empty($pluginController)) {
561-
$appVars = get_class_vars($this->_mergeParent);
562-
$merge = array('components', 'helpers');
563-
$this->_mergeVars($merge, $this->_mergeParent, true);
564-
}
565-
566540
if ($this->uses === null) {
567541
$this->uses = false;
568542
}
543+
if ($this->uses === false) {
544+
$this->uses = [];
545+
$this->modelClass = '';
546+
}
569547
if ($this->uses === true) {
570548
$this->uses = array($pluginDot . $this->modelClass);
571549
}
572-
if (isset($appVars['uses']) && $appVars['uses'] === $this->uses) {
550+
$oldUses = $this->uses;
551+
$this->_mergeVars(
552+
['components', 'helpers', 'uses'],
553+
[
554+
'associative' => ['components', 'helpers'],
555+
'reverse' => ['uses']
556+
]
557+
);
558+
if ($this->uses === $oldUses) {
573559
array_unshift($this->uses, $pluginDot . $this->modelClass);
574560
}
575-
if ($pluginController) {
576-
$pluginVars = get_class_vars($pluginController);
577-
}
578-
if ($this->uses !== false) {
579-
$this->_mergeUses($pluginVars);
580-
$this->_mergeUses($appVars);
581-
} else {
582-
$this->uses = array();
583-
$this->modelClass = '';
584-
}
585-
}
586-
587-
/**
588-
* Helper method for merging the $uses property together.
589-
*
590-
* Merges the elements not already in $this->uses into
591-
* $this->uses.
592-
*
593-
* @param array $merge The data to merge in.
594-
* @return void
595-
*/
596-
protected function _mergeUses($merge) {
597-
if (!isset($merge['uses'])) {
598-
return;
599-
}
600-
if ($merge['uses'] === true) {
601-
return;
602-
}
603-
$this->uses = array_merge(
604-
$this->uses,
605-
array_diff($merge['uses'], $this->uses)
606-
);
561+
$this->uses = array_unique($this->uses);
607562
}
608563

609564
/**

lib/Cake/Test/TestApp/Controller/CommentsController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,4 @@
2323
*/
2424
class CommentsController extends AppController {
2525

26-
protected $_mergeParent = 'ControllerTestAppController';
2726
}

lib/Cake/Test/TestCase/AllControllerTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* @package Cake.Test.Case
2828
*/
29-
class AllControllersTest extends \PHPUnit_Framework_TestSuite {
29+
class AllControllerTest extends \PHPUnit_Framework_TestSuite {
3030

3131
/**
3232
* suite method, defines tests for this suite.
@@ -40,7 +40,6 @@ public static function suite() {
4040
$suite->addTestFile(CORE_TEST_CASES . DS . 'Controller/ScaffoldTest.php');
4141
$suite->addTestFile(CORE_TEST_CASES . DS . 'Controller/PagesControllerTest.php');
4242
$suite->addTestFile(CORE_TEST_CASES . DS . 'Controller/ComponentTest.php');
43-
$suite->addTestFile(CORE_TEST_CASES . DS . 'Controller/ControllerMergeVarsTest.php');
4443
return $suite;
4544
}
46-
}
45+
}

lib/Cake/Test/TestCase/Controller/ControllerMergeVarsTest.php

Lines changed: 0 additions & 171 deletions
This file was deleted.

0 commit comments

Comments
 (0)