Commit 8e11ad03 by Qiang Xue

refactored code to use Instance::ensure()

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