Fixes #2775: Added yii\base\Application::bootstrap to support running bootstrap classes when starting an application

This commit is contained in:
Qiang Xue
2014-03-17 11:57:44 -04:00
parent 6b8107da85
commit d2c84606a9
5 changed files with 18 additions and 5 deletions

View File

@ -132,6 +132,11 @@ abstract class Application extends Module
* ~~~
*/
public $extensions = [];
/**
* @var array list of bootstrap classes. A bootstrap class must have a public static method named
* `bootstrap()`. The method will be called during [[init()]] for every bootstrap class.
*/
public $bootstrap = [];
/**
* @var \Exception the exception that is being handled currently. When this is not null,
* it means the application is handling some exception and extra care should be taken.
@ -209,6 +214,10 @@ abstract class Application extends Module
public function init()
{
$this->initExtensions($this->extensions);
foreach ($this->bootstrap as $class) {
/** @var Extension $class */
$class::bootstrap();
}
parent::init();
}
@ -228,7 +237,7 @@ abstract class Application extends Module
if (isset($extension['bootstrap'])) {
/** @var Extension $class */
$class = $extension['bootstrap'];
$class::init();
$class::bootstrap();
}
}
}

View File

@ -11,7 +11,7 @@ namespace yii\base;
* Extension is the base class that may be extended by individual extensions.
*
* Extension serves as the bootstrap class for extensions. When an extension
* is installed via composer, the [[init()]] method of its Extension class (if any)
* is installed via composer, the [[bootstrap()]] method of its Extension class (if any)
* will be invoked during the application initialization stage.
*
* @author Qiang Xue <qiang.xue@gmail.com>
@ -21,9 +21,9 @@ class Extension
{
/**
* Initializes the extension.
* This method is invoked at the end of [[Application::init()]].
* This method is invoked at the beginning of [[Application::init()]].
*/
public static function init()
public static function bootstrap()
{
}
}