Commit 586f5b87 by Qiang Xue

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

parent b2c8dba0
......@@ -9,7 +9,6 @@ $params = array_merge(
return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'preload' => ['log'],
'controllerNamespace' => 'backend\controllers',
'modules' => [],
'components' => [
......
......@@ -4,7 +4,7 @@ $config = [];
if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['preload'][] = 'debug';
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';
$config['modules']['gii'] = 'yii\gii\Module';
}
......
......@@ -4,7 +4,7 @@ $config = [];
if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['preload'][] = 'debug';
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';
$config['modules']['gii'] = 'yii\gii\Module';
}
......
......@@ -8,7 +8,6 @@ $db = require(__DIR__ . '/db.php');
return [
'id' => 'basic-console',
'basePath' => dirname(__DIR__),
'preload' => ['log'],
'controllerNamespace' => 'app\commands',
'extensions' => require(__DIR__ . '/../vendor/yiisoft/extensions.php'),
'components' => [
......
......@@ -38,7 +38,7 @@ $config = [
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['preload'][] = 'debug';
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';
$config['modules']['gii'] = 'yii\gii\Module';
}
......
......@@ -129,9 +129,9 @@ namespace myname\mywidget;
use yii\base\BootstrapInterface;
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 () {
// do something here
......@@ -145,7 +145,7 @@ You then list this bootstrap class in `composer.json` as follows,
```json
{
"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
if (YII_ENV_DEV)
{
// configuration adjustments for 'dev' environment
$config['preload'][] = 'debug';
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';
$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:
if (YII_ENV_DEV)
{
// configuration adjustments for 'dev' environment
$config['preload'][] = 'debug';
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
......
......@@ -23,7 +23,7 @@ Installing and configuring
To enable these features, add these lines to your configuration file to enable the debug module:
```php
'preload' => ['debug'],
'bootstrap' => ['debug'],
'modules' => [
'debug' => 'yii\debug\Module',
]
......@@ -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:
```php
'preload' => ['debug'],
'bootstrap' => ['debug'],
'modules' => [
'debug' => [
'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
```php
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['preload'][] = 'debug';
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
'panels' => [
......
......@@ -9,6 +9,7 @@ namespace yii\debug;
use Yii;
use yii\base\Application;
use yii\base\BootstrapInterface;
use yii\helpers\Url;
use yii\web\View;
use yii\web\ForbiddenHttpException;
......@@ -19,7 +20,7 @@ use yii\web\ForbiddenHttpException;
* @author Qiang Xue <qiang.xue@gmail.com>
* @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.
......@@ -72,10 +73,6 @@ class Module extends \yii\base\Module
parent::init();
$this->dataPath = Yii::getAlias($this->dataPath);
$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
if (empty($this->panels)) {
......@@ -100,6 +97,17 @@ class Module extends \yii\base\Module
/**
* @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)
{
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
```php
return [
'preload' => ['debug'],
'bootstrap' => ['debug'],
'modules' => [
'debug' => 'yii\debug\Module',
...
......
......@@ -176,7 +176,7 @@ enabled, it is sufficient to just add the panels configuration):
```php
// ...
'preload' => 'debug',
'bootstrap' => ['debug'],
'modules' => [
'debug' => [
'class' => 'yii\\debug\\Module',
......
......@@ -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: `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: 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 #303: Added built-in support for REST API (qiangxue)
- New #503: Added `yii\di\Container` and `yii\di\ServiceLocator` (qiangxue)
......
......@@ -8,8 +8,6 @@
namespace yii\base;
use Yii;
use yii\helpers\Console;
use yii\web\HttpException;
/**
* Application is the base class for all application classes.
......@@ -145,9 +143,19 @@ abstract class Application extends Module
*/
public $extensions = [];
/**
* @var array list of bootstrap classes or their configurations. A bootstrap class must implement
* [[BootstrapInterface]]. The [[BootstrapInterface::bootstrap()]] method of each bootstrap class
* will be invoked at the beginning of [[init()]].
* @var array list of bootstrap components that should be run right after
* the application is configured.
*
* 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 = [];
/**
......@@ -233,45 +241,53 @@ abstract class Application extends Module
public function init()
{
$this->state = self::STATE_INIT;
$this->initExtensions($this->extensions);
foreach ($this->bootstrap as $class) {
/** @var BootstrapInterface $bootstrap */
$bootstrap = Yii::createObject($class);
$bootstrap->bootstrap($this);
}
parent::init();
$this->get('log');
$this->bootstrap();
}
/**
* Initializes the extensions.
* @param array $extensions the extensions to be initialized. Please refer to [[extensions]]
* for the structure of the extension array.
* Initializes extensions and executes bootstrap components.
* This method is called by [[init()]] after the application has been fully configured.
* 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'])) {
foreach ($extension['alias'] as $name => $path) {
Yii::setAlias($name, $path);
}
}
if (isset($extension['bootstrap'])) {
/** @var BootstrapInterface $bootstrap */
$bootstrap = Yii::createObject($extension['bootstrap']);
$bootstrap->bootstrap($this);
$component = Yii::createObject($extension['bootstrap']);
if ($component instanceof BootstrapInterface) {
$component->bootstrap($this);
} else {
$class = is_array($extension['bootstrap']) ? $extension['bootstrap']['class'] : $extension['bootstrap'];
throw new InvalidConfigException("$class must implement \\yii\\base\\BootstrapInterface");
}
}
}
}
/**
* Loads components that are declared in [[preload]].
* @throws InvalidConfigException if a component or module to be preloaded is unknown
*/
public function preloadComponents()
{
$this->get('log');
parent::preloadComponents();
foreach ($this->bootstrap as $class) {
if (is_string($class)) {
if ($this->has($class)) {
$component = $this->get($class);
} elseif ($this->hasModule($class)) {
$component = $this->getModule($class);
}
}
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
* Bootstrap method to be called during application bootstrap stage.
* @param Application $app the application currently running
*/
public function bootstrap(Application $app);
public function bootstrap($app);
}
......@@ -52,10 +52,6 @@ class Module extends ServiceLocator
*/
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]].
*/
public $id;
......@@ -137,9 +133,10 @@ class Module extends ServiceLocator
/**
* Initializes the module.
*
* This method is called after the module is created and initialized with property values
* given in configuration. The default implementation will call [[preloadComponents()]] to
* load components that are declared in [[preload]].
* given in configuration. The default implementation will initialize [[controllerNamespace]]
* if it is not set.
*
* If you override this method, please make sure you call the parent implementation.
*/
......@@ -151,7 +148,6 @@ class Module extends ServiceLocator
$this->controllerNamespace = substr($class, 0, $pos) . '\\controllers';
}
}
$this->preloadComponents();
}
/**
......@@ -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.
* 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.
......
......@@ -56,12 +56,13 @@ class Application extends \yii\base\Application
/**
* @inheritdoc
*/
public function preloadComponents()
protected function bootstrap()
{
parent::preloadComponents();
$request = $this->getRequest();
Yii::setAlias('@webroot', dirname($request->getScriptFile()));
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