Commit 8e11ad03 by Qiang Xue

refactored code to use Instance::ensure()

parent 72c99663
...@@ -437,7 +437,7 @@ class Generator extends \yii\gii\Generator ...@@ -437,7 +437,7 @@ class Generator extends \yii\gii\Generator
*/ */
public function validateDb() public function validateDb()
{ {
if (Yii::$app->hasComponent($this->db) === false) { if (!Yii::$app->has($this->db)) {
$this->addError('db', 'There is no application component named "db".'); $this->addError('db', 'There is no application component named "db".');
} elseif (!Yii::$app->get($this->db) instanceof Connection) { } elseif (!Yii::$app->get($this->db) instanceof Connection) {
$this->addError('db', 'The "db" application component must be a DB connection instance.'); $this->addError('db', 'The "db" application component must be a DB connection instance.');
......
...@@ -9,6 +9,7 @@ namespace yii\mongodb; ...@@ -9,6 +9,7 @@ namespace yii\mongodb;
use Yii; use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\di\Instance;
/** /**
* Cache implements a cache application component by storing cached data in a MongoDB. * Cache implements a cache application component by storing cached data in a MongoDB.
...@@ -61,12 +62,7 @@ class Cache extends \yii\caching\Cache ...@@ -61,12 +62,7 @@ class Cache extends \yii\caching\Cache
public function init() public function init()
{ {
parent::init(); parent::init();
if (is_string($this->db)) { $this->db = Instance::ensure($this->db, Connection::className());
$this->db = Yii::$app->get($this->db);
}
if (!$this->db instanceof Connection) {
throw new InvalidConfigException($this->className() . "::db must be either a MongoDB connection instance or the application component ID of a MongoDB connection.");
}
} }
/** /**
......
...@@ -9,6 +9,7 @@ namespace yii\mongodb; ...@@ -9,6 +9,7 @@ namespace yii\mongodb;
use Yii; use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\di\Instance;
/** /**
* Session extends [[\yii\web\Session]] by using MongoDB as session data storage. * Session extends [[\yii\web\Session]] by using MongoDB as session data storage.
...@@ -55,13 +56,8 @@ class Session extends \yii\web\Session ...@@ -55,13 +56,8 @@ class Session extends \yii\web\Session
*/ */
public function init() public function init()
{ {
if (is_string($this->db)) {
$this->db = Yii::$app->get($this->db);
}
if (!$this->db instanceof Connection) {
throw new InvalidConfigException($this->className() . "::db must be either a MongoDB connection instance or the application component ID of a MongoDB connection.");
}
parent::init(); parent::init();
$this->db = Instance::ensure($this->db, Connection::className());
} }
/** /**
......
...@@ -132,7 +132,7 @@ class Schema extends Object ...@@ -132,7 +132,7 @@ class Schema extends Object
if ($db->enableSchemaCache && !in_array($name, $db->schemaCacheExclude, true)) { if ($db->enableSchemaCache && !in_array($name, $db->schemaCacheExclude, true)) {
/** @var $cache Cache */ /** @var $cache Cache */
$cache = is_string($db->schemaCache) ? Yii::$app->get($db->schemaCache) : $db->schemaCache; $cache = is_string($db->schemaCache) ? Yii::$app->get($db->schemaCache, false) : $db->schemaCache;
if ($cache instanceof Cache) { if ($cache instanceof Cache) {
$key = $this->getCacheKey($name); $key = $this->getCacheKey($name);
if ($refresh || ($index = $cache->get($key)) === false) { if ($refresh || ($index = $cache->get($key)) === false) {
...@@ -296,7 +296,7 @@ class Schema extends Object ...@@ -296,7 +296,7 @@ class Schema extends Object
public function refresh() public function refresh()
{ {
/** @var $cache Cache */ /** @var $cache Cache */
$cache = is_string($this->db->schemaCache) ? Yii::$app->get($this->db->schemaCache) : $this->db->schemaCache; $cache = is_string($this->db->schemaCache) ? Yii::$app->get($this->db->schemaCache, false) : $this->db->schemaCache;
if ($this->db->enableSchemaCache && $cache instanceof Cache) { if ($this->db->enableSchemaCache && $cache instanceof Cache) {
GroupDependency::invalidate($cache, $this->getCacheGroup()); GroupDependency::invalidate($cache, $this->getCacheGroup());
} }
......
...@@ -11,6 +11,7 @@ use Yii; ...@@ -11,6 +11,7 @@ use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\db\Connection; use yii\db\Connection;
use yii\db\Query; use yii\db\Query;
use yii\di\Instance;
/** /**
* DbCache implements a cache application component by storing cached data in a database. * DbCache implements a cache application component by storing cached data in a database.
...@@ -79,12 +80,7 @@ class DbCache extends Cache ...@@ -79,12 +80,7 @@ class DbCache extends Cache
public function init() public function init()
{ {
parent::init(); parent::init();
if (is_string($this->db)) { $this->db = Instance::ensure($this->db, Connection::className());
$this->db = Yii::$app->get($this->db);
}
if (!$this->db instanceof Connection) {
throw new InvalidConfigException("DbCache::db must be either a DB connection instance or the application component ID of a DB connection.");
}
} }
/** /**
......
...@@ -10,6 +10,7 @@ namespace yii\caching; ...@@ -10,6 +10,7 @@ namespace yii\caching;
use Yii; use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\db\Connection; use yii\db\Connection;
use yii\di\Instance;
/** /**
* DbDependency represents a dependency based on the query result of a SQL statement. * DbDependency represents a dependency based on the query result of a SQL statement.
...@@ -45,10 +46,7 @@ class DbDependency extends Dependency ...@@ -45,10 +46,7 @@ class DbDependency extends Dependency
*/ */
protected function generateDependencyData($cache) protected function generateDependencyData($cache)
{ {
$db = Yii::$app->get($this->db); $db = Instance::ensure($this->db, Connection::className());
if (!$db instanceof Connection) {
throw new InvalidConfigException("DbDependency::db must be the application component ID of a DB connection.");
}
if ($this->sql === null) { if ($this->sql === null) {
throw new InvalidConfigException("DbDependency::sql must be set."); throw new InvalidConfigException("DbDependency::sql must be set.");
} }
......
...@@ -53,7 +53,7 @@ class CacheController extends Controller ...@@ -53,7 +53,7 @@ class CacheController extends Controller
public function actionFlush($component = 'cache') public function actionFlush($component = 'cache')
{ {
/** @var Cache $cache */ /** @var Cache $cache */
$cache = Yii::$app->get($component); $cache = Yii::$app->get($component, false);
if (!$cache || !$cache instanceof Cache) { if (!$cache || !$cache instanceof Cache) {
throw new Exception('Application component "'.$component.'" is not defined or not a cache.'); throw new Exception('Application component "'.$component.'" is not defined or not a cache.');
} }
......
...@@ -13,6 +13,7 @@ use yii\base\InvalidConfigException; ...@@ -13,6 +13,7 @@ use yii\base\InvalidConfigException;
use yii\base\Model; use yii\base\Model;
use yii\db\Connection; use yii\db\Connection;
use yii\db\QueryInterface; use yii\db\QueryInterface;
use yii\di\Instance;
/** /**
* ActiveDataProvider implements a data provider based on [[\yii\db\Query]] and [[\yii\db\ActiveQuery]]. * ActiveDataProvider implements a data provider based on [[\yii\db\Query]] and [[\yii\db\ActiveQuery]].
...@@ -85,10 +86,7 @@ class ActiveDataProvider extends BaseDataProvider ...@@ -85,10 +86,7 @@ class ActiveDataProvider extends BaseDataProvider
{ {
parent::init(); parent::init();
if (is_string($this->db)) { if (is_string($this->db)) {
$this->db = Yii::$app->get($this->db); $this->db = Instance::ensure($this->db, Connection::className());
if ($this->db === null) {
throw new InvalidConfigException('The "db" property must be a valid DB Connection application component.');
}
} }
} }
......
...@@ -10,6 +10,7 @@ namespace yii\data; ...@@ -10,6 +10,7 @@ namespace yii\data;
use Yii; use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\db\Connection; use yii\db\Connection;
use yii\di\Instance;
/** /**
* SqlDataProvider implements a data provider based on a plain SQL statement. * SqlDataProvider implements a data provider based on a plain SQL statement.
...@@ -89,12 +90,7 @@ class SqlDataProvider extends BaseDataProvider ...@@ -89,12 +90,7 @@ class SqlDataProvider extends BaseDataProvider
public function init() public function init()
{ {
parent::init(); parent::init();
if (is_string($this->db)) { $this->db = Instance::ensure($this->db, Connection::className());
$this->db = Yii::$app->get($this->db);
}
if (!$this->db instanceof Connection) {
throw new InvalidConfigException('The "db" property must be a valid DB Connection application component.');
}
if ($this->sql === null) { if ($this->sql === null) {
throw new InvalidConfigException('The "sql" property must be set.'); throw new InvalidConfigException('The "sql" property must be set.');
} }
......
...@@ -378,7 +378,7 @@ class Command extends \yii\base\Component ...@@ -378,7 +378,7 @@ class Command extends \yii\base\Component
/** @var \yii\caching\Cache $cache */ /** @var \yii\caching\Cache $cache */
if ($db->enableQueryCache && $method !== '') { if ($db->enableQueryCache && $method !== '') {
$cache = is_string($db->queryCache) ? Yii::$app->get($db->queryCache) : $db->queryCache; $cache = is_string($db->queryCache) ? Yii::$app->get($db->queryCache, false) : $db->queryCache;
} }
if (isset($cache) && $cache instanceof Cache) { if (isset($cache) && $cache instanceof Cache) {
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
namespace yii\db; namespace yii\db;
use yii\di\Instance;
/** /**
* Migration is the base class for representing a database migration. * Migration is the base class for representing a database migration.
...@@ -36,10 +37,10 @@ namespace yii\db; ...@@ -36,10 +37,10 @@ namespace yii\db;
class Migration extends \yii\base\Component class Migration extends \yii\base\Component
{ {
/** /**
* @var Connection the database connection that this migration should work with. * @var Connection|string the DB connection object or the application component ID of the DB connection
* If not set, it will be initialized as the 'db' application component. * that this migration should work with.
*/ */
public $db; public $db = 'db';
/** /**
* Initializes the migration. * Initializes the migration.
...@@ -48,9 +49,7 @@ class Migration extends \yii\base\Component ...@@ -48,9 +49,7 @@ class Migration extends \yii\base\Component
public function init() public function init()
{ {
parent::init(); parent::init();
if ($this->db === null) { $this->db = Instance::ensure($this->db, Connection::className());
$this->db = \Yii::$app->get('db');
}
} }
/** /**
......
...@@ -96,7 +96,7 @@ abstract class Schema extends Object ...@@ -96,7 +96,7 @@ abstract class Schema extends Object
if ($db->enableSchemaCache && !in_array($name, $db->schemaCacheExclude, true)) { if ($db->enableSchemaCache && !in_array($name, $db->schemaCacheExclude, true)) {
/** @var Cache $cache */ /** @var Cache $cache */
$cache = is_string($db->schemaCache) ? Yii::$app->get($db->schemaCache) : $db->schemaCache; $cache = is_string($db->schemaCache) ? Yii::$app->get($db->schemaCache, false) : $db->schemaCache;
if ($cache instanceof Cache) { if ($cache instanceof Cache) {
$key = $this->getCacheKey($name); $key = $this->getCacheKey($name);
if ($refresh || ($table = $cache->get($key)) === false) { if ($refresh || ($table = $cache->get($key)) === false) {
...@@ -225,7 +225,7 @@ abstract class Schema extends Object ...@@ -225,7 +225,7 @@ abstract class Schema extends Object
public function refresh() public function refresh()
{ {
/** @var Cache $cache */ /** @var Cache $cache */
$cache = is_string($this->db->schemaCache) ? Yii::$app->get($this->db->schemaCache) : $this->db->schemaCache; $cache = is_string($this->db->schemaCache) ? Yii::$app->get($this->db->schemaCache, false) : $this->db->schemaCache;
if ($this->db->enableSchemaCache && $cache instanceof Cache) { if ($this->db->enableSchemaCache && $cache instanceof Cache) {
GroupDependency::invalidate($cache, $this->getCacheGroup()); GroupDependency::invalidate($cache, $this->getCacheGroup());
} }
......
...@@ -61,13 +61,14 @@ interface ContainerInterface ...@@ -61,13 +61,14 @@ interface ContainerInterface
* If a component is not shared, this method will create a new instance every time. * If a component is not shared, this method will create a new instance every time.
* *
* @param string $typeOrID component type (a fully qualified namespaced class/interface name, e.g. `yii\db\Connection`) or ID (e.g. `db`). * @param string $typeOrID component type (a fully qualified namespaced class/interface name, e.g. `yii\db\Connection`) or ID (e.g. `db`).
* @param boolean $throwException whether to throw an exception if `$typeOrID` is not registered with the container before.
* @return object the component of the specified type or ID * @return object the component of the specified type or ID
* @throws \yii\base\InvalidConfigException if `$typeOrID` refers to a nonexistent component ID * @throws \yii\base\InvalidConfigException if `$typeOrID` refers to a nonexistent component ID
* or if there is cyclic dependency detected * or if there is cyclic dependency detected
* @see has() * @see has()
* @see set() * @see set()
*/ */
public function get($typeOrID); public function get($typeOrID, $throwException = true);
/** /**
* Registers a component definition with this container. * Registers a component definition with this container.
......
...@@ -63,24 +63,36 @@ trait ContainerTrait ...@@ -63,24 +63,36 @@ trait ContainerTrait
* *
* @param string $typeOrID component type (a fully qualified namespaced class/interface name, e.g. `yii\db\Connection`) * @param string $typeOrID component type (a fully qualified namespaced class/interface name, e.g. `yii\db\Connection`)
* or ID (e.g. `db`). When a class/interface name is given, make sure it does NOT have a leading backslash. * or ID (e.g. `db`). When a class/interface name is given, make sure it does NOT have a leading backslash.
* @return object the component of the specified type or ID * @param boolean $throwException whether to throw an exception if `$typeOrID` is not registered with the container before.
* @return object|null the component of the specified type or ID. If `$throwException` is false and `$typeOrID`
* is not registered before, null will be returned.
* @throws InvalidConfigException if `$typeOrID` refers to a nonexistent component ID * @throws InvalidConfigException if `$typeOrID` refers to a nonexistent component ID
* or if there is cyclic dependency detected * or if there is cyclic dependency detected
* @see has() * @see has()
* @see set() * @see set()
*/ */
public function get($typeOrID) public function get($typeOrID, $throwException = true)
{ {
if (isset($this->_components[$typeOrID])) { if (isset($this->_components[$typeOrID])) {
return $this->_components[$typeOrID]; return $this->_components[$typeOrID];
} }
if (!isset($this->_definitions[$typeOrID])) {
if (strpos($typeOrID, '\\') !== false) {
// a class name
return $this->buildComponent($typeOrID);
} elseif (!$throwException) {
return null;
} else {
throw new InvalidConfigException("Unknown component ID: $typeOrID");
}
}
if (isset($this->_building[$typeOrID])) { if (isset($this->_building[$typeOrID])) {
throw new InvalidConfigException("A cyclic dependency of \"$typeOrID\" is detected."); throw new InvalidConfigException("A cyclic dependency of \"$typeOrID\" is detected.");
} }
$this->_building[$typeOrID] = true; $this->_building[$typeOrID] = true;
if (isset($this->_definitions[$typeOrID])) {
$definition = $this->_definitions[$typeOrID]; $definition = $this->_definitions[$typeOrID];
if (is_string($definition)) { if (is_string($definition)) {
// a type or ID // a type or ID
...@@ -95,12 +107,6 @@ trait ContainerTrait ...@@ -95,12 +107,6 @@ trait ContainerTrait
// a configuration array // a configuration array
$component = $this->buildComponent($definition); $component = $this->buildComponent($definition);
} }
} elseif (strpos($typeOrID, '\\') !== false) {
// a class name
$component = $this->buildComponent($typeOrID);
} else {
throw new InvalidConfigException("Unknown component ID: $typeOrID");
}
unset($this->_building[$typeOrID]); unset($this->_building[$typeOrID]);
if (array_key_exists($typeOrID, $this->_components)) { if (array_key_exists($typeOrID, $this->_components)) {
......
...@@ -82,7 +82,7 @@ class Instance ...@@ -82,7 +82,7 @@ class Instance
*/ */
public static function of($id, ContainerInterface $container = null) public static function of($id, ContainerInterface $container = null)
{ {
return new self($id, $container); return new static($id, $container);
} }
/** /**
...@@ -108,7 +108,7 @@ class Instance ...@@ -108,7 +108,7 @@ class Instance
* 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.
* @param string $type the class name to be checked * @param string $type the class name to be checked
* @param ContainerInterface $container the container. If null, the application instance will be used. * @param ContainerInterface $container the container. If null, the application instance will be used.
* @return null|\yii\base\Component|Instance * @return object
* @throws \yii\base\InvalidConfigException * @throws \yii\base\InvalidConfigException
*/ */
public static function ensure($value, $type, $container = null) public static function ensure($value, $type, $container = null)
...@@ -119,7 +119,7 @@ class Instance ...@@ -119,7 +119,7 @@ class Instance
if ($value instanceof $type) { if ($value instanceof $type) {
return $value; return $value;
} elseif (is_string($value)) { } elseif (is_string($value)) {
$value = new self($value, $container); $value = new static($value, $container);
} }
if ($value instanceof self) { if ($value instanceof self) {
......
...@@ -9,6 +9,7 @@ namespace yii\i18n; ...@@ -9,6 +9,7 @@ namespace yii\i18n;
use Yii; use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\di\Instance;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\caching\Cache; use yii\caching\Cache;
use yii\db\Connection; use yii\db\Connection;
...@@ -93,19 +94,9 @@ class DbMessageSource extends MessageSource ...@@ -93,19 +94,9 @@ class DbMessageSource extends MessageSource
public function init() public function init()
{ {
parent::init(); parent::init();
if (is_string($this->db)) { $this->db = Instance::ensure($this->db, Connection::className());
$this->db = Yii::$app->get($this->db);
}
if (!$this->db instanceof Connection) {
throw new InvalidConfigException("DbMessageSource::db must be either a DB connection instance or the application component ID of a DB connection.");
}
if ($this->enableCaching) { if ($this->enableCaching) {
if (is_string($this->cache)) { $this->cache = Instance::ensure($this->cache, Cache::className());
$this->cache = Yii::$app->get($this->cache);
}
if (!$this->cache instanceof Cache) {
throw new InvalidConfigException("DbMessageSource::cache must be either a cache object or the application component ID of the cache object.");
}
} }
} }
......
...@@ -10,6 +10,7 @@ namespace yii\log; ...@@ -10,6 +10,7 @@ namespace yii\log;
use Yii; use Yii;
use yii\db\Connection; use yii\db\Connection;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\di\Instance;
/** /**
* DbTarget stores log messages in a database table. * DbTarget stores log messages in a database table.
...@@ -62,12 +63,7 @@ class DbTarget extends Target ...@@ -62,12 +63,7 @@ class DbTarget extends Target
public function init() public function init()
{ {
parent::init(); parent::init();
if (is_string($this->db)) { $this->db = Instance::ensure($this->db, Connection::className());
$this->db = Yii::$app->get($this->db);
}
if (!$this->db instanceof Connection) {
throw new InvalidConfigException("DbTarget::db must be either a DB connection instance or the application component ID of a DB connection.");
}
} }
/** /**
......
...@@ -9,6 +9,7 @@ namespace yii\log; ...@@ -9,6 +9,7 @@ namespace yii\log;
use Yii; use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\di\Instance;
use yii\mail\MailerInterface; use yii\mail\MailerInterface;
/** /**
...@@ -43,12 +44,7 @@ class EmailTarget extends Target ...@@ -43,12 +44,7 @@ class EmailTarget extends Target
if (empty($this->message['to'])) { if (empty($this->message['to'])) {
throw new InvalidConfigException('The "to" option must be set for EmailTarget::message.'); throw new InvalidConfigException('The "to" option must be set for EmailTarget::message.');
} }
if (is_string($this->mail)) { $this->mail = Instance::ensure($this->mail, 'yii\mail\MailerInterface');
$this->mail = Yii::$app->get($this->mail);
}
if (!$this->mail instanceof MailerInterface) {
throw new InvalidConfigException("EmailTarget::mailer must be either a mailer object or the application component ID of a mailer object.");
}
} }
/** /**
......
...@@ -10,6 +10,7 @@ namespace yii\mutex; ...@@ -10,6 +10,7 @@ namespace yii\mutex;
use Yii; use Yii;
use yii\db\Connection; use yii\db\Connection;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\di\Instance;
/** /**
* @author resurtm <resurtm@gmail.com> * @author resurtm <resurtm@gmail.com>
...@@ -31,11 +32,6 @@ abstract class DbMutex extends Mutex ...@@ -31,11 +32,6 @@ abstract class DbMutex extends Mutex
public function init() public function init()
{ {
parent::init(); parent::init();
if (is_string($this->db)) { $this->db = Instance::ensure($this->db, Connection::className());
$this->db = Yii::$app->get($this->db);
}
if (!$this->db instanceof Connection) {
throw new InvalidConfigException('Mutex::db must be either a DB connection instance or the application component ID of a DB connection.');
}
} }
} }
...@@ -15,6 +15,7 @@ use yii\base\Exception; ...@@ -15,6 +15,7 @@ use yii\base\Exception;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\base\InvalidCallException; use yii\base\InvalidCallException;
use yii\base\InvalidParamException; use yii\base\InvalidParamException;
use yii\di\Instance;
/** /**
* DbManager represents an authorization manager that stores authorization information in database. * DbManager represents an authorization manager that stores authorization information in database.
...@@ -59,14 +60,9 @@ class DbManager extends Manager ...@@ -59,14 +60,9 @@ class DbManager extends Manager
*/ */
public function init() public function init()
{ {
if (is_string($this->db)) {
$this->db = Yii::$app->get($this->db);
}
if (!$this->db instanceof Connection) {
throw new InvalidConfigException("DbManager::db must be either a DB connection instance or the application component ID of a DB connection.");
}
$this->_usingSqlite = !strncmp($this->db->getDriverName(), 'sqlite', 6);
parent::init(); parent::init();
$this->db = Instance::ensure($this->db, Connection::className());
$this->_usingSqlite = !strncmp($this->db->getDriverName(), 'sqlite', 6);
} }
/** /**
......
...@@ -10,6 +10,8 @@ namespace yii\test; ...@@ -10,6 +10,8 @@ namespace yii\test;
use Yii; use Yii;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\db\Connection; use yii\db\Connection;
use yii\di\Instance;
use yii\base\Object;
/** /**
* DbFixture is the base class for DB-related fixtures. * DbFixture is the base class for DB-related fixtures.
...@@ -34,11 +36,6 @@ abstract class DbFixture extends Fixture ...@@ -34,11 +36,6 @@ abstract class DbFixture extends Fixture
public function init() public function init()
{ {
parent::init(); parent::init();
if (is_string($this->db)) { $this->db = Instance::ensure($this->db, Object::className());
$this->db = Yii::$app->get($this->db);
}
if (!is_object($this->db)) {
throw new InvalidConfigException("The 'db' property must be either a DB connection instance or the application component ID of a DB connection.");
}
} }
} }
...@@ -10,6 +10,7 @@ namespace yii\web; ...@@ -10,6 +10,7 @@ namespace yii\web;
use Yii; use Yii;
use yii\caching\Cache; use yii\caching\Cache;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\di\Instance;
/** /**
* CacheSession implements a session component using cache as storage medium. * CacheSession implements a session component using cache as storage medium.
...@@ -52,13 +53,8 @@ class CacheSession extends Session ...@@ -52,13 +53,8 @@ class CacheSession extends Session
*/ */
public function init() public function init()
{ {
if (is_string($this->cache)) {
$this->cache = Yii::$app->get($this->cache);
}
if (!$this->cache instanceof Cache) {
throw new InvalidConfigException('CacheSession::cache must refer to the application component ID of a cache object.');
}
parent::init(); parent::init();
$this->cache = Instance::ensure($this->cache, Cache::className());
} }
/** /**
......
...@@ -11,6 +11,7 @@ use Yii; ...@@ -11,6 +11,7 @@ use Yii;
use yii\db\Connection; use yii\db\Connection;
use yii\db\Query; use yii\db\Query;
use yii\base\InvalidConfigException; use yii\base\InvalidConfigException;
use yii\di\Instance;
/** /**
* DbSession extends [[Session]] by using database as session data storage. * DbSession extends [[Session]] by using database as session data storage.
...@@ -74,13 +75,8 @@ class DbSession extends Session ...@@ -74,13 +75,8 @@ class DbSession extends Session
*/ */
public function init() public function init()
{ {
if (is_string($this->db)) {
$this->db = Yii::$app->get($this->db);
}
if (!$this->db instanceof Connection) {
throw new InvalidConfigException("DbSession::db must be either a DB connection instance or the application component ID of a DB connection.");
}
parent::init(); parent::init();
$this->db = Instance::ensure($this->db, Connection::className());
} }
/** /**
......
...@@ -145,7 +145,7 @@ class UrlManager extends Component ...@@ -145,7 +145,7 @@ class UrlManager extends Component
return; return;
} }
if (is_string($this->cache)) { if (is_string($this->cache)) {
$this->cache = Yii::$app->get($this->cache); $this->cache = Yii::$app->get($this->cache, false);
} }
if ($this->cache instanceof Cache) { if ($this->cache instanceof Cache) {
$key = __CLASS__; $key = __CLASS__;
......
...@@ -11,6 +11,7 @@ use Yii; ...@@ -11,6 +11,7 @@ use Yii;
use yii\base\Widget; use yii\base\Widget;
use yii\caching\Cache; use yii\caching\Cache;
use yii\caching\Dependency; use yii\caching\Dependency;
use yii\di\Instance;
/** /**
* *
...@@ -79,11 +80,7 @@ class FragmentCache extends Widget ...@@ -79,11 +80,7 @@ class FragmentCache extends Widget
{ {
parent::init(); parent::init();
if (!$this->enabled) { $this->cache = $this->enabled ? Instance::ensure($this->cache, Cache::className()) : null;
$this->cache = null;
} elseif (is_string($this->cache)) {
$this->cache = Yii::$app->get($this->cache);
}
if ($this->getCachedContent() === false) { if ($this->getCachedContent() === false) {
$this->getView()->cacheStack[] = $this; $this->getView()->cacheStack[] = $this;
......
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