Commit 8c2e3923 by Qiang Xue

Turn module and application into DI containers.

parent 66abd5b8
......@@ -164,7 +164,7 @@ class AuthAction extends Action
if (!empty($_GET[$this->clientIdGetParamName])) {
$clientId = $_GET[$this->clientIdGetParamName];
/** @var \yii\authclient\Collection $collection */
$collection = Yii::$app->getComponent($this->clientCollection);
$collection = Yii::$app->get($this->clientCollection);
if (!$collection->hasClient($clientId)) {
throw new NotFoundHttpException("Unknown auth client '{$clientId}'");
}
......
......@@ -143,7 +143,7 @@ class Choice extends Widget
protected function defaultClients()
{
/** @var $collection \yii\authclient\Collection */
$collection = Yii::$app->getComponent($this->clientCollection);
$collection = Yii::$app->get($this->clientCollection);
return $collection->getClients();
}
......
......@@ -86,7 +86,7 @@ class RequestPanel extends Panel
$action = null;
}
/** @var \yii\web\Session $session */
$session = Yii::$app->getComponent('session', false);
$session = Yii::$app->get('session', [], false);
return [
'flashes' => $session ? $session->getAllFlashes() : [],
......
......@@ -59,7 +59,7 @@ class ActiveRecord extends BaseActiveRecord
*/
public static function getDb()
{
return \Yii::$app->getComponent('elasticsearch');
return \Yii::$app->get('elasticsearch');
}
/**
......
......@@ -55,7 +55,7 @@ class DebugAction extends Action
$options = ['pretty' => true];
/** @var Connection $db */
$db = \Yii::$app->getComponent($this->db);
$db = \Yii::$app->get($this->db);
$time = microtime(true);
switch ($method) {
case 'GET': $result = $db->get($url, $options, $body, true); break;
......
......@@ -115,7 +115,7 @@ class Query extends Component implements QueryInterface
public function createCommand($db = null)
{
if ($db === null) {
$db = Yii::$app->getComponent('elasticsearch');
$db = Yii::$app->get('elasticsearch');
}
$commandConfig = $db->getQueryBuilder()->build($this);
......
......@@ -439,7 +439,7 @@ class Generator extends \yii\gii\Generator
{
if (Yii::$app->hasComponent($this->db) === false) {
$this->addError('db', 'There is no application component named "db".');
} elseif (!Yii::$app->getComponent($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.');
}
}
......
......@@ -29,7 +29,7 @@ abstract class ActiveRecord extends BaseActiveRecord
*/
public static function getDb()
{
return \Yii::$app->getComponent('mongodb');
return \Yii::$app->get('mongodb');
}
/**
......
......@@ -62,7 +62,7 @@ class Cache extends \yii\caching\Cache
{
parent::init();
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($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.");
......
......@@ -62,7 +62,7 @@ class Query extends Component implements QueryInterface
public function getCollection($db = null)
{
if ($db === null) {
$db = Yii::$app->getComponent('mongodb');
$db = Yii::$app->get('mongodb');
}
return $db->getCollection($this->from);
......
......@@ -56,7 +56,7 @@ class Session extends \yii\web\Session
public function init()
{
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($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.");
......
......@@ -31,7 +31,7 @@ class Query extends \yii\mongodb\Query
public function getCollection($db = null)
{
if ($db === null) {
$db = Yii::$app->getComponent('mongodb');
$db = Yii::$app->get('mongodb');
}
return $db->getFileCollection($this->from);
......
......@@ -45,7 +45,7 @@ class ActiveRecord extends BaseActiveRecord
*/
public static function getDb()
{
return \Yii::$app->getComponent('redis');
return \Yii::$app->get('redis');
}
/**
......
......@@ -76,7 +76,7 @@ class Cache extends \yii\caching\Cache
{
parent::init();
if (is_string($this->redis)) {
$this->redis = Yii::$app->getComponent($this->redis);
$this->redis = Yii::$app->get($this->redis);
} elseif (is_array($this->redis)) {
if (!isset($this->redis['class'])) {
$this->redis['class'] = Connection::className();
......
......@@ -80,7 +80,7 @@ class Session extends \yii\web\Session
public function init()
{
if (is_string($this->redis)) {
$this->redis = Yii::$app->getComponent($this->redis);
$this->redis = Yii::$app->get($this->redis);
} elseif (is_array($this->redis)) {
if (!isset($this->redis['class'])) {
$this->redis['class'] = Connection::className();
......
......@@ -62,7 +62,7 @@ abstract class ActiveRecord extends BaseActiveRecord
*/
public static function getDb()
{
return \Yii::$app->getComponent('sphinx');
return \Yii::$app->get('sphinx');
}
/**
......
......@@ -155,7 +155,7 @@ class Query extends Component implements QueryInterface
*/
protected function defaultConnection()
{
return Yii::$app->getComponent('sphinx');
return Yii::$app->get('sphinx');
}
/**
......
......@@ -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->getComponent($db->schemaCache) : $db->schemaCache;
$cache = is_string($db->schemaCache) ? Yii::$app->get($db->schemaCache) : $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->getComponent($this->db->schemaCache) : $this->db->schemaCache;
$cache = is_string($this->db->schemaCache) ? Yii::$app->get($this->db->schemaCache) : $this->db->schemaCache;
if ($this->db->enableSchemaCache && $cache instanceof Cache) {
GroupDependency::invalidate($cache, $this->getCacheGroup());
}
......
......@@ -160,7 +160,6 @@ abstract class Application extends Module
$this->preInit($config);
$this->registerErrorHandlers();
$this->registerCoreComponents();
Component::__construct($config);
}
......@@ -206,6 +205,17 @@ abstract class Application extends Module
} elseif (!ini_get('date.timezone')) {
$this->setTimeZone('UTC');
}
// merge core components with custom components
if (!empty($config['components'])) {
foreach ($this->coreComponents() as $id => $component) {
if (!isset($config['components'][$id])) {
$config['components'][$id] = $component;
} elseif (!isset($config['components'][$id]['class'])) {
$config['components'][$id]['class'] = $component['class'];
}
}
}
}
/**
......@@ -248,7 +258,7 @@ abstract class Application extends Module
*/
public function preloadComponents()
{
$this->getComponent('log');
$this->get('log');
parent::preloadComponents();
}
......@@ -400,7 +410,7 @@ abstract class Application extends Module
*/
public function getDb()
{
return $this->getComponent('db');
return $this->get('db');
}
/**
......@@ -409,7 +419,7 @@ abstract class Application extends Module
*/
public function getLog()
{
return $this->getComponent('log');
return $this->get('log');
}
/**
......@@ -418,7 +428,7 @@ abstract class Application extends Module
*/
public function getErrorHandler()
{
return $this->getComponent('errorHandler');
return $this->get('errorHandler');
}
/**
......@@ -427,7 +437,7 @@ abstract class Application extends Module
*/
public function getCache()
{
return $this->getComponent('cache');
return $this->get('cache');
}
/**
......@@ -436,7 +446,7 @@ abstract class Application extends Module
*/
public function getFormatter()
{
return $this->getComponent('formatter');
return $this->get('formatter');
}
/**
......@@ -445,7 +455,7 @@ abstract class Application extends Module
*/
public function getRequest()
{
return $this->getComponent('request');
return $this->get('request');
}
/**
......@@ -454,7 +464,7 @@ abstract class Application extends Module
*/
public function getView()
{
return $this->getComponent('view');
return $this->get('view');
}
/**
......@@ -463,7 +473,7 @@ abstract class Application extends Module
*/
public function getUrlManager()
{
return $this->getComponent('urlManager');
return $this->get('urlManager');
}
/**
......@@ -472,7 +482,7 @@ abstract class Application extends Module
*/
public function getI18n()
{
return $this->getComponent('i18n');
return $this->get('i18n');
}
/**
......@@ -481,7 +491,7 @@ abstract class Application extends Module
*/
public function getMail()
{
return $this->getComponent('mail');
return $this->get('mail');
}
/**
......@@ -490,16 +500,16 @@ abstract class Application extends Module
*/
public function getAuthManager()
{
return $this->getComponent('authManager');
return $this->get('authManager');
}
/**
* Registers the core application components.
* @see setComponents
* Returns the core application components.
* @see set
*/
public function registerCoreComponents()
public function coreComponents()
{
$this->setComponents([
return [
'log' => ['class' => 'yii\log\Logger'],
'errorHandler' => ['class' => 'yii\base\ErrorHandler'],
'formatter' => ['class' => 'yii\base\Formatter'],
......@@ -507,7 +517,7 @@ abstract class Application extends Module
'mail' => ['class' => 'yii\swiftmailer\Mailer'],
'urlManager' => ['class' => 'yii\web\UrlManager'],
'view' => ['class' => 'yii\web\View'],
]);
];
}
/**
......
......@@ -8,6 +8,8 @@
namespace yii\base;
use Yii;
use yii\di\ContainerInterface;
use yii\di\ContainerTrait;
/**
* Module is the base class for module and application classes.
......@@ -35,8 +37,10 @@ use Yii;
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Module extends Component
class Module extends Component implements ContainerInterface
{
use ContainerTrait;
/**
* @var array custom module parameters (name => value).
*/
......@@ -110,10 +114,7 @@ class Module extends Component
* @var array child modules of this module
*/
private $_modules = [];
/**
* @var array components registered under this module
*/
private $_components = [];
/**
* Constructor.
......@@ -130,15 +131,14 @@ class Module extends Component
/**
* Getter magic method.
* This method is overridden to support accessing components
* like reading module properties.
* This method is overridden to support accessing components like reading properties.
* @param string $name component or property name
* @return mixed the named property value
*/
public function __get($name)
{
if ($this->hasComponent($name)) {
return $this->getComponent($name);
if ($this->has($name)) {
return $this->get($name);
} else {
return parent::__get($name);
}
......@@ -146,15 +146,14 @@ class Module extends Component
/**
* Checks if a property value is null.
* This method overrides the parent implementation by checking
* if the named component is loaded.
* This method overrides the parent implementation by checking if the named component is loaded.
* @param string $name the property name or the event name
* @return boolean whether the property value is null
*/
public function __isset($name)
{
if ($this->hasComponent($name)) {
return $this->getComponent($name) !== null;
if ($this->has($name)) {
return $this->get($name, [], false) !== null;
} else {
return parent::__isset($name);
}
......@@ -432,126 +431,14 @@ class Module extends Component
}
/**
* Checks whether the named component exists.
* @param string $id component ID
* @return boolean whether the named component exists. Both loaded and unloaded components
* are considered.
*/
public function hasComponent($id)
{
return isset($this->_components[$id]);
}
/**
* Retrieves the named component.
* @param string $id component ID (case-sensitive)
* @param boolean $load whether to load the component if it is not yet loaded.
* @return Component|null the component instance, null if the component does not exist.
* @see hasComponent()
*/
public function getComponent($id, $load = true)
{
if (isset($this->_components[$id])) {
if ($this->_components[$id] instanceof Object) {
return $this->_components[$id];
} elseif ($load) {
return $this->_components[$id] = Yii::createObject($this->_components[$id]);
}
}
return null;
}
/**
* Registers a component with this module.
* @param string $id component ID
* @param Component|array|null $component the component to be registered with the module. This can
* be one of the followings:
*
* - a [[Component]] object
* - a configuration array: when [[getComponent()]] is called initially for this component, the array
* will be used to instantiate the component via [[Yii::createObject()]].
* - null: the named component will be removed from the module
*/
public function setComponent($id, $component)
{
if ($component === null) {
unset($this->_components[$id]);
} else {
$this->_components[$id] = $component;
}
}
/**
* Returns the registered components.
* @param boolean $loadedOnly whether to return the loaded components only. If this is set false,
* then all components specified in the configuration will be returned, whether they are loaded or not.
* Loaded components will be returned as objects, while unloaded components as configuration arrays.
* @return array the components (indexed by their IDs)
*/
public function getComponents($loadedOnly = false)
{
if ($loadedOnly) {
$components = [];
foreach ($this->_components as $component) {
if ($component instanceof Component) {
$components[] = $component;
}
}
return $components;
} else {
return $this->_components;
}
}
/**
* Registers a set of components in this module.
*
* Each component should be specified as a name-value pair, where
* name refers to the ID of the component and value the component or a configuration
* array that can be used to create the component. In the latter case, [[Yii::createObject()]]
* will be used to create the component.
*
* If a new component has the same ID as an existing one, the existing one will be overwritten silently.
*
* The following is an example for setting two components:
*
* ~~~
* [
* 'db' => [
* 'class' => 'yii\db\Connection',
* 'dsn' => 'sqlite:path/to/file.db',
* ],
* 'cache' => [
* 'class' => 'yii\caching\DbCache',
* 'db' => 'db',
* ],
* ]
* ~~~
*
* @param array $components components (id => component configuration or instance)
*/
public function setComponents($components)
{
foreach ($components as $id => $component) {
if (!is_object($component) && isset($this->_components[$id]['class']) && !isset($component['class'])) {
// set default component class
$component['class'] = $this->_components[$id]['class'];
}
$this->_components[$id] = $component;
}
}
/**
* Loads components that are declared in [[preload]].
* @throws InvalidConfigException if a component or module to be preloaded is unknown
*/
public function preloadComponents()
{
foreach ($this->preload as $id) {
if ($this->hasComponent($id)) {
$this->getComponent($id);
if ($this->has($id)) {
$this->get($id);
} elseif ($this->hasModule($id)) {
$this->getModule($id);
} else {
......
......@@ -80,7 +80,7 @@ class DbCache extends Cache
{
parent::init();
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($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.");
......
......@@ -45,7 +45,7 @@ class DbDependency extends Dependency
*/
protected function generateDependencyData($cache)
{
$db = Yii::$app->getComponent($this->db);
$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.");
}
......
......@@ -154,7 +154,7 @@ class Application extends \yii\base\Application
*/
public function getResponse()
{
return $this->getComponent('response');
return $this->get('response');
}
/**
......@@ -193,15 +193,13 @@ class Application extends \yii\base\Application
}
/**
* Registers the core application components.
* @see setComponents
* @inheritdoc
*/
public function registerCoreComponents()
public function coreComponents()
{
parent::registerCoreComponents();
$this->setComponents([
return array_merge([
'request' => ['class' => 'yii\console\Request'],
'response' => ['class' => 'yii\console\Response'],
]);
], parent::coreComponents());
}
}
......@@ -26,7 +26,7 @@ class CacheController extends Controller
public function actionIndex()
{
$caches = [];
$components = Yii::$app->getComponents();
$components = Yii::$app->getComponentDefinitions();
foreach ($components as $name => $component) {
if ($component instanceof Cache) {
$caches[$name] = get_class($component);
......@@ -53,7 +53,7 @@ class CacheController extends Controller
public function actionFlush($component = 'cache')
{
/** @var Cache $cache */
$cache = Yii::$app->getComponent($component);
$cache = Yii::$app->get($component);
if (!$cache || !$cache instanceof Cache) {
throw new Exception('Application component "'.$component.'" is not defined or not a cache.');
}
......
......@@ -129,7 +129,7 @@ class MessageController extends Controller
}
}
} elseif ($config['format'] === 'db') {
$db = \Yii::$app->getComponent(isset($config['db']) ? $config['db'] : 'db');
$db = \Yii::$app->get(isset($config['db']) ? $config['db'] : 'db');
if (!$db instanceof \yii\db\Connection) {
throw new Exception('The "db" option must refer to a valid database application component.');
}
......
......@@ -122,7 +122,7 @@ class MigrateController extends Controller
if ($action->id !== 'create') {
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($this->db);
$this->db = Yii::$app->get($this->db);
}
if (!$this->db instanceof Connection) {
throw new Exception("The 'db' option must refer to the application component ID of a DB connection.");
......
......@@ -85,7 +85,7 @@ class ActiveDataProvider extends BaseDataProvider
{
parent::init();
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($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.');
}
......
......@@ -90,7 +90,7 @@ class SqlDataProvider extends BaseDataProvider
{
parent::init();
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($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.');
......
......@@ -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->getComponent($db->queryCache) : $db->queryCache;
$cache = is_string($db->queryCache) ? Yii::$app->get($db->queryCache) : $db->queryCache;
}
if (isset($cache) && $cache instanceof Cache) {
......
......@@ -49,7 +49,7 @@ class Migration extends \yii\base\Component
{
parent::init();
if ($this->db === null) {
$this->db = \Yii::$app->getComponent('db');
$this->db = \Yii::$app->get('db');
}
}
......
......@@ -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->getComponent($db->schemaCache) : $db->schemaCache;
$cache = is_string($db->schemaCache) ? Yii::$app->get($db->schemaCache) : $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->getComponent($this->db->schemaCache) : $this->db->schemaCache;
$cache = is_string($this->db->schemaCache) ? Yii::$app->get($this->db->schemaCache) : $this->db->schemaCache;
if ($this->db->enableSchemaCache && $cache instanceof Cache) {
GroupDependency::invalidate($cache, $this->getCacheGroup());
}
......
......@@ -191,7 +191,7 @@ trait ContainerTrait
$definition['class'] = $typeOrID;
$this->_definitions[$typeOrID] = $definition;
} else {
throw new InvalidConfigException("The configuration for \"$typeOrID\" must contain a \"class\" element.");
throw new InvalidConfigException("The configuration for the \"$typeOrID\" component must contain a \"class\" element.");
}
} else {
// a type or ID
......
......@@ -94,14 +94,14 @@ class DbMessageSource extends MessageSource
{
parent::init();
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($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.");
}
if ($this->enableCaching) {
if (is_string($this->cache)) {
$this->cache = Yii::$app->getComponent($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.");
......
......@@ -63,7 +63,7 @@ class DbTarget extends Target
{
parent::init();
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($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.");
......
......@@ -44,7 +44,7 @@ class EmailTarget extends Target
throw new InvalidConfigException('The "to" option must be set for EmailTarget::message.');
}
if (is_string($this->mail)) {
$this->mail = Yii::$app->getComponent($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.");
......
......@@ -247,10 +247,10 @@ abstract class Target extends Component
$request = Yii::$app->getRequest();
$ip = $request instanceof Request ? $request->getUserIP() : '-';
/** @var \yii\web\User $user */
$user = Yii::$app->getComponent('user', false);
$user = Yii::$app->get('user', [], false);
$userID = $user ? $user->getId(false) : '-';
/** @var \yii\web\Session $session */
$session = Yii::$app->getComponent('session', false);
$session = Yii::$app->get('session', [], false);
$sessionID = $session && $session->getIsActive() ? $session->getId() : '-';
return "[$ip] [$userID] [$sessionID]";
}
......
......@@ -32,7 +32,7 @@ abstract class DbMutex extends Mutex
{
parent::init();
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($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.');
......
......@@ -60,7 +60,7 @@ class DbManager extends Manager
public function init()
{
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($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.");
......
......@@ -35,7 +35,7 @@ abstract class DbFixture extends Fixture
{
parent::init();
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($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.");
......
......@@ -129,7 +129,7 @@ class Application extends \yii\base\Application
*/
public function getRequest()
{
return $this->getComponent('request');
return $this->get('request');
}
/**
......@@ -138,7 +138,7 @@ class Application extends \yii\base\Application
*/
public function getResponse()
{
return $this->getComponent('response');
return $this->get('response');
}
/**
......@@ -147,7 +147,7 @@ class Application extends \yii\base\Application
*/
public function getSession()
{
return $this->getComponent('session');
return $this->get('session');
}
/**
......@@ -156,7 +156,7 @@ class Application extends \yii\base\Application
*/
public function getUser()
{
return $this->getComponent('user');
return $this->get('user');
}
/**
......@@ -165,22 +165,20 @@ class Application extends \yii\base\Application
*/
public function getAssetManager()
{
return $this->getComponent('assetManager');
return $this->get('assetManager');
}
/**
* Registers the core application components.
* @see setComponents
* @inheritdoc
*/
public function registerCoreComponents()
public function coreComponents()
{
parent::registerCoreComponents();
$this->setComponents([
return array_merge([
'request' => ['class' => 'yii\web\Request'],
'response' => ['class' => 'yii\web\Response'],
'session' => ['class' => 'yii\web\Session'],
'user' => ['class' => 'yii\web\User'],
'assetManager' => ['class' => 'yii\web\AssetManager'],
]);
], parent::coreComponents());
}
}
......@@ -53,7 +53,7 @@ class CacheSession extends Session
public function init()
{
if (is_string($this->cache)) {
$this->cache = Yii::$app->getComponent($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.');
......
......@@ -75,7 +75,7 @@ class DbSession extends Session
public function init()
{
if (is_string($this->db)) {
$this->db = Yii::$app->getComponent($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.");
......
......@@ -145,7 +145,7 @@ class UrlManager extends Component
return;
}
if (is_string($this->cache)) {
$this->cache = Yii::$app->getComponent($this->cache);
$this->cache = Yii::$app->get($this->cache);
}
if ($this->cache instanceof Cache) {
$key = __CLASS__;
......
......@@ -82,7 +82,7 @@ class FragmentCache extends Widget
if (!$this->enabled) {
$this->cache = null;
} elseif (is_string($this->cache)) {
$this->cache = Yii::$app->getComponent($this->cache);
$this->cache = Yii::$app->get($this->cache);
}
if ($this->getCachedContent() === false) {
......
......@@ -68,7 +68,7 @@ class MessageTest extends VendorTestCase
*/
protected function createTestMessage()
{
return Yii::$app->getComponent('mail')->compose();
return Yii::$app->get('mail')->compose();
}
/**
......
......@@ -59,7 +59,7 @@ class BaseMailerTest extends TestCase
*/
protected function getTestMailComponent()
{
return Yii::$app->getComponent('mail');
return Yii::$app->get('mail');
}
// Tests :
......
......@@ -36,7 +36,7 @@ class BaseMessageTest extends TestCase
*/
protected function getMailer()
{
return Yii::$app->getComponent('mail');
return Yii::$app->get('mail');
}
// Tests :
......
......@@ -58,7 +58,7 @@ class ActiveFixtureTest extends DatabaseTestCase
public function setUp()
{
parent::setUp();
\Yii::$app->setComponent('db', $this->getConnection());
\Yii::$app->set('db', $this->getConnection());
ActiveRecord::$db = $this->getConnection();
}
......
......@@ -15,7 +15,7 @@ class CacheSessionTest extends \yiiunit\TestCase
{
parent::setUp();
$this->mockApplication();
Yii::$app->setComponent('cache', new FileCache());
Yii::$app->set('cache', new FileCache());
}
public function testCacheSession()
......
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