Commit d341bf02 by Qiang Xue

Fixes #6398: Added support for specifying dependent component in terms of a…

Fixes #6398: Added support for specifying dependent component in terms of a configuration array for classes such as `DbCache`
parent 389d9e2f
...@@ -36,9 +36,10 @@ use yii\di\Instance; ...@@ -36,9 +36,10 @@ use yii\di\Instance;
class Cache extends \yii\caching\Cache class Cache extends \yii\caching\Cache
{ {
/** /**
* @var Connection|string the MongoDB connection object or the application component ID of the MongoDB connection. * @var Connection|array|string the MongoDB connection object or the application component ID of the MongoDB connection.
* After the Cache object is created, if you want to change this property, you should only assign it * After the Cache object is created, if you want to change this property, you should only assign it
* with a MongoDB connection object. * with a MongoDB connection object.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/ */
public $db = 'mongodb'; public $db = 'mongodb';
/** /**
......
...@@ -34,8 +34,9 @@ use yii\helpers\Json; ...@@ -34,8 +34,9 @@ use yii\helpers\Json;
abstract class Migration extends Component implements MigrationInterface abstract class Migration extends Component implements MigrationInterface
{ {
/** /**
* @var Connection|string the MongoDB connection object or the application component ID of the MongoDB connection * @var Connection|array|string the MongoDB connection object or the application component ID of the MongoDB connection
* that this migration should work with. * that this migration should work with.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/ */
public $db = 'mongodb'; public $db = 'mongodb';
......
...@@ -38,9 +38,10 @@ use yii\di\Instance; ...@@ -38,9 +38,10 @@ use yii\di\Instance;
class Session extends \yii\web\Session class Session extends \yii\web\Session
{ {
/** /**
* @var Connection|string the MongoDB connection object or the application component ID of the MongoDB connection. * @var Connection|array|string the MongoDB connection object or the application component ID of the MongoDB connection.
* After the Session object is created, if you want to change this property, you should only assign it * After the Session object is created, if you want to change this property, you should only assign it
* with a MongoDB connection object. * with a MongoDB connection object.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/ */
public $db = 'mongodb'; public $db = 'mongodb';
/** /**
......
...@@ -13,6 +13,7 @@ Yii Framework 2 Change Log ...@@ -13,6 +13,7 @@ Yii Framework 2 Change Log
- Bug #6717: Fixed issue with UrlManager not matching a route on url creation when it was prefixed with `/` and pattern was empty (cebe) - Bug #6717: Fixed issue with UrlManager not matching a route on url creation when it was prefixed with `/` and pattern was empty (cebe)
- Enh #4502: Added alias support to URL route when calling `Url::toRoute()` and `Url::to()` (qiangxue, lynicidn) - Enh #4502: Added alias support to URL route when calling `Url::toRoute()` and `Url::to()` (qiangxue, lynicidn)
- Enh #6247: Logger and error handler are now using slightly less memory (stepanselyuk, samdark) - Enh #6247: Logger and error handler are now using slightly less memory (stepanselyuk, samdark)
- Enh #6398: Added support for specifying dependent component in terms of a configuration array for classes such as `DbCache` (qiangxue)
- Enh #6434: Added `yii\behaviors\SluggableBehavior::immutable` to support keeping the generated slug unchanged (trntv) - Enh #6434: Added `yii\behaviors\SluggableBehavior::immutable` to support keeping the generated slug unchanged (trntv)
- Enh #6467: `ActiveForm` will scroll to the nearest visible element when the first error input is hidden (newartix) - Enh #6467: `ActiveForm` will scroll to the nearest visible element when the first error input is hidden (newartix)
- Enh #6488: Support changing `yii\base\Theme::basePath` during runtime (qiangxue) - Enh #6488: Support changing `yii\base\Theme::basePath` during runtime (qiangxue)
......
...@@ -37,9 +37,10 @@ use yii\di\Instance; ...@@ -37,9 +37,10 @@ use yii\di\Instance;
class DbCache extends Cache class DbCache extends Cache
{ {
/** /**
* @var Connection|string the DB connection object or the application component ID of the DB connection. * @var Connection|array|string the DB connection object or the application component ID of the DB connection.
* After the DbCache object is created, if you want to change this property, you should only assign it * After the DbCache object is created, if you want to change this property, you should only assign it
* with a DB connection object. * with a DB connection object.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/ */
public $db = 'db'; public $db = 'db';
/** /**
......
...@@ -72,8 +72,9 @@ class ActiveDataProvider extends BaseDataProvider ...@@ -72,8 +72,9 @@ class ActiveDataProvider extends BaseDataProvider
*/ */
public $key; public $key;
/** /**
* @var Connection|string the DB connection object or the application component ID of the DB connection. * @var Connection|array|string the DB connection object or the application component ID of the DB connection.
* If not set, the default DB connection will be used. * If not set, the default DB connection will be used.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/ */
public $db; public $db;
......
...@@ -64,7 +64,8 @@ use yii\di\Instance; ...@@ -64,7 +64,8 @@ use yii\di\Instance;
class SqlDataProvider extends BaseDataProvider class SqlDataProvider extends BaseDataProvider
{ {
/** /**
* @var Connection|string the DB connection object or the application component ID of the DB connection. * @var Connection|array|string the DB connection object or the application component ID of the DB connection.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/ */
public $db = 'db'; public $db = 'db';
/** /**
......
...@@ -39,10 +39,13 @@ use \yii\base\Component; ...@@ -39,10 +39,13 @@ use \yii\base\Component;
class Migration extends Component implements MigrationInterface class Migration extends Component implements MigrationInterface
{ {
/** /**
* @var Connection|string the DB connection object or the application component ID of the DB connection * @var Connection|array|string the DB connection object or the application component ID of the DB connection
* that this migration should work with. Note that when a Migration object is created by * that this migration should work with. Starting from version 2.0.2, this can also be a configuration array
* the `migrate` command, this property will be overwritten by the command. If you do not want to * for creating the object.
* use the DB connection provided by the command, you may override the [[init()]] method like the following: *
* Note that when a Migration object is created by the `migrate` command, this property will be overwritten
* by the command. If you do not want to use the DB connection provided by the command, you may override
* the [[init()]] method like the following:
* *
* ```php * ```php
* public function init() * public function init()
......
...@@ -92,13 +92,14 @@ class Instance ...@@ -92,13 +92,14 @@ class Instance
* *
* // returns Yii::$app->db * // returns Yii::$app->db
* $db = Instance::ensure('db', Connection::className()); * $db = Instance::ensure('db', Connection::className());
* // or * // returns an instance of Connection using the given configuration
* $instance = Instance::of('db'); * $db = Instance::ensure(['dsn' => 'sqlite:path/to/my.db'], Connection::className());
* $db = Instance::ensure($instance, Connection::className());
* ``` * ```
* *
* @param object|string|static $reference an object or a reference to the desired object. * @param object|string|array|static $reference an object or a reference to the desired object.
* You may specify a reference in terms of a component ID or an Instance object. * You may specify a reference in terms of a component ID or an Instance object.
* Starting from version 2.0.2, you may also pass in a configuration array for creating the object.
* If the "class" value is not specified in the configuration array, it will use the value of `$type`.
* @param string $type the class/interface name to be checked. If null, type check will not be performed. * @param string $type the class/interface name to be checked. If null, type check will not be performed.
* @param ServiceLocator|Container $container the container. This will be passed to [[get()]]. * @param ServiceLocator|Container $container the container. This will be passed to [[get()]].
* @return object the object referenced by the Instance, or `$reference` itself if it is an object. * @return object the object referenced by the Instance, or `$reference` itself if it is an object.
...@@ -108,6 +109,13 @@ class Instance ...@@ -108,6 +109,13 @@ class Instance
{ {
if ($reference instanceof $type) { if ($reference instanceof $type) {
return $reference; return $reference;
} elseif (is_array($reference)) {
$class = isset($reference['class']) ? $reference['class'] : $type;
if (!$container instanceof Container) {
$container = Yii::$app;
}
unset($reference['class']);
return $container->get($class, [], $reference);
} elseif (empty($reference)) { } elseif (empty($reference)) {
throw new InvalidConfigException('The required component is not specified.'); throw new InvalidConfigException('The required component is not specified.');
} }
......
...@@ -57,7 +57,8 @@ use yii\web\ForbiddenHttpException; ...@@ -57,7 +57,8 @@ use yii\web\ForbiddenHttpException;
class AccessControl extends ActionFilter class AccessControl extends ActionFilter
{ {
/** /**
* @var User|string the user object representing the authentication status or the ID of the user application component. * @var User|array|string the user object representing the authentication status or the ID of the user application component.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/ */
public $user = 'user'; public $user = 'user';
/** /**
......
...@@ -53,17 +53,19 @@ class DbMessageSource extends MessageSource ...@@ -53,17 +53,19 @@ class DbMessageSource extends MessageSource
const CACHE_KEY_PREFIX = 'DbMessageSource'; const CACHE_KEY_PREFIX = 'DbMessageSource';
/** /**
* @var Connection|string the DB connection object or the application component ID of the DB connection. * @var Connection|array|string the DB connection object or the application component ID of the DB connection.
* After the DbMessageSource object is created, if you want to change this property, you should only assign * After the DbMessageSource object is created, if you want to change this property, you should only assign
* it with a DB connection object. * it with a DB connection object.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/ */
public $db = 'db'; public $db = 'db';
/** /**
* @var Cache|string the cache object or the application component ID of the cache object. * @var Cache|array|string the cache object or the application component ID of the cache object.
* The messages data will be cached using this cache object. Note, this property has meaning only * The messages data will be cached using this cache object. Note, this property has meaning only
* in case [[cachingDuration]] set to non-zero value. * in case [[cachingDuration]] set to non-zero value.
* After the DbMessageSource object is created, if you want to change this property, you should only assign * After the DbMessageSource object is created, if you want to change this property, you should only assign
* it with a cache object. * it with a cache object.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/ */
public $cache = 'cache'; public $cache = 'cache';
/** /**
......
...@@ -32,9 +32,10 @@ use yii\helpers\VarDumper; ...@@ -32,9 +32,10 @@ use yii\helpers\VarDumper;
class DbTarget extends Target class DbTarget extends Target
{ {
/** /**
* @var Connection|string the DB connection object or the application component ID of the DB connection. * @var Connection|array|string the DB connection object or the application component ID of the DB connection.
* After the DbTarget object is created, if you want to change this property, you should only assign it * After the DbTarget object is created, if you want to change this property, you should only assign it
* with a DB connection object. * with a DB connection object.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/ */
public $db = 'db'; public $db = 'db';
/** /**
......
...@@ -50,9 +50,10 @@ class EmailTarget extends Target ...@@ -50,9 +50,10 @@ class EmailTarget extends Target
*/ */
public $message = []; public $message = [];
/** /**
* @var MailerInterface|string the mailer object or the application component ID of the mailer object. * @var MailerInterface|array|string the mailer object or the application component ID of the mailer object.
* After the EmailTarget object is created, if you want to change this property, you should only assign it * After the EmailTarget object is created, if you want to change this property, you should only assign it
* with a mailer object. * with a mailer object.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/ */
public $mailer = 'mailer'; public $mailer = 'mailer';
......
...@@ -23,9 +23,10 @@ use yii\di\Instance; ...@@ -23,9 +23,10 @@ use yii\di\Instance;
abstract class DbMutex extends Mutex abstract class DbMutex extends Mutex
{ {
/** /**
* @var Connection|string the DB connection object or the application component ID of the DB connection. * @var Connection|array|string the DB connection object or the application component ID of the DB connection.
* After the Mutex object is created, if you want to change this property, you should only assign * After the Mutex object is created, if you want to change this property, you should only assign
* it with a DB connection object. * it with a DB connection object.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/ */
public $db = 'db'; public $db = 'db';
......
...@@ -36,9 +36,10 @@ use yii\di\Instance; ...@@ -36,9 +36,10 @@ use yii\di\Instance;
class DbManager extends BaseManager class DbManager extends BaseManager
{ {
/** /**
* @var Connection|string the DB connection object or the application component ID of the DB connection. * @var Connection|array|string the DB connection object or the application component ID of the DB connection.
* After the DbManager object is created, if you want to change this property, you should only assign it * After the DbManager object is created, if you want to change this property, you should only assign it
* with a DB connection object. * with a DB connection object.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/ */
public $db = 'db'; public $db = 'db';
/** /**
......
...@@ -23,9 +23,10 @@ use yii\base\Object; ...@@ -23,9 +23,10 @@ use yii\base\Object;
abstract class DbFixture extends Fixture abstract class DbFixture extends Fixture
{ {
/** /**
* @var Connection|string the DB connection object or the application component ID of the DB connection. * @var Connection|array|string the DB connection object or the application component ID of the DB connection.
* After the DbFixture object is created, if you want to change this property, you should only assign it * After the DbFixture object is created, if you want to change this property, you should only assign it
* with a DB connection object. * with a DB connection object.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/ */
public $db = 'db'; public $db = 'db';
......
...@@ -39,11 +39,13 @@ use yii\di\Instance; ...@@ -39,11 +39,13 @@ use yii\di\Instance;
class CacheSession extends Session class CacheSession extends Session
{ {
/** /**
* @var Cache|string the cache object or the application component ID of the cache object. * @var Cache|array|string the cache object or the application component ID of the cache object.
* The session data will be stored using this cache object. * The session data will be stored using this cache object.
* *
* After the CacheSession object is created, if you want to change this property, * After the CacheSession object is created, if you want to change this property,
* you should only assign it with a cache object. * you should only assign it with a cache object.
*
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/ */
public $cache = 'cache'; public $cache = 'cache';
......
...@@ -38,9 +38,10 @@ use yii\di\Instance; ...@@ -38,9 +38,10 @@ use yii\di\Instance;
class DbSession extends Session class DbSession extends Session
{ {
/** /**
* @var Connection|string the DB connection object or the application component ID of the DB connection. * @var Connection|array|string the DB connection object or the application component ID of the DB connection.
* After the DbSession object is created, if you want to change this property, you should only assign it * After the DbSession object is created, if you want to change this property, you should only assign it
* with a DB connection object. * with a DB connection object.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/ */
public $db = 'db'; public $db = 'db';
/** /**
......
...@@ -24,9 +24,10 @@ use yii\di\Instance; ...@@ -24,9 +24,10 @@ use yii\di\Instance;
class FragmentCache extends Widget class FragmentCache extends Widget
{ {
/** /**
* @var Cache|string the cache object or the application component ID of the cache object. * @var Cache|array|string the cache object or the application component ID of the cache object.
* After the FragmentCache object is created, if you want to change this property, * After the FragmentCache object is created, if you want to change this property,
* you should only assign it with a cache object. * you should only assign it with a cache object.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/ */
public $cache = 'cache'; public $cache = 'cache';
/** /**
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace yiiunit\framework\di; namespace yiiunit\framework\di;
use yii\base\Component; use yii\base\Component;
use yii\db\Connection;
use yii\di\Container; use yii\di\Container;
use yii\di\Instance; use yii\di\Instance;
use yiiunit\TestCase; use yiiunit\TestCase;
...@@ -29,4 +30,20 @@ class InstanceTest extends TestCase ...@@ -29,4 +30,20 @@ class InstanceTest extends TestCase
$this->assertTrue(Instance::ensure($instance, $className, $container) instanceof Component); $this->assertTrue(Instance::ensure($instance, $className, $container) instanceof Component);
$this->assertTrue($instance->get($container) !== Instance::ensure($instance, $className, $container)); $this->assertTrue($instance->get($container) !== Instance::ensure($instance, $className, $container));
} }
public function testEnsure()
{
$container = new Container;
$container->set('db', [
'class' => 'yii\db\Connection',
'dsn' => 'test',
]);
$this->assertTrue(Instance::ensure('db', 'yii\db\Connection', $container) instanceof Connection);
$this->assertTrue(Instance::ensure(new Connection, 'yii\db\Connection', $container) instanceof Connection);
$this->assertTrue(Instance::ensure([
'class' => 'yii\db\Connection',
'dsn' => 'test',
], 'yii\db\Connection', $container) instanceof Connection);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment