Browse Source

w

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
712a77582b
  1. 25
      framework/YiiBase.php
  2. 47
      framework/base/Component.php
  3. 20
      tests/unit/framework/base/ComponentTest.php
  4. 4
      todo.txt

25
framework/YiiBase.php

@ -72,7 +72,6 @@ class YiiBase
private static $_imported = array(); // alias => class name or directory private static $_imported = array(); // alias => class name or directory
private static $_logger; private static $_logger;
/** /**
* @return string the version of Yii framework * @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. * Returns the installation directory of the Yii framework.
* @return string the path of the framework * @return string the path of the framework
*/ */

47
framework/base/Component.php

@ -311,30 +311,51 @@ class Component
* *
* - Call [[Initable::preinit|preinit]] if the class implements [[Initable]]; * - Call [[Initable::preinit|preinit]] if the class implements [[Initable]];
* - Initialize the component properties using the name-value pairs given as the * - 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]]. * - Call [[Initable::init|init]] if the class implements [[Initable]].
* *
* Any additional parameters passed to this method will be * Parameters passed to this method will be used as the parameters to the object
* passed to the constructor of the component being created. For example, * 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 * @return object the created component
* @throws Exception if the configuration is invalid. * @throws Exception if the configuration is invalid.
*/ */
public static function create($config = array()) public static function create()
{ {
if (!is_array($config)) { $class = '\\' . get_called_class();
throw new Exception('The $config parameter must be an array.'); if (($n = func_num_args()) > 0) {
}
if (($n = func_num_args()) > 1) {
$args = func_get_args(); $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); return call_user_func_array('\Yii::createComponent', $args);
} }
else { else {
$config['class'] = '\\' . get_called_class(); return \Yii::createComponent($class);
return \Yii::createComponent($config);
} }
} }

20
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('"Hello $who"',array('who' => 'world')));
$this->assertEquals('Hello world',$component->evaluateExpression(array($component,'exprEvaluator'),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 class NewComponent extends \yii\base\Component
@ -259,3 +267,15 @@ class NewBehavior extends \yii\base\Behavior
return 2; 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;
}
}

4
todo.txt

@ -1,9 +1,10 @@
- logging
- base - base
* error/exception handling * error/exception handling
* security
* module * module
* application * application
* http exception * http exception
* security
- validators - validators
* type conversion rules * type conversion rules
* CompareValidator::clientValidateAttribute(): search for "CHtml::activeId" * CompareValidator::clientValidateAttribute(): search for "CHtml::activeId"
@ -24,7 +25,6 @@
* AR * AR
* document-based * document-based
* key-value-based * key-value-based
- logging
- i18n - i18n
* consider using PHP built-in support and data * consider using PHP built-in support and data
* message translations, choice format * message translations, choice format

Loading…
Cancel
Save