Added extension initialization to application life cycle.

This commit is contained in:
Qiang Xue
2013-10-21 23:46:05 -04:00
parent 1360f84d3d
commit c28a005fb8
2 changed files with 54 additions and 15 deletions

View File

@ -144,6 +144,24 @@ abstract class Application extends Module
public function __construct($config = []) public function __construct($config = [])
{ {
Yii::$app = $this; Yii::$app = $this;
$this->preInit($config);
$this->registerErrorHandlers();
$this->registerCoreComponents();
Component::__construct($config);
}
/**
* Pre-initializes the application.
* This method is called at the beginning of the application constructor.
* It initializes several important application properties.
* If you override this method, please make sure you call the parent implementation.
* @param array $config the application configuration
* @throws InvalidConfigException if either [[id]] or [[basePath]] configuration is missing.
*/
public function preInit(&$config)
{
if (!isset($config['id'])) { if (!isset($config['id'])) {
throw new InvalidConfigException('The "id" configuration is required.'); throw new InvalidConfigException('The "id" configuration is required.');
} }
@ -154,21 +172,6 @@ abstract class Application extends Module
throw new InvalidConfigException('The "basePath" configuration is required.'); throw new InvalidConfigException('The "basePath" configuration is required.');
} }
$this->preInit($config);
$this->registerErrorHandlers();
$this->registerCoreComponents();
Component::__construct($config);
}
/**
* Pre-initializes the application.
* This method is called at the beginning of the application constructor.
* @param array $config the application configuration
*/
public function preInit(&$config)
{
if (isset($config['vendorPath'])) { if (isset($config['vendorPath'])) {
$this->setVendorPath($config['vendorPath']); $this->setVendorPath($config['vendorPath']);
unset($config['vendorPath']); unset($config['vendorPath']);
@ -183,6 +186,7 @@ abstract class Application extends Module
// set "@runtime" // set "@runtime"
$this->getRuntimePath(); $this->getRuntimePath();
} }
if (isset($config['timeZone'])) { if (isset($config['timeZone'])) {
$this->setTimeZone($config['timeZone']); $this->setTimeZone($config['timeZone']);
unset($config['timeZone']); unset($config['timeZone']);
@ -191,6 +195,31 @@ abstract class Application extends Module
} }
} }
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->initExtensions($this->extensions);
}
/**
* Initializes the extensions.
* @param array $extensions the extensions to be initialized. Please refer to [[extensions]]
* for the structure of the extension array.
*/
protected function initExtensions($extensions)
{
foreach ($extensions as $extension) {
if (isset($extension['bootstrap'])) {
/** @var Extension $class */
$class = $extension['bootstrap'];
$class::init();
}
}
}
/** /**
* Loads components that are declared in [[preload]]. * Loads components that are declared in [[preload]].
* @throws InvalidConfigException if a component or module to be preloaded is unknown * @throws InvalidConfigException if a component or module to be preloaded is unknown

View File

@ -8,11 +8,21 @@
namespace yii\base; 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)
* will be invoked during the application initialization stage.
*
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class Extension class Extension
{ {
/**
* Initializes the extension.
* This method is invoked at the end of [[Application::init()]].
*/
public static function init() public static function init()
{ {
} }