mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
Components can have settings assigned in the components declaration, tests added. Closes #4055
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7112 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
7c6cdb6354
commit
55bcf1e25e
4 changed files with 129 additions and 15 deletions
|
@ -43,7 +43,14 @@ class Component extends Object {
|
||||||
* @var object
|
* @var object
|
||||||
* @access private
|
* @access private
|
||||||
*/
|
*/
|
||||||
var $__loaded = array();
|
var $__loaded = array();
|
||||||
|
/**
|
||||||
|
* Settings for loaded components.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
* @access private
|
||||||
|
**/
|
||||||
|
var $__settings = array();
|
||||||
/**
|
/**
|
||||||
* Used to initialize the components for current controller
|
* Used to initialize the components for current controller
|
||||||
*
|
*
|
||||||
|
@ -71,7 +78,11 @@ class Component extends Object {
|
||||||
function initialize(&$controller) {
|
function initialize(&$controller) {
|
||||||
foreach ($this->__loaded as $name => $component) {
|
foreach ($this->__loaded as $name => $component) {
|
||||||
if (method_exists($component,'initialize') && $component->enabled === true) {
|
if (method_exists($component,'initialize') && $component->enabled === true) {
|
||||||
$component->initialize($controller);
|
$settings = array();
|
||||||
|
if (isset($this->__settings[$name])) {
|
||||||
|
$settings = $this->__settings[$name];
|
||||||
|
}
|
||||||
|
$component->initialize($controller, $settings);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -144,9 +155,10 @@ class Component extends Object {
|
||||||
function _loadComponents(&$object, $parent = null) {
|
function _loadComponents(&$object, $parent = null) {
|
||||||
$components = $object->components;
|
$components = $object->components;
|
||||||
$base = $this->__controllerVars['base'];
|
$base = $this->__controllerVars['base'];
|
||||||
|
|
||||||
if (is_array($object->components)) {
|
if (is_array($object->components)) {
|
||||||
foreach ($object->components as $component) {
|
$normal = Set::normalize($object->components);
|
||||||
|
foreach ($normal as $component => $config) {
|
||||||
$parts = preg_split('/\/|\./', $component);
|
$parts = preg_split('/\/|\./', $component);
|
||||||
|
|
||||||
if (count($parts) === 1) {
|
if (count($parts) === 1) {
|
||||||
|
@ -182,9 +194,15 @@ class Component extends Object {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($this->__loaded[$component])) {
|
if (isset($this->__loaded[$component])) {
|
||||||
$object->{$component} =& $this->__loaded[$component];
|
$object->{$component} =& $this->__loaded[$component];
|
||||||
|
|
||||||
|
if (!empty($config) && isset($this->__settings[$component])) {
|
||||||
|
$this->__settings[$component] = array_merge($this->__settings[$component], $config);
|
||||||
|
} elseif (!empty($config)) {
|
||||||
|
$this->__settings[$component] = $config;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if ($componentCn == 'SessionComponent') {
|
if ($componentCn == 'SessionComponent') {
|
||||||
$object->{$component} =& new $componentCn($base);
|
$object->{$component} =& new $componentCn($base);
|
||||||
|
@ -193,8 +211,11 @@ class Component extends Object {
|
||||||
}
|
}
|
||||||
$object->{$component}->enabled = true;
|
$object->{$component}->enabled = true;
|
||||||
$this->__loaded[$component] =& $object->{$component};
|
$this->__loaded[$component] =& $object->{$component};
|
||||||
|
if (!empty($config)) {
|
||||||
|
$this->__settings[$component] = $config;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($object->{$component}->components) && is_array($object->{$component}->components)) {
|
if (isset($object->{$component}->components) && is_array($object->{$component}->components)) {
|
||||||
$this->_loadComponents($object->{$component});
|
$this->_loadComponents($object->{$component});
|
||||||
}
|
}
|
||||||
|
|
|
@ -339,7 +339,13 @@ class Controller extends Object {
|
||||||
|
|
||||||
foreach ($merge as $var) {
|
foreach ($merge as $var) {
|
||||||
if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
|
if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
|
||||||
$this->{$var} = Set::merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
|
if ($var == 'components') {
|
||||||
|
$normal = Set::normalize($this->{$var});
|
||||||
|
$app = Set::normalize($appVars[$var]);
|
||||||
|
$this->{$var} = Set::merge($normal, $app);
|
||||||
|
} else {
|
||||||
|
$this->{$var} = Set::merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -355,7 +361,13 @@ class Controller extends Object {
|
||||||
|
|
||||||
foreach ($merge as $var) {
|
foreach ($merge as $var) {
|
||||||
if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
|
if (isset($appVars[$var]) && !empty($appVars[$var]) && is_array($this->{$var})) {
|
||||||
$this->{$var} = Set::merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
|
if ($var == 'components') {
|
||||||
|
$normal = Set::normalize($this->{$var});
|
||||||
|
$app = Set::normalize($appVars[$var]);
|
||||||
|
$this->{$var} = Set::merge($normal, array_diff_assoc($app, $normal));
|
||||||
|
} else {
|
||||||
|
$this->{$var} = Set::merge($this->{$var}, array_diff($appVars[$var], $this->{$var}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,42 @@
|
||||||
* @lastmodified $Date$
|
* @lastmodified $Date$
|
||||||
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
|
||||||
*/
|
*/
|
||||||
App::import('Core', array('Component', 'AppController'));
|
App::import('Core', array('Component', 'Controller'));
|
||||||
|
|
||||||
|
if (!class_exists('AppController')) {
|
||||||
|
class AppController extends Controller {
|
||||||
|
var $name = 'App';
|
||||||
|
var $uses = array();
|
||||||
|
var $helpers = array();
|
||||||
|
var $components = array('Orange' => array('colour' => 'blood orange'));
|
||||||
|
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
define('AppControllerExists', true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ParamTestComponent
|
||||||
|
*
|
||||||
|
* @package cake.tests.cases.libs.controller
|
||||||
|
*/
|
||||||
|
class ParamTestComponent extends Object {
|
||||||
|
var $name = 'ParamTest';
|
||||||
|
|
||||||
|
var $components = array('Banana' => array('config' => 'value'));
|
||||||
|
|
||||||
|
function initialize(&$controller, $settings) {
|
||||||
|
foreach ($settings as $key => $value) {
|
||||||
|
if (is_numeric($key)) {
|
||||||
|
$this->{$value} = true;
|
||||||
|
} else {
|
||||||
|
$this->{$key} = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Short description for class.
|
* Short description for class.
|
||||||
*
|
*
|
||||||
|
@ -103,8 +138,9 @@ class OrangeComponent extends Object {
|
||||||
* @access public
|
* @access public
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function initialize(&$controller) {
|
function initialize(&$controller, $settings) {
|
||||||
$this->Banana->testField = 'OrangeField';
|
$this->Banana->testField = 'OrangeField';
|
||||||
|
$this->settings = $settings;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
@ -186,7 +222,11 @@ class ComponentTest extends CakeTestCase {
|
||||||
$this->assertTrue(is_a($Controller->RequestHandler, 'RequestHandlerComponent'));
|
$this->assertTrue(is_a($Controller->RequestHandler, 'RequestHandlerComponent'));
|
||||||
$this->assertTrue(is_a($Controller->Cookie, 'CookieComponent'));
|
$this->assertTrue(is_a($Controller->Cookie, 'CookieComponent'));
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* test component loading
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
function testNestedComponentLoading() {
|
function testNestedComponentLoading() {
|
||||||
$Controller =& new ComponentTestController();
|
$Controller =& new ComponentTestController();
|
||||||
$Controller->components = array('Apple');
|
$Controller->components = array('Apple');
|
||||||
|
@ -196,7 +236,11 @@ class ComponentTest extends CakeTestCase {
|
||||||
$this->assertTrue(is_a($Controller->Apple->Orange, 'OrangeComponent'));
|
$this->assertTrue(is_a($Controller->Apple->Orange, 'OrangeComponent'));
|
||||||
$this->assertTrue(is_a($Controller->Apple->Orange->Banana, 'BananaComponent'));
|
$this->assertTrue(is_a($Controller->Apple->Orange->Banana, 'BananaComponent'));
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* test component::startup and running all built components startup()
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
function testComponentStartup() {
|
function testComponentStartup() {
|
||||||
$Controller =& new ComponentTestController();
|
$Controller =& new ComponentTestController();
|
||||||
$Controller->components = array('Apple');
|
$Controller->components = array('Apple');
|
||||||
|
@ -209,7 +253,11 @@ class ComponentTest extends CakeTestCase {
|
||||||
|
|
||||||
$this->assertEqual($Controller->Apple->testName, 'ComponentTest');
|
$this->assertEqual($Controller->Apple->testName, 'ComponentTest');
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* test a component being used more than once.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
function testMultipleComponentInitialize() {
|
function testMultipleComponentInitialize() {
|
||||||
$Controller =& new ComponentTestController();
|
$Controller =& new ComponentTestController();
|
||||||
$Controller->components = array('Orange', 'Banana');
|
$Controller->components = array('Orange', 'Banana');
|
||||||
|
@ -220,5 +268,37 @@ class ComponentTest extends CakeTestCase {
|
||||||
$this->assertEqual($Controller->Banana->testField, 'OrangeField');
|
$this->assertEqual($Controller->Banana->testField, 'OrangeField');
|
||||||
$this->assertEqual($Controller->Orange->Banana->testField, 'OrangeField');
|
$this->assertEqual($Controller->Orange->Banana->testField, 'OrangeField');
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Test Component declarations with Parameters
|
||||||
|
* tests merging of component parameters and merging / construction of components.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function testComponentsWithParams() {
|
||||||
|
$this->skipIf(defined('AppControllerExists'), 'Components with Params test will be skipped as it needs a non-existent AppController. As the an AppController class exists, this cannot be run.');
|
||||||
|
|
||||||
|
$Controller =& new ComponentTestController();
|
||||||
|
$Controller->components = array('ParamTest' => array('test' => 'value', 'flag'), 'Apple');
|
||||||
|
|
||||||
|
$Controller->constructClasses();
|
||||||
|
$Controller->Component->initialize($Controller);
|
||||||
|
|
||||||
|
$this->assertTrue(is_a($Controller->ParamTest, 'ParamTestComponent'));
|
||||||
|
$this->assertTrue(is_a($Controller->ParamTest->Banana, 'BananaComponent'));
|
||||||
|
$this->assertTrue(is_a($Controller->Orange, 'OrangeComponent'));
|
||||||
|
$this->assertTrue(is_a($Controller->Session, 'SessionComponent'));
|
||||||
|
$this->assertEqual($Controller->Orange->settings, array('colour' => 'blood orange'));
|
||||||
|
$this->assertEqual($Controller->ParamTest->test, 'value');
|
||||||
|
$this->assertEqual($Controller->ParamTest->flag, true);
|
||||||
|
|
||||||
|
//Settings are merged from app controller and current controller.
|
||||||
|
$Controller =& new ComponentTestController();
|
||||||
|
$Controller->components = array('ParamTest' => array('test' => 'value'), 'Orange' => array('ripeness' => 'perfect'));
|
||||||
|
$Controller->constructClasses();
|
||||||
|
$Controller->Component->initialize($Controller);
|
||||||
|
|
||||||
|
$this->assertEqual($Controller->Orange->settings, array('colour' => 'blood orange', 'ripeness' => 'perfect'));
|
||||||
|
$this->assertEqual($Controller->ParamTest->test, 'value');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -573,6 +573,7 @@ class ControllerTest extends CakeTestCase {
|
||||||
|
|
||||||
$testVars = get_class_vars('TestController');
|
$testVars = get_class_vars('TestController');
|
||||||
$appVars = get_class_vars('AppController');
|
$appVars = get_class_vars('AppController');
|
||||||
|
|
||||||
$components = is_array($appVars['components'])
|
$components = is_array($appVars['components'])
|
||||||
? array_merge($appVars['components'], $testVars['components'])
|
? array_merge($appVars['components'], $testVars['components'])
|
||||||
: $testVars['components'];
|
: $testVars['components'];
|
||||||
|
@ -585,10 +586,10 @@ class ControllerTest extends CakeTestCase {
|
||||||
$uses = is_array($appVars['uses'])
|
$uses = is_array($appVars['uses'])
|
||||||
? array_merge($appVars['uses'], $testVars['uses'])
|
? array_merge($appVars['uses'], $testVars['uses'])
|
||||||
: $testVars['uses'];
|
: $testVars['uses'];
|
||||||
|
|
||||||
$this->assertEqual(count(array_diff($TestController->helpers, $helpers)), 0);
|
$this->assertEqual(count(array_diff($TestController->helpers, $helpers)), 0);
|
||||||
$this->assertEqual(count(array_diff($TestController->uses, $uses)), 0);
|
$this->assertEqual(count(array_diff($TestController->uses, $uses)), 0);
|
||||||
$this->assertEqual(count(array_diff($TestController->components, $components)), 0);
|
$this->assertEqual(count(array_diff_assoc(Set::normalize($TestController->components), Set::normalize($components))), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testReferer() {
|
function testReferer() {
|
||||||
|
|
Loading…
Reference in a new issue