From 712a77582b8f27d6fcc237705f12c0171aee0a47 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Tue, 2 Aug 2011 21:34:01 -0400 Subject: [PATCH] w --- framework/YiiBase.php | 25 --------------- framework/base/Component.php | 47 +++++++++++++++++++++-------- tests/unit/framework/base/ComponentTest.php | 20 ++++++++++++ todo.txt | 4 +-- 4 files changed, 56 insertions(+), 40 deletions(-) diff --git a/framework/YiiBase.php b/framework/YiiBase.php index 226efb4..fa097d4 100644 --- a/framework/YiiBase.php +++ b/framework/YiiBase.php @@ -72,7 +72,6 @@ class YiiBase private static $_imported = array(); // alias => class name or directory private static $_logger; - /** * @return string the version of Yii framework */ @@ -82,30 +81,6 @@ class YiiBase } /** - * Creates a Web application instance. - * @param mixed $config application configuration. This can be either an array representing - * the configuration to be applied to the newly created application instance, or a string - * referring to a PHP file returning the configuration array. - * @return yii\web\Application the newly created application instance. - */ - public static function createWebApplication($config = null) - { - return new yii\web\Application($config); - } - - /** - * Creates a console application instance. - * @param mixed $config application configuration. This can be either an array representing - * the configuration to be applied to the newly created application instance, or a string - * referring to a PHP file returning the configuration array. - * @return yii\console\Application the newly created application instance. - */ - public static function createConsoleApplication($config = null) - { - return new yii\console\Application($config); - } - - /** * Returns the installation directory of the Yii framework. * @return string the path of the framework */ diff --git a/framework/base/Component.php b/framework/base/Component.php index 10a1fc7..8619a39 100644 --- a/framework/base/Component.php +++ b/framework/base/Component.php @@ -311,30 +311,51 @@ class Component * * - Call [[Initable::preinit|preinit]] if the class implements [[Initable]]; * - Initialize the component properties using the name-value pairs given as the - * first parameter to this method; + * last parameter to this method; * - Call [[Initable::init|init]] if the class implements [[Initable]]. * - * Any additional parameters passed to this method will be - * passed to the constructor of the component being created. For example, + * Parameters passed to this method will be used as the parameters to the object + * constructor. If, however, the last parameter is an array and the count of the parameters + * is one more than the count of declared constructor parameters, that parameter + * will be treated as name-value pairs for initializing the component properties. + * For example, + * + * ~~~ + * class Foo extends \yii\base\Component { + * public $c; + * public function __construct($a, $b) { ... } + * } + * + * $model = Foo::create(1, 2, array('c' => 3)); + * // which is equivalent to the following lines: + * $model = new Foo(1, 2); + * $model->preinit(); + * $model->c = 3; + * $model->init(); + * ~~~ * - * @param array $config the name-value pairs that will be used to initialize component properties. * @return object the created component * @throws Exception if the configuration is invalid. */ - public static function create($config = array()) + public static function create() { - if (!is_array($config)) { - throw new Exception('The $config parameter must be an array.'); - } - - if (($n = func_num_args()) > 1) { + $class = '\\' . get_called_class(); + if (($n = func_num_args()) > 0) { $args = func_get_args(); - $args[0]['class'] = '\\' . get_called_class(); + if (is_array($args[$n-1])) { + // the last parameter could be configuration array + $method = new \ReflectionMethod($class, '__construct'); + if ($method->getNumberOfParameters()+1 == $n) { + $config = $args[$n-1]; + array_pop($args); + } + } + $config['class'] = $class; + array_unshift($args, $config); return call_user_func_array('\Yii::createComponent', $args); } else { - $config['class'] = '\\' . get_called_class(); - return \Yii::createComponent($config); + return \Yii::createComponent($class); } } diff --git a/tests/unit/framework/base/ComponentTest.php b/tests/unit/framework/base/ComponentTest.php index 85c4379..53bc35d 100644 --- a/tests/unit/framework/base/ComponentTest.php +++ b/tests/unit/framework/base/ComponentTest.php @@ -208,6 +208,14 @@ class ComponentTest extends \yii\test\TestCase $this->assertEquals('Hello world',$component->evaluateExpression('"Hello $who"',array('who' => 'world'))); $this->assertEquals('Hello world',$component->evaluateExpression(array($component,'exprEvaluator'),array('who' => 'world'))); } + + public function testCreate() + { + $component = NewComponent2::create(1, 2, array('a'=>3)); + $this->assertEquals(1, $component->b); + $this->assertEquals(2, $component->c); + $this->assertEquals(3, $component->a); + } } class NewComponent extends \yii\base\Component @@ -259,3 +267,15 @@ class NewBehavior extends \yii\base\Behavior return 2; } } + +class NewComponent2 extends \yii\base\Component +{ + public $a; + public $b; + public $c; + public function __construct($b, $c) + { + $this->b = $b; + $this->c = $c; + } +} \ No newline at end of file diff --git a/todo.txt b/todo.txt index 1f23e83..22f5a0b 100644 --- a/todo.txt +++ b/todo.txt @@ -1,9 +1,10 @@ +- logging - base * error/exception handling - * security * module * application * http exception + * security - validators * type conversion rules * CompareValidator::clientValidateAttribute(): search for "CHtml::activeId" @@ -24,7 +25,6 @@ * AR * document-based * key-value-based -- logging - i18n * consider using PHP built-in support and data * message translations, choice format