Commit 586f5b87 by Qiang Xue

Removed `Application::preload` in favor of `Application::bootstrap`

parent b2c8dba0
...@@ -9,7 +9,6 @@ $params = array_merge( ...@@ -9,7 +9,6 @@ $params = array_merge(
return [ return [
'id' => 'app-backend', 'id' => 'app-backend',
'basePath' => dirname(__DIR__), 'basePath' => dirname(__DIR__),
'preload' => ['log'],
'controllerNamespace' => 'backend\controllers', 'controllerNamespace' => 'backend\controllers',
'modules' => [], 'modules' => [],
'components' => [ 'components' => [
......
...@@ -4,7 +4,7 @@ $config = []; ...@@ -4,7 +4,7 @@ $config = [];
if (!YII_ENV_TEST) { if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment // configuration adjustments for 'dev' environment
$config['preload'][] = 'debug'; $config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module'; $config['modules']['debug'] = 'yii\debug\Module';
$config['modules']['gii'] = 'yii\gii\Module'; $config['modules']['gii'] = 'yii\gii\Module';
} }
......
...@@ -4,7 +4,7 @@ $config = []; ...@@ -4,7 +4,7 @@ $config = [];
if (!YII_ENV_TEST) { if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment // configuration adjustments for 'dev' environment
$config['preload'][] = 'debug'; $config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module'; $config['modules']['debug'] = 'yii\debug\Module';
$config['modules']['gii'] = 'yii\gii\Module'; $config['modules']['gii'] = 'yii\gii\Module';
} }
......
...@@ -8,7 +8,6 @@ $db = require(__DIR__ . '/db.php'); ...@@ -8,7 +8,6 @@ $db = require(__DIR__ . '/db.php');
return [ return [
'id' => 'basic-console', 'id' => 'basic-console',
'basePath' => dirname(__DIR__), 'basePath' => dirname(__DIR__),
'preload' => ['log'],
'controllerNamespace' => 'app\commands', 'controllerNamespace' => 'app\commands',
'extensions' => require(__DIR__ . '/../vendor/yiisoft/extensions.php'), 'extensions' => require(__DIR__ . '/../vendor/yiisoft/extensions.php'),
'components' => [ 'components' => [
......
...@@ -38,7 +38,7 @@ $config = [ ...@@ -38,7 +38,7 @@ $config = [
if (YII_ENV_DEV) { if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment // configuration adjustments for 'dev' environment
$config['preload'][] = 'debug'; $config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module'; $config['modules']['debug'] = 'yii\debug\Module';
$config['modules']['gii'] = 'yii\gii\Module'; $config['modules']['gii'] = 'yii\gii\Module';
} }
......
...@@ -129,9 +129,9 @@ namespace myname\mywidget; ...@@ -129,9 +129,9 @@ namespace myname\mywidget;
use yii\base\BootstrapInterface; use yii\base\BootstrapInterface;
use yii\base\Application; use yii\base\Application;
class Bootstrap implements BootstrapInterface class MyBootstrapClass implements BootstrapInterface
{ {
public function bootstrap(Application $app) public function bootstrap($app)
{ {
$app->on(Application::EVENT_BEFORE_REQUEST, function () { $app->on(Application::EVENT_BEFORE_REQUEST, function () {
// do something here // do something here
...@@ -145,7 +145,7 @@ You then list this bootstrap class in `composer.json` as follows, ...@@ -145,7 +145,7 @@ You then list this bootstrap class in `composer.json` as follows,
```json ```json
{ {
"extra": { "extra": {
"bootstrap": "path\\to\\MyBootstrapClass" "bootstrap": "myname\\mywidget\\MyBootstrapClass"
} }
} }
``` ```
......
...@@ -58,7 +58,7 @@ In basic application template configuration structure is a bit different so Gii ...@@ -58,7 +58,7 @@ In basic application template configuration structure is a bit different so Gii
if (YII_ENV_DEV) if (YII_ENV_DEV)
{ {
// configuration adjustments for 'dev' environment // configuration adjustments for 'dev' environment
$config['preload'][] = 'debug'; $config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module'; $config['modules']['debug'] = 'yii\debug\Module';
$config['modules']['gii'] = 'yii\gii\Module'; // <--- here $config['modules']['gii'] = 'yii\gii\Module'; // <--- here
} }
...@@ -70,7 +70,7 @@ So in order to adjust IP address you need to do it like the following: ...@@ -70,7 +70,7 @@ So in order to adjust IP address you need to do it like the following:
if (YII_ENV_DEV) if (YII_ENV_DEV)
{ {
// configuration adjustments for 'dev' environment // configuration adjustments for 'dev' environment
$config['preload'][] = 'debug'; $config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module'; $config['modules']['debug'] = 'yii\debug\Module';
$config['modules']['gii'] = [ $config['modules']['gii'] = [
'class' => 'yii\gii\Module', 'class' => 'yii\gii\Module',
......
...@@ -23,7 +23,7 @@ Installing and configuring ...@@ -23,7 +23,7 @@ Installing and configuring
To enable these features, add these lines to your configuration file to enable the debug module: To enable these features, add these lines to your configuration file to enable the debug module:
```php ```php
'preload' => ['debug'], 'bootstrap' => ['debug'],
'modules' => [ 'modules' => [
'debug' => 'yii\debug\Module', 'debug' => 'yii\debug\Module',
] ]
...@@ -32,7 +32,7 @@ To enable these features, add these lines to your configuration file to enable t ...@@ -32,7 +32,7 @@ To enable these features, add these lines to your configuration file to enable t
By default, the debug module only works when browsing the website from localhost. If you want to use it on a remote (staging) server, add the parameter `allowedIPs` to the configuration to whitelist your IP: By default, the debug module only works when browsing the website from localhost. If you want to use it on a remote (staging) server, add the parameter `allowedIPs` to the configuration to whitelist your IP:
```php ```php
'preload' => ['debug'], 'bootstrap' => ['debug'],
'modules' => [ 'modules' => [
'debug' => [ 'debug' => [
'class' => 'yii\debug\Module', 'class' => 'yii\debug\Module',
...@@ -169,7 +169,7 @@ Now it's time to tell the debugger to use the new panel. In `config/web.php`, th ...@@ -169,7 +169,7 @@ Now it's time to tell the debugger to use the new panel. In `config/web.php`, th
```php ```php
if (YII_ENV_DEV) { if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment // configuration adjustments for 'dev' environment
$config['preload'][] = 'debug'; $config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [ $config['modules']['debug'] = [
'class' => 'yii\debug\Module', 'class' => 'yii\debug\Module',
'panels' => [ 'panels' => [
......
...@@ -9,6 +9,7 @@ namespace yii\debug; ...@@ -9,6 +9,7 @@ namespace yii\debug;
use Yii; use Yii;
use yii\base\Application; use yii\base\Application;
use yii\base\BootstrapInterface;
use yii\helpers\Url; use yii\helpers\Url;
use yii\web\View; use yii\web\View;
use yii\web\ForbiddenHttpException; use yii\web\ForbiddenHttpException;
...@@ -19,7 +20,7 @@ use yii\web\ForbiddenHttpException; ...@@ -19,7 +20,7 @@ use yii\web\ForbiddenHttpException;
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class Module extends \yii\base\Module class Module extends \yii\base\Module implements BootstrapInterface
{ {
/** /**
* @var array the list of IPs that are allowed to access this module. * @var array the list of IPs that are allowed to access this module.
...@@ -72,10 +73,6 @@ class Module extends \yii\base\Module ...@@ -72,10 +73,6 @@ class Module extends \yii\base\Module
parent::init(); parent::init();
$this->dataPath = Yii::getAlias($this->dataPath); $this->dataPath = Yii::getAlias($this->dataPath);
$this->logTarget = Yii::$app->getLog()->targets['debug'] = new LogTarget($this); $this->logTarget = Yii::$app->getLog()->targets['debug'] = new LogTarget($this);
// do not initialize view component before application is ready (needed when debug in preload)
Yii::$app->on(Application::EVENT_BEFORE_REQUEST, function () {
Yii::$app->getView()->on(View::EVENT_END_BODY, [$this, 'renderToolbar']);
});
// merge custom panels and core panels so that they are ordered mainly by custom panels // merge custom panels and core panels so that they are ordered mainly by custom panels
if (empty($this->panels)) { if (empty($this->panels)) {
...@@ -100,6 +97,17 @@ class Module extends \yii\base\Module ...@@ -100,6 +97,17 @@ class Module extends \yii\base\Module
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function bootstrap($app)
{
// delay attaching event handler to the view component after it is fully configured
$app->on(Application::EVENT_BEFORE_REQUEST, function () use ($app) {
$app->getView()->on(View::EVENT_END_BODY, [$this, 'renderToolbar']);
});
}
/**
* @inheritdoc
*/
public function beforeAction($action) public function beforeAction($action)
{ {
Yii::$app->getView()->off(View::EVENT_END_BODY, [$this, 'renderToolbar']); Yii::$app->getView()->off(View::EVENT_END_BODY, [$this, 'renderToolbar']);
......
...@@ -33,7 +33,7 @@ Once the extension is installed, simply modify your application configuration as ...@@ -33,7 +33,7 @@ Once the extension is installed, simply modify your application configuration as
```php ```php
return [ return [
'preload' => ['debug'], 'bootstrap' => ['debug'],
'modules' => [ 'modules' => [
'debug' => 'yii\debug\Module', 'debug' => 'yii\debug\Module',
... ...
......
...@@ -176,7 +176,7 @@ enabled, it is sufficient to just add the panels configuration): ...@@ -176,7 +176,7 @@ enabled, it is sufficient to just add the panels configuration):
```php ```php
// ... // ...
'preload' => 'debug', 'bootstrap' => ['debug'],
'modules' => [ 'modules' => [
'debug' => [ 'debug' => [
'class' => 'yii\\debug\\Module', 'class' => 'yii\\debug\\Module',
......
...@@ -279,6 +279,7 @@ Yii Framework 2 Change Log ...@@ -279,6 +279,7 @@ Yii Framework 2 Change Log
- Chg: Removed `yii\grid\Column::getDataCellContent()` and renamed `yii\grid\DataColumn::getDataCellContent()` to `yii\grid\DataColumn::getDataCellValue()` (cebe) - Chg: Removed `yii\grid\Column::getDataCellContent()` and renamed `yii\grid\DataColumn::getDataCellContent()` to `yii\grid\DataColumn::getDataCellValue()` (cebe)
- Chg: `yii\log\Logger` is split into `yii\log\Logger` and `yii\log\Dispatcher`. (qiangxue) - Chg: `yii\log\Logger` is split into `yii\log\Logger` and `yii\log\Dispatcher`. (qiangxue)
- Chg: Moved all filter classes to namespace `yii\filters` (qiangxue) - Chg: Moved all filter classes to namespace `yii\filters` (qiangxue)
- Chg: Removed `Application::preload` in favor of `Application::bootstrap` (qiangxue)
- New #66: [Auth client library](https://github.com/yiisoft/yii2-authclient) OpenId, OAuth1, OAuth2 clients (klimov-paul) - New #66: [Auth client library](https://github.com/yiisoft/yii2-authclient) OpenId, OAuth1, OAuth2 clients (klimov-paul)
- New #303: Added built-in support for REST API (qiangxue) - New #303: Added built-in support for REST API (qiangxue)
- New #503: Added `yii\di\Container` and `yii\di\ServiceLocator` (qiangxue) - New #503: Added `yii\di\Container` and `yii\di\ServiceLocator` (qiangxue)
......
...@@ -8,8 +8,6 @@ ...@@ -8,8 +8,6 @@
namespace yii\base; namespace yii\base;
use Yii; use Yii;
use yii\helpers\Console;
use yii\web\HttpException;
/** /**
* Application is the base class for all application classes. * Application is the base class for all application classes.
...@@ -145,9 +143,19 @@ abstract class Application extends Module ...@@ -145,9 +143,19 @@ abstract class Application extends Module
*/ */
public $extensions = []; public $extensions = [];
/** /**
* @var array list of bootstrap classes or their configurations. A bootstrap class must implement * @var array list of bootstrap components that should be run right after
* [[BootstrapInterface]]. The [[BootstrapInterface::bootstrap()]] method of each bootstrap class * the application is configured.
* will be invoked at the beginning of [[init()]]. *
* Each bootstrap component may be specified in one of the following formats:
*
* - an application component ID as specified via [[components]].
* - a module ID as specified via [[modules]].
* - a class name.
* - a configuration array.
*
* Note that a bootstrap component must implement [[BootstrapInterface]], or an exception will be thrown.
* The [[BootstrapInterface::bootstrap()]] method of each bootstrap component will be called
* when the component is instantiated and run in in [[bootstrap()]].
*/ */
public $bootstrap = []; public $bootstrap = [];
/** /**
...@@ -233,45 +241,53 @@ abstract class Application extends Module ...@@ -233,45 +241,53 @@ abstract class Application extends Module
public function init() public function init()
{ {
$this->state = self::STATE_INIT; $this->state = self::STATE_INIT;
$this->get('log');
$this->initExtensions($this->extensions); $this->bootstrap();
foreach ($this->bootstrap as $class) {
/** @var BootstrapInterface $bootstrap */
$bootstrap = Yii::createObject($class);
$bootstrap->bootstrap($this);
}
parent::init();
} }
/** /**
* Initializes the extensions. * Initializes extensions and executes bootstrap components.
* @param array $extensions the extensions to be initialized. Please refer to [[extensions]] * This method is called by [[init()]] after the application has been fully configured.
* for the structure of the extension array. * If you override this method, make sure you also call the parent implementation.
* @throws InvalidConfigException if a bootstrap component does not implement BootstrapInterface
*/ */
protected function initExtensions($extensions) protected function bootstrap()
{ {
foreach ($extensions as $extension) { foreach ($this->extensions as $extension) {
if (!empty($extension['alias'])) { if (!empty($extension['alias'])) {
foreach ($extension['alias'] as $name => $path) { foreach ($extension['alias'] as $name => $path) {
Yii::setAlias($name, $path); Yii::setAlias($name, $path);
} }
} }
if (isset($extension['bootstrap'])) { if (isset($extension['bootstrap'])) {
/** @var BootstrapInterface $bootstrap */ $component = Yii::createObject($extension['bootstrap']);
$bootstrap = Yii::createObject($extension['bootstrap']); if ($component instanceof BootstrapInterface) {
$bootstrap->bootstrap($this); $component->bootstrap($this);
} else {
$class = is_array($extension['bootstrap']) ? $extension['bootstrap']['class'] : $extension['bootstrap'];
throw new InvalidConfigException("$class must implement \\yii\\base\\BootstrapInterface");
}
} }
} }
}
/** foreach ($this->bootstrap as $class) {
* Loads components that are declared in [[preload]]. if (is_string($class)) {
* @throws InvalidConfigException if a component or module to be preloaded is unknown if ($this->has($class)) {
*/ $component = $this->get($class);
public function preloadComponents() } elseif ($this->hasModule($class)) {
{ $component = $this->getModule($class);
$this->get('log'); }
parent::preloadComponents(); }
if (!isset($component)) {
$component = Yii::createObject($class);
}
if ($component instanceof BootstrapInterface) {
$component->bootstrap($this);
} else {
throw new InvalidConfigException((is_array($class) ? $class['class'] : $class) . " must implement \\yii\\base\\BootstrapInterface");
}
}
} }
/** /**
......
...@@ -56,5 +56,5 @@ interface BootstrapInterface ...@@ -56,5 +56,5 @@ interface BootstrapInterface
* Bootstrap method to be called during application bootstrap stage. * Bootstrap method to be called during application bootstrap stage.
* @param Application $app the application currently running * @param Application $app the application currently running
*/ */
public function bootstrap(Application $app); public function bootstrap($app);
} }
...@@ -52,10 +52,6 @@ class Module extends ServiceLocator ...@@ -52,10 +52,6 @@ class Module extends ServiceLocator
*/ */
public $params = []; public $params = [];
/** /**
* @var array the IDs of the components or modules that should be preloaded right after initialization.
*/
public $preload = [];
/**
* @var string an ID that uniquely identifies this module among other modules which have the same [[module|parent]]. * @var string an ID that uniquely identifies this module among other modules which have the same [[module|parent]].
*/ */
public $id; public $id;
...@@ -137,9 +133,10 @@ class Module extends ServiceLocator ...@@ -137,9 +133,10 @@ class Module extends ServiceLocator
/** /**
* Initializes the module. * Initializes the module.
*
* This method is called after the module is created and initialized with property values * This method is called after the module is created and initialized with property values
* given in configuration. The default implementation will call [[preloadComponents()]] to * given in configuration. The default implementation will initialize [[controllerNamespace]]
* load components that are declared in [[preload]]. * if it is not set.
* *
* If you override this method, please make sure you call the parent implementation. * If you override this method, please make sure you call the parent implementation.
*/ */
...@@ -151,7 +148,6 @@ class Module extends ServiceLocator ...@@ -151,7 +148,6 @@ class Module extends ServiceLocator
$this->controllerNamespace = substr($class, 0, $pos) . '\\controllers'; $this->controllerNamespace = substr($class, 0, $pos) . '\\controllers';
} }
} }
$this->preloadComponents();
} }
/** /**
...@@ -407,23 +403,6 @@ class Module extends ServiceLocator ...@@ -407,23 +403,6 @@ class Module extends ServiceLocator
} }
/** /**
* 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->has($id)) {
$this->get($id);
} elseif ($this->hasModule($id)) {
$this->getModule($id);
} else {
throw new InvalidConfigException("Unknown component or module: $id");
}
}
}
/**
* Runs a controller action specified by a route. * Runs a controller action specified by a route.
* This method parses the specified route and creates the corresponding child module(s), controller and action * This method parses the specified route and creates the corresponding child module(s), controller and action
* instances. It then calls [[Controller::runAction()]] to run the action with the given parameters. * instances. It then calls [[Controller::runAction()]] to run the action with the given parameters.
......
...@@ -56,12 +56,13 @@ class Application extends \yii\base\Application ...@@ -56,12 +56,13 @@ class Application extends \yii\base\Application
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function preloadComponents() protected function bootstrap()
{ {
parent::preloadComponents();
$request = $this->getRequest(); $request = $this->getRequest();
Yii::setAlias('@webroot', dirname($request->getScriptFile())); Yii::setAlias('@webroot', dirname($request->getScriptFile()));
Yii::setAlias('@web', $request->getBaseUrl()); Yii::setAlias('@web', $request->getBaseUrl());
parent::bootstrap();
} }
/** /**
......
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