Browse Source

..

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
86ae5bdf28
  1. 12
      framework/YiiBase.php
  2. 15
      framework/base/ApplicationComponent.php
  3. 35
      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

12
framework/YiiBase.php

@ -76,7 +76,7 @@ class YiiBase
/** /**
* @var array initial property values that will be applied to objects newly created via [[createObject]]. * @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 * 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, * the leading backslashes. For example,
* *
* ~~~ * ~~~
@ -317,7 +317,7 @@ class YiiBase
* *
* - create the object using the PHP `new` operator; * - create the object using the PHP `new` operator;
* - if [[objectConfig]] contains the configuration for the object class, * - 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; * - 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. * - call the `init` method of the object if it implements the [[yii\base\Initable]] interface.
* *
@ -374,13 +374,11 @@ class YiiBase
$c = get_class($object); $c = get_class($object);
if (isset(\Yii::$objectConfig[$c])) { 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) {
foreach ($config as $name => $value) { $object->$name = $value;
$object->$name = $value;
}
} }
if ($object instanceof \yii\base\Initable) { if ($object instanceof \yii\base\Initable) {

15
framework/base/ApplicationComponent.php

@ -12,30 +12,15 @@ namespace yii\base;
/** /**
* ApplicationComponent is the base class for application component classes. * 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> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
abstract class ApplicationComponent extends Component implements Initable 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. * 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() public function init()
{ {
$this->attachBehaviors($this->behaviors);
} }
} }

35
framework/base/Object.php

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

24
framework/db/dao/Connection.php

@ -26,7 +26,11 @@ use yii\db\Exception;
* the DB connection: * 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(); * $connection->active = true; // same as: $connection->open();
* ~~~ * ~~~
* *
@ -255,24 +259,6 @@ class Connection extends \yii\base\ApplicationComponent
private $_driver; 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. * Closes the connection when this component is being serialized.
* @return array * @return array
*/ */

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

@ -206,7 +206,7 @@ class ComponentTest extends \yiiunit\TestCase
public function testCreate() 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(1, $component->b);
$this->assertEquals(2, $component->c); $this->assertEquals(2, $component->c);
$this->assertEquals(3, $component->a); $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']); $this->assertEquals('test', $foo->prop['test']);
$bar = Bar::newInstance(10, 20); $bar = Bar::newInstance(array(), 10, 20);
$this->assertEquals(30, $bar->prop1); $this->assertEquals(30, $bar->prop1);
$this->assertEquals(null, $bar->prop2); $this->assertEquals(null, $bar->prop2);
$this->assertEquals(3, $bar->prop3); $this->assertEquals(3, $bar->prop3);
$bar = Bar::newInstance(100, 200, array( $bar = Bar::newInstance(array(
'prop2' => 'x', 'prop2' => 'x',
'prop3' => 400, 'prop3' => 400,
)); ), 100, 200);
$this->assertEquals(300, $bar->prop1); $this->assertEquals(300, $bar->prop1);
$this->assertEquals('x', $bar->prop2); $this->assertEquals('x', $bar->prop2);
$this->assertEquals(3, $bar->prop3); $this->assertEquals(3, $bar->prop3);

Loading…
Cancel
Save