Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.0 merge vars #901

Merged
merged 10 commits into from
Oct 15, 2012
13 changes: 6 additions & 7 deletions lib/Cake/Console/Shell.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Cake\Utility\ClassRegistry;
use Cake\Utility\File;
use Cake\Utility\Inflector;
use Cake\Utility\MergeVariablesTrait;
use Cake\Utility\String;

/**
Expand All @@ -36,6 +37,7 @@
*/
class Shell extends Object {

use MergeVariablesTrait;
/**
* Output constants for making verbose and quiet shells.
*/
Expand Down Expand Up @@ -179,13 +181,10 @@ public function __construct($stdout = null, $stderr = null, $stdin = null) {
$this->stdin = new ConsoleInput('php://stdin');
}
$this->_useLogger();
$parent = get_parent_class($this);
if ($this->tasks !== null && $this->tasks !== false) {
$this->_mergeVars(array('tasks'), $parent, true);
}
if ($this->uses !== null && $this->uses !== false) {
$this->_mergeVars(array('uses'), $parent, false);
}
$this->_mergeVars(
['tasks', 'uses'],
['associative' => ['tasks']]
);
}

/**
Expand Down
90 changes: 21 additions & 69 deletions lib/Cake/Controller/Controller.php
Original file line number Diff line number Diff line change
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 @@ -335,9 +329,6 @@ public function __construct($request = null, $response = null) {
}
$this->viewPath = $viewPath;
}
if ($this->_mergeParent === null) {
$this->_mergeParent = Configure::read('App.namespace') . '\Controller\Controller';
}

$this->modelClass = Inflector::singularize($this->name);
$this->modelKey = Inflector::underscore($this->modelClass);
Expand Down Expand Up @@ -534,76 +525,37 @@ protected function _getScaffold(Request $request) {

/**
* Merge components, helpers, and uses vars from
* Controller::$_mergeParent and PluginAppController.
* parent classes.
*
* @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 === true) {
$this->uses = array($pluginDot . $this->modelClass);
}
if (isset($appVars['uses']) && $appVars['uses'] === $this->uses) {
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();
if ($this->uses === false) {
$this->uses = [];
$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;
if ($this->uses === true) {
$this->uses = [$pluginDot . $this->modelClass];
}
$this->uses = array_merge(
$this->uses,
array_diff($merge['uses'], $this->uses)
$this->_mergeVars(
['components', 'helpers', 'uses'],
[
'associative' => ['components', 'helpers'],
'reverse' => ['uses']
]
);
$usesProperty = new \ReflectionProperty($this, 'uses');
if ($this->uses && $usesProperty->getDeclaringClass()->getName() !== get_class($this)) {
array_unshift($this->uses, $pluginDot . $this->modelClass);
}
$this->uses = array_unique($this->uses);
}

/**
Expand Down
30 changes: 0 additions & 30 deletions lib/Cake/Core/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,34 +174,4 @@ protected function _set($properties = array()) {
}
}

/**
* Merges this objects $property with the property in $class' definition.
* This classes value for the property will be merged on top of $class'
*
* This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine
* this method as an empty function.
*
* @param array $properties The name of the properties to merge.
* @param string $class The class to merge the property with.
* @param boolean $normalize Set to true to run the properties through Hash::normalize() before merging.
* @return void
*/
protected function _mergeVars($properties, $class, $normalize = true) {
$classProperties = get_class_vars($class);
foreach ($properties as $var) {
if (
isset($classProperties[$var]) &&
!empty($classProperties[$var]) &&
is_array($this->{$var}) &&
$this->{$var} != $classProperties[$var]
) {
if ($normalize) {
$classProperties[$var] = Hash::normalize($classProperties[$var]);
$this->{$var} = Hash::normalize($this->{$var});
}
$this->{$var} = Hash::merge($classProperties[$var], $this->{$var});
}
}
}

}
19 changes: 8 additions & 11 deletions lib/Cake/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
* @since CakePHP(tm) v 0.10.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

namespace Cake\Model;

use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Object;
Expand All @@ -30,6 +30,7 @@
use Cake\Utility\ClassRegistry;
use Cake\Utility\Hash;
use Cake\Utility\Inflector;
use Cake\Utility\MergeVariablesTrait;
use Cake\Utility\Set;
use Cake\Utility\Xml;

Expand All @@ -46,6 +47,8 @@
*/
class Model extends Object implements EventListener {

use MergeVariablesTrait;

/**
* The name of the DataSource connection that this Model uses
*
Expand Down Expand Up @@ -714,16 +717,10 @@ public function __construct($id = false, $table = null, $ds = null) {
$this->useDbConfig = $ds;
}

$appModelClass = App::classname('Model', 'Model');
if (is_subclass_of($this, $appModelClass)) {
$merge = array('actsAs', 'findMethods');
$parentClass = get_parent_class($this);
if ($parentClass !== $appModelClass) {
$this->_mergeVars($merge, $parentClass);
}
$this->_mergeVars($merge, $appModelClass);
}
$this->_mergeVars(array('findMethods'), 'Model');
$this->_mergeVars(
['actsAs', 'findMethods'],
['associative' => ['actsAs', 'findMethods']]
);

$this->Behaviors = new BehaviorCollection();

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

protected $_mergeParent = 'ControllerTestAppController';
}
48 changes: 0 additions & 48 deletions lib/Cake/Test/TestApp/Controller/MergeVariablesController.php

This file was deleted.

41 changes: 0 additions & 41 deletions lib/Cake/Test/TestApp/Controller/MergeVarsAppController.php

This file was deleted.

4 changes: 2 additions & 2 deletions lib/Cake/Test/TestApp/Controller/RequestActionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
* @package Cake.Test.TestApp.Controller
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
Expand All @@ -21,6 +20,7 @@
/**
* RequestActionController class
*
* @package Cake.Test.TestApp.Controller
*/
class RequestActionController extends AppController {

Expand All @@ -30,7 +30,7 @@ class RequestActionController extends AppController {
* @var array
* @access public
*/
public $uses = array('RequestActionPost');
public $uses = ['Post'];

/**
* test_request_action method
Expand Down
Loading