diff --git a/extensions/authclient/AuthAction.php b/extensions/authclient/AuthAction.php index 820e41e..c2f4457 100644 --- a/extensions/authclient/AuthAction.php +++ b/extensions/authclient/AuthAction.php @@ -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}'"); } diff --git a/extensions/authclient/widgets/Choice.php b/extensions/authclient/widgets/Choice.php index d5622a7..50d830f 100644 --- a/extensions/authclient/widgets/Choice.php +++ b/extensions/authclient/widgets/Choice.php @@ -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(); } diff --git a/extensions/debug/panels/RequestPanel.php b/extensions/debug/panels/RequestPanel.php index 53971c1..750d433 100644 --- a/extensions/debug/panels/RequestPanel.php +++ b/extensions/debug/panels/RequestPanel.php @@ -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() : [], diff --git a/extensions/elasticsearch/ActiveRecord.php b/extensions/elasticsearch/ActiveRecord.php index 75bb515..097cbc4 100644 --- a/extensions/elasticsearch/ActiveRecord.php +++ b/extensions/elasticsearch/ActiveRecord.php @@ -59,7 +59,7 @@ class ActiveRecord extends BaseActiveRecord */ public static function getDb() { - return \Yii::$app->getComponent('elasticsearch'); + return \Yii::$app->get('elasticsearch'); } /** diff --git a/extensions/elasticsearch/DebugAction.php b/extensions/elasticsearch/DebugAction.php index 59b178c..f0619c2 100644 --- a/extensions/elasticsearch/DebugAction.php +++ b/extensions/elasticsearch/DebugAction.php @@ -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; diff --git a/extensions/elasticsearch/Query.php b/extensions/elasticsearch/Query.php index 63a2c88..e44adf3 100644 --- a/extensions/elasticsearch/Query.php +++ b/extensions/elasticsearch/Query.php @@ -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); diff --git a/extensions/gii/generators/model/Generator.php b/extensions/gii/generators/model/Generator.php index 7f030f1..e0c0a44 100644 --- a/extensions/gii/generators/model/Generator.php +++ b/extensions/gii/generators/model/Generator.php @@ -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.'); } } diff --git a/extensions/mongodb/ActiveRecord.php b/extensions/mongodb/ActiveRecord.php index ebeda89..66f9267 100644 --- a/extensions/mongodb/ActiveRecord.php +++ b/extensions/mongodb/ActiveRecord.php @@ -29,7 +29,7 @@ abstract class ActiveRecord extends BaseActiveRecord */ public static function getDb() { - return \Yii::$app->getComponent('mongodb'); + return \Yii::$app->get('mongodb'); } /** diff --git a/extensions/mongodb/Cache.php b/extensions/mongodb/Cache.php index 7f4edce..f9265a0 100644 --- a/extensions/mongodb/Cache.php +++ b/extensions/mongodb/Cache.php @@ -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."); diff --git a/extensions/mongodb/Query.php b/extensions/mongodb/Query.php index 3058ea0..86bec38 100644 --- a/extensions/mongodb/Query.php +++ b/extensions/mongodb/Query.php @@ -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); diff --git a/extensions/mongodb/Session.php b/extensions/mongodb/Session.php index a969f75..8d9076d 100644 --- a/extensions/mongodb/Session.php +++ b/extensions/mongodb/Session.php @@ -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."); diff --git a/extensions/mongodb/file/Query.php b/extensions/mongodb/file/Query.php index c801828..b4bb937 100644 --- a/extensions/mongodb/file/Query.php +++ b/extensions/mongodb/file/Query.php @@ -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); diff --git a/extensions/redis/ActiveRecord.php b/extensions/redis/ActiveRecord.php index 980265a..3244d6b 100644 --- a/extensions/redis/ActiveRecord.php +++ b/extensions/redis/ActiveRecord.php @@ -45,7 +45,7 @@ class ActiveRecord extends BaseActiveRecord */ public static function getDb() { - return \Yii::$app->getComponent('redis'); + return \Yii::$app->get('redis'); } /** diff --git a/extensions/redis/Cache.php b/extensions/redis/Cache.php index 02438ae..a4edec4 100644 --- a/extensions/redis/Cache.php +++ b/extensions/redis/Cache.php @@ -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(); diff --git a/extensions/redis/Session.php b/extensions/redis/Session.php index 1d1fe76..29241bd 100644 --- a/extensions/redis/Session.php +++ b/extensions/redis/Session.php @@ -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(); diff --git a/extensions/sphinx/ActiveRecord.php b/extensions/sphinx/ActiveRecord.php index e25ab70..bd9832a 100644 --- a/extensions/sphinx/ActiveRecord.php +++ b/extensions/sphinx/ActiveRecord.php @@ -62,7 +62,7 @@ abstract class ActiveRecord extends BaseActiveRecord */ public static function getDb() { - return \Yii::$app->getComponent('sphinx'); + return \Yii::$app->get('sphinx'); } /** diff --git a/extensions/sphinx/Query.php b/extensions/sphinx/Query.php index b75abdd..2a3cfdf 100644 --- a/extensions/sphinx/Query.php +++ b/extensions/sphinx/Query.php @@ -155,7 +155,7 @@ class Query extends Component implements QueryInterface */ protected function defaultConnection() { - return Yii::$app->getComponent('sphinx'); + return Yii::$app->get('sphinx'); } /** diff --git a/extensions/sphinx/Schema.php b/extensions/sphinx/Schema.php index f1b20fa..ed7a2a7 100644 --- a/extensions/sphinx/Schema.php +++ b/extensions/sphinx/Schema.php @@ -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()); } diff --git a/framework/base/Application.php b/framework/base/Application.php index 52df984..72996e5 100644 --- a/framework/base/Application.php +++ b/framework/base/Application.php @@ -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'], - ]); + ]; } /** diff --git a/framework/base/Module.php b/framework/base/Module.php index ef7ac08..99f9e08 100644 --- a/framework/base/Module.php +++ b/framework/base/Module.php @@ -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 { diff --git a/framework/caching/DbCache.php b/framework/caching/DbCache.php index 0731b1e..a810dc5 100644 --- a/framework/caching/DbCache.php +++ b/framework/caching/DbCache.php @@ -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."); diff --git a/framework/caching/DbDependency.php b/framework/caching/DbDependency.php index a705d89..1b2fe01 100644 --- a/framework/caching/DbDependency.php +++ b/framework/caching/DbDependency.php @@ -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."); } diff --git a/framework/console/Application.php b/framework/console/Application.php index 0a6b793..c015d94 100644 --- a/framework/console/Application.php +++ b/framework/console/Application.php @@ -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()); } } diff --git a/framework/console/controllers/CacheController.php b/framework/console/controllers/CacheController.php index 0fcc047..c136447 100644 --- a/framework/console/controllers/CacheController.php +++ b/framework/console/controllers/CacheController.php @@ -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.'); } diff --git a/framework/console/controllers/MessageController.php b/framework/console/controllers/MessageController.php index be135bf..aeb828a 100644 --- a/framework/console/controllers/MessageController.php +++ b/framework/console/controllers/MessageController.php @@ -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.'); } diff --git a/framework/console/controllers/MigrateController.php b/framework/console/controllers/MigrateController.php index 01f35cf..0392ea8 100644 --- a/framework/console/controllers/MigrateController.php +++ b/framework/console/controllers/MigrateController.php @@ -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."); diff --git a/framework/data/ActiveDataProvider.php b/framework/data/ActiveDataProvider.php index 073eb1b..bc08678 100644 --- a/framework/data/ActiveDataProvider.php +++ b/framework/data/ActiveDataProvider.php @@ -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.'); } diff --git a/framework/data/SqlDataProvider.php b/framework/data/SqlDataProvider.php index 090c469..11eaeb7 100644 --- a/framework/data/SqlDataProvider.php +++ b/framework/data/SqlDataProvider.php @@ -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.'); diff --git a/framework/db/Command.php b/framework/db/Command.php index bf29e72..58e40e1 100644 --- a/framework/db/Command.php +++ b/framework/db/Command.php @@ -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) { diff --git a/framework/db/Migration.php b/framework/db/Migration.php index 89927a7..f5f85f5 100644 --- a/framework/db/Migration.php +++ b/framework/db/Migration.php @@ -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'); } } diff --git a/framework/db/Schema.php b/framework/db/Schema.php index 79625c9..40075f5 100644 --- a/framework/db/Schema.php +++ b/framework/db/Schema.php @@ -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()); } diff --git a/framework/di/ContainerTrait.php b/framework/di/ContainerTrait.php index e329c78..6cc933d 100644 --- a/framework/di/ContainerTrait.php +++ b/framework/di/ContainerTrait.php @@ -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 diff --git a/framework/i18n/DbMessageSource.php b/framework/i18n/DbMessageSource.php index 9b32368..51bb29d 100644 --- a/framework/i18n/DbMessageSource.php +++ b/framework/i18n/DbMessageSource.php @@ -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."); diff --git a/framework/log/DbTarget.php b/framework/log/DbTarget.php index 9322129..d8de874 100644 --- a/framework/log/DbTarget.php +++ b/framework/log/DbTarget.php @@ -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."); diff --git a/framework/log/EmailTarget.php b/framework/log/EmailTarget.php index e29ad66..1da69b6 100644 --- a/framework/log/EmailTarget.php +++ b/framework/log/EmailTarget.php @@ -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."); diff --git a/framework/log/Target.php b/framework/log/Target.php index 062b203..f26fef5 100644 --- a/framework/log/Target.php +++ b/framework/log/Target.php @@ -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]"; } diff --git a/framework/mutex/DbMutex.php b/framework/mutex/DbMutex.php index 5a5fe5d..c7de73a 100644 --- a/framework/mutex/DbMutex.php +++ b/framework/mutex/DbMutex.php @@ -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.'); diff --git a/framework/rbac/DbManager.php b/framework/rbac/DbManager.php index b1236e0..f1b77f4 100644 --- a/framework/rbac/DbManager.php +++ b/framework/rbac/DbManager.php @@ -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."); diff --git a/framework/test/DbFixture.php b/framework/test/DbFixture.php index 2b16549..3ba3b00 100644 --- a/framework/test/DbFixture.php +++ b/framework/test/DbFixture.php @@ -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."); diff --git a/framework/web/Application.php b/framework/web/Application.php index f991ec3..33cd961 100644 --- a/framework/web/Application.php +++ b/framework/web/Application.php @@ -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()); } } diff --git a/framework/web/CacheSession.php b/framework/web/CacheSession.php index 733fa27..ac7743e 100644 --- a/framework/web/CacheSession.php +++ b/framework/web/CacheSession.php @@ -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.'); diff --git a/framework/web/DbSession.php b/framework/web/DbSession.php index 798f0e9..10d1b24 100644 --- a/framework/web/DbSession.php +++ b/framework/web/DbSession.php @@ -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."); diff --git a/framework/web/UrlManager.php b/framework/web/UrlManager.php index c335112..f4e4ea2 100644 --- a/framework/web/UrlManager.php +++ b/framework/web/UrlManager.php @@ -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__; diff --git a/framework/widgets/FragmentCache.php b/framework/widgets/FragmentCache.php index 30f7a28..03e057d 100644 --- a/framework/widgets/FragmentCache.php +++ b/framework/widgets/FragmentCache.php @@ -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) { diff --git a/tests/unit/extensions/swiftmailer/MessageTest.php b/tests/unit/extensions/swiftmailer/MessageTest.php index f51ee23..b625848 100644 --- a/tests/unit/extensions/swiftmailer/MessageTest.php +++ b/tests/unit/extensions/swiftmailer/MessageTest.php @@ -68,7 +68,7 @@ class MessageTest extends VendorTestCase */ protected function createTestMessage() { - return Yii::$app->getComponent('mail')->compose(); + return Yii::$app->get('mail')->compose(); } /** diff --git a/tests/unit/framework/mail/BaseMailerTest.php b/tests/unit/framework/mail/BaseMailerTest.php index d5baf47..2942626 100644 --- a/tests/unit/framework/mail/BaseMailerTest.php +++ b/tests/unit/framework/mail/BaseMailerTest.php @@ -59,7 +59,7 @@ class BaseMailerTest extends TestCase */ protected function getTestMailComponent() { - return Yii::$app->getComponent('mail'); + return Yii::$app->get('mail'); } // Tests : diff --git a/tests/unit/framework/mail/BaseMessageTest.php b/tests/unit/framework/mail/BaseMessageTest.php index b8a0063..7a52b42 100644 --- a/tests/unit/framework/mail/BaseMessageTest.php +++ b/tests/unit/framework/mail/BaseMessageTest.php @@ -36,7 +36,7 @@ class BaseMessageTest extends TestCase */ protected function getMailer() { - return Yii::$app->getComponent('mail'); + return Yii::$app->get('mail'); } // Tests : diff --git a/tests/unit/framework/test/ActiveFixtureTest.php b/tests/unit/framework/test/ActiveFixtureTest.php index 6aead47..b73f4ff 100644 --- a/tests/unit/framework/test/ActiveFixtureTest.php +++ b/tests/unit/framework/test/ActiveFixtureTest.php @@ -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(); } diff --git a/tests/unit/framework/web/CacheSessionTest.php b/tests/unit/framework/web/CacheSessionTest.php index fa029bf..502be18 100644 --- a/tests/unit/framework/web/CacheSessionTest.php +++ b/tests/unit/framework/web/CacheSessionTest.php @@ -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()