Browse Source

..

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
86ae5bdf28
  1. 8
      framework/YiiBase.php
  2. 15
      framework/base/ApplicationComponent.php
  3. 31
      framework/base/Object.php
  4. 24
      framework/db/dao/Connection.php
  5. 2
      tests/unit/framework/base/ComponentTest.php
  6. 6
      tests/unit/framework/base/ObjectTest.php

8
framework/YiiBase.php

@ -76,7 +76,7 @@ class YiiBase
/**
* @var array initial property values that will be applied to objects newly created via [[createObject]].
* The array keys are fully qualified namespaced class names, and the array values are the corresponding
* name-value pairs for initializing the created class instances. Make sure the class names do not have
* name-value pairs for initializing the created class instances. Make sure the class names do NOT have
* the leading backslashes. For example,
*
* ~~~
@ -317,7 +317,7 @@ class YiiBase
*
* - create the object using the PHP `new` operator;
* - if [[objectConfig]] contains the configuration for the object class,
* initialize the object properties with that configuration;
* it will be merged with the configuration passed to this method;
* - initialize the object properties using the configuration passed to this method;
* - call the `init` method of the object if it implements the [[yii\base\Initable]] interface.
*
@ -374,14 +374,12 @@ class YiiBase
$c = get_class($object);
if (isset(\Yii::$objectConfig[$c])) {
$config = isset($config) ? array_merge(\Yii::$objectConfig[$c], $config) : \Yii::$objectConfig[$c];
$config = array_merge(\Yii::$objectConfig[$c], $config);
}
if (!empty($config)) {
foreach ($config as $name => $value) {
$object->$name = $value;
}
}
if ($object instanceof \yii\base\Initable) {
$object->init();

15
framework/base/ApplicationComponent.php

@ -12,30 +12,15 @@ namespace yii\base;
/**
* ApplicationComponent is the base class for application component classes.
*
* Child classes mainly needs to implement the [[Initable::init|init]] method as required by
* the [[Initable]] interface.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
abstract class ApplicationComponent extends Component implements Initable
{
/**
* @var array the behaviors that should be attached to this component.
* The behaviors will be attached to the component when [[init]] is called.
* Please refer to [[Model::behaviors]] on how to specify the value of this property.
*/
public $behaviors = array();
/**
* Initializes the application component.
* This method is invoked after the component is created and its property values are
* initialized. The default implementation will call [[Component::attachBehaviors()]]
* to attach behaviors declared in [[behaviors]].
* If you override this method, make sure to call the parent implementation.
*/
public function init()
{
$this->attachBehaviors($this->behaviors);
}
}

31
framework/base/Object.php

@ -258,17 +258,17 @@ class Object
/**
* Creates a new instance of the calling class.
*
* Parameters passed to this method will be used as the parameters to the object
* The newly created object will be initialized with the specified configuration.
*
* Extra parameters passed to this method will be used as the parameters to the object
* constructor.
*
* This method does the following steps to create a object:
*
* - create the object using the PHP `new` operator;
* - if [[Yii::objectConfig]] contains the configuration for the object class,
* initialize the object properties with that configuration;
* - if the number of the given parameters is more than the number of the parameters
* listed in the object constructor and the last given parameter is an array,
* initialize the object properties using that array;
* it will be merged with the $config parameter;
* - initialize the object properties using the configuration passed to this method;
* - call the `init` method of the object if it implements the [[yii\base\Initable]] interface.
*
* For example,
@ -287,30 +287,25 @@ class Object
* }
* }
*
* $model = Foo::newInstance(1, 2, array('c' => 3));
* $model = Foo::newInstance(array('c' => 3), 1, 2);
* // which is equivalent to the following lines:
* $model = new Foo(1, 2);
* $model->c = 3;
* $model->init();
* ~~~
*
* @param array $config the object configuration (name-value pairs that will be used to initialize the object)
* @return Object the created object
* @throws Exception if the configuration is invalid.
*/
public static function newInstance()
public static function newInstance($config = array())
{
$c = get_called_class();
$class = '\\' . $c;
if (($n = func_num_args()) > 0) {
if (($n = func_num_args()-1) > 0) {
$args = func_get_args();
if (is_array($args[$n - 1])) {
$method = new \ReflectionMethod($class, '__construct');
if ($method->getNumberOfParameters() < $n) {
// the last EXTRA parameter is a configuration array
$config = $args[--$n];
unset($args[$n]);
}
}
array_shift($args); // remove $config
}
if ($n === 0) {
@ -327,14 +322,12 @@ class Object
}
if (isset(\Yii::$objectConfig[$c])) {
$config = isset($config) ? array_merge(\Yii::$objectConfig[$c], $config) : \Yii::$objectConfig[$c];
$config = array_merge(\Yii::$objectConfig[$c], $config);
}
if (!empty($config)) {
foreach ($config as $name => $value) {
$object->$name = $value;
}
}
if ($object instanceof \yii\base\Initable) {
$object->init();

24
framework/db/dao/Connection.php

@ -26,7 +26,11 @@ use yii\db\Exception;
* the DB connection:
*
* ~~~
* $connection = \yii\db\dao\Connection::newInstance($dsn, $username, $password);
* $connection = \yii\db\dao\Connection::newInstance(array(
* 'dsn' => $dsn,
* 'username' => $username,
* 'password' => $password,
* ));
* $connection->active = true; // same as: $connection->open();
* ~~~
*
@ -255,24 +259,6 @@ class Connection extends \yii\base\ApplicationComponent
private $_driver;
/**
* Constructor.
* Note, the DB connection is not established when this connection
* instance is created. You may set [[active]] to be true or call [[open]]
* to establish the connection.
* @param string $dsn the Data Source Name, or DSN, contains the information
* required to connect to the database.
* @param string $username the user name for the DSN string.
* @param string $password the password for the DSN string.
* @see http://www.php.net/manual/en/function.PDO-construct.php
*/
public function __construct($dsn = '', $username = '', $password = '')
{
$this->dsn = $dsn;
$this->username = $username;
$this->password = $password;
}
/**
* Closes the connection when this component is being serialized.
* @return array
*/

2
tests/unit/framework/base/ComponentTest.php

@ -206,7 +206,7 @@ class ComponentTest extends \yiiunit\TestCase
public function testCreate()
{
$component = NewComponent2::newInstance(1, 2, array('a'=>3));
$component = NewComponent2::newInstance(array('a'=>3), 1, 2);
$this->assertEquals(1, $component->b);
$this->assertEquals(2, $component->c);
$this->assertEquals(3, $component->a);

6
tests/unit/framework/base/ObjectTest.php

@ -51,15 +51,15 @@ class ObjectTest extends \yiiunit\TestCase
$this->assertEquals('test', $foo->prop['test']);
$bar = Bar::newInstance(10, 20);
$bar = Bar::newInstance(array(), 10, 20);
$this->assertEquals(30, $bar->prop1);
$this->assertEquals(null, $bar->prop2);
$this->assertEquals(3, $bar->prop3);
$bar = Bar::newInstance(100, 200, array(
$bar = Bar::newInstance(array(
'prop2' => 'x',
'prop3' => 400,
));
), 100, 200);
$this->assertEquals(300, $bar->prop1);
$this->assertEquals('x', $bar->prop2);
$this->assertEquals(3, $bar->prop3);

Loading…
Cancel
Save