mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-10 23:50:38 +08:00
Refactored app bootstrap logic.
This commit is contained in:
@@ -10,6 +10,7 @@ return [
|
|||||||
'id' => 'app-backend',
|
'id' => 'app-backend',
|
||||||
'basePath' => dirname(__DIR__),
|
'basePath' => dirname(__DIR__),
|
||||||
'controllerNamespace' => 'backend\controllers',
|
'controllerNamespace' => 'backend\controllers',
|
||||||
|
'bootstrap' => ['log'],
|
||||||
'modules' => [],
|
'modules' => [],
|
||||||
'components' => [
|
'components' => [
|
||||||
'user' => [
|
'user' => [
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ $params = array_merge(
|
|||||||
return [
|
return [
|
||||||
'id' => 'app-console',
|
'id' => 'app-console',
|
||||||
'basePath' => dirname(__DIR__),
|
'basePath' => dirname(__DIR__),
|
||||||
|
'bootstrap' => ['log'],
|
||||||
'controllerNamespace' => 'console\controllers',
|
'controllerNamespace' => 'console\controllers',
|
||||||
'modules' => [],
|
'modules' => [],
|
||||||
'components' => [
|
'components' => [
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ $params = array_merge(
|
|||||||
return [
|
return [
|
||||||
'id' => 'app-frontend',
|
'id' => 'app-frontend',
|
||||||
'basePath' => dirname(__DIR__),
|
'basePath' => dirname(__DIR__),
|
||||||
|
'bootstrap' => ['log'],
|
||||||
'controllerNamespace' => 'frontend\controllers',
|
'controllerNamespace' => 'frontend\controllers',
|
||||||
'components' => [
|
'components' => [
|
||||||
'user' => [
|
'user' => [
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ $db = require(__DIR__ . '/db.php');
|
|||||||
return [
|
return [
|
||||||
'id' => 'basic-console',
|
'id' => 'basic-console',
|
||||||
'basePath' => dirname(__DIR__),
|
'basePath' => dirname(__DIR__),
|
||||||
|
'bootstrap' => ['log'],
|
||||||
'controllerNamespace' => 'app\commands',
|
'controllerNamespace' => 'app\commands',
|
||||||
'extensions' => require(__DIR__ . '/../vendor/yiisoft/extensions.php'),
|
'extensions' => require(__DIR__ . '/../vendor/yiisoft/extensions.php'),
|
||||||
'components' => [
|
'components' => [
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ $db = require(__DIR__ . '/db.php');
|
|||||||
$config = [
|
$config = [
|
||||||
'id' => 'basic',
|
'id' => 'basic',
|
||||||
'basePath' => dirname(__DIR__),
|
'basePath' => dirname(__DIR__),
|
||||||
|
'bootstrap' => ['log'],
|
||||||
'extensions' => require(__DIR__ . '/../vendor/yiisoft/extensions.php'),
|
'extensions' => require(__DIR__ . '/../vendor/yiisoft/extensions.php'),
|
||||||
'components' => [
|
'components' => [
|
||||||
'cache' => [
|
'cache' => [
|
||||||
|
|||||||
@@ -71,9 +71,15 @@ class Module extends \yii\base\Module implements BootstrapInterface
|
|||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
parent::init();
|
parent::init();
|
||||||
|
|
||||||
$this->dataPath = Yii::getAlias($this->dataPath);
|
$this->dataPath = Yii::getAlias($this->dataPath);
|
||||||
|
$this->initPanels();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes panels.
|
||||||
|
*/
|
||||||
|
protected function initPanels()
|
||||||
|
{
|
||||||
// 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)) {
|
||||||
$this->panels = $this->corePanels();
|
$this->panels = $this->corePanels();
|
||||||
|
|||||||
@@ -137,29 +137,32 @@ abstract class Application extends Module
|
|||||||
* [
|
* [
|
||||||
* 'name' => 'extension name',
|
* 'name' => 'extension name',
|
||||||
* 'version' => 'version number',
|
* 'version' => 'version number',
|
||||||
* 'bootstrap' => 'BootstrapClassName',
|
* 'bootstrap' => 'BootstrapClassName', // optional, may also be a configuration array
|
||||||
* 'alias' => [
|
* 'alias' => [
|
||||||
* '@alias1' => 'to/path1',
|
* '@alias1' => 'to/path1',
|
||||||
* '@alias2' => 'to/path2',
|
* '@alias2' => 'to/path2',
|
||||||
* ],
|
* ],
|
||||||
* ]
|
* ]
|
||||||
* ~~~
|
* ~~~
|
||||||
|
*
|
||||||
|
* The "bootstrap" class listed above will be instantiated during the application
|
||||||
|
* [[bootstrap()|bootstrapping process]]. If the class implements [[BootstrapInterface]],
|
||||||
|
* its [[BootstrapInterface::bootstrap()|bootstrap()]] method will be also be called.
|
||||||
*/
|
*/
|
||||||
public $extensions = [];
|
public $extensions = [];
|
||||||
/**
|
/**
|
||||||
* @var array list of bootstrap components that should be run right after
|
* @var array list of components that should be run during the application [[bootstrap()|bootstrapping process]].
|
||||||
* the application is configured.
|
|
||||||
*
|
*
|
||||||
* Each bootstrap component may be specified in one of the following formats:
|
* Each component may be specified in one of the following formats:
|
||||||
*
|
*
|
||||||
* - an application component ID as specified via [[components]].
|
* - an application component ID as specified via [[components]].
|
||||||
* - a module ID as specified via [[modules]].
|
* - a module ID as specified via [[modules]].
|
||||||
* - a class name.
|
* - a class name.
|
||||||
* - a configuration array.
|
* - a configuration array.
|
||||||
*
|
*
|
||||||
* Note that a bootstrap component must implement [[BootstrapInterface]], or an exception will be thrown.
|
* During the bootstrapping process, each component will be instantiated. If the component class
|
||||||
* The [[BootstrapInterface::bootstrap()]] method of each bootstrap component will be called
|
* implements [[BootstrapInterface]], its [[BootstrapInterface::bootstrap()|bootstrap()]] method
|
||||||
* when the component is instantiated and run in in [[bootstrap()]].
|
* will be also be called.
|
||||||
*/
|
*/
|
||||||
public $bootstrap = [];
|
public $bootstrap = [];
|
||||||
/**
|
/**
|
||||||
@@ -245,7 +248,6 @@ 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->bootstrap();
|
$this->bootstrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -253,7 +255,6 @@ abstract class Application extends Module
|
|||||||
* Initializes extensions and executes bootstrap components.
|
* Initializes extensions and executes bootstrap components.
|
||||||
* This method is called by [[init()]] after the application has been fully configured.
|
* 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.
|
* 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 bootstrap()
|
protected function bootstrap()
|
||||||
{
|
{
|
||||||
@@ -266,10 +267,10 @@ abstract class Application extends Module
|
|||||||
if (isset($extension['bootstrap'])) {
|
if (isset($extension['bootstrap'])) {
|
||||||
$component = Yii::createObject($extension['bootstrap']);
|
$component = Yii::createObject($extension['bootstrap']);
|
||||||
if ($component instanceof BootstrapInterface) {
|
if ($component instanceof BootstrapInterface) {
|
||||||
|
Yii::trace("Bootstrap with " . get_class($component) . '::bootstrap()', __METHOD__);
|
||||||
$component->bootstrap($this);
|
$component->bootstrap($this);
|
||||||
} else {
|
} else {
|
||||||
$class = is_array($extension['bootstrap']) ? $extension['bootstrap']['class'] : $extension['bootstrap'];
|
Yii::trace("Bootstrap with " . get_class($component), __METHOD__);
|
||||||
throw new InvalidConfigException("$class must implement \\yii\\base\\BootstrapInterface");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -280,6 +281,8 @@ abstract class Application extends Module
|
|||||||
$component = $this->get($class);
|
$component = $this->get($class);
|
||||||
} elseif ($this->hasModule($class)) {
|
} elseif ($this->hasModule($class)) {
|
||||||
$component = $this->getModule($class);
|
$component = $this->getModule($class);
|
||||||
|
} elseif (strpos($class, '\\') === false) {
|
||||||
|
throw new InvalidConfigException("Unknown bootstrap component ID: $class");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!isset($component)) {
|
if (!isset($component)) {
|
||||||
@@ -287,9 +290,10 @@ abstract class Application extends Module
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ($component instanceof BootstrapInterface) {
|
if ($component instanceof BootstrapInterface) {
|
||||||
|
Yii::trace("Bootstrap with " . get_class($component) . '::bootstrap()', __METHOD__);
|
||||||
$component->bootstrap($this);
|
$component->bootstrap($this);
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidConfigException((is_array($class) ? $class['class'] : $class) . " must implement \\yii\\base\\BootstrapInterface");
|
Yii::trace("Bootstrap with " . get_class($component), __METHOD__);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user