|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657 |
- <?php
-
-
- namespace yii\base;
-
- use Yii;
-
-
- abstract class Application extends Module
- {
-
-
- const EVENT_BEFORE_REQUEST = 'beforeRequest';
-
-
- const EVENT_AFTER_REQUEST = 'afterRequest';
-
-
- const STATE_BEGIN = 0;
-
-
- const STATE_INIT = 1;
-
-
- const STATE_BEFORE_REQUEST = 2;
-
-
- const STATE_HANDLING_REQUEST = 3;
-
-
- const STATE_AFTER_REQUEST = 4;
-
-
- const STATE_SENDING_RESPONSE = 5;
-
-
- const STATE_END = 6;
-
-
-
- public $controllerNamespace = 'app\\controllers';
-
-
- public $name = 'My Application';
-
-
- public $version = '1.0';
-
-
- public $charset = 'UTF-8';
-
-
- public $language = 'en-US';
-
-
- public $sourceLanguage = 'en-US';
-
-
- public $controller;
-
-
- public $layout = 'main';
-
-
- public $requestedRoute;
-
-
- public $requestedAction;
-
-
- public $requestedParams;
-
-
- public $extensions;
-
-
- public $bootstrap = [];
-
-
- public $state;
-
-
- public $loadedModules = [];
-
-
-
-
- public function __construct($config = [])
- {
- Yii::$app = $this;
- static::setInstance($this);
-
- $this->state = self::STATE_BEGIN;
-
- $this->preInit($config);
-
- $this->registerErrorHandler($config);
-
- Component::__construct($config);
- }
-
-
-
- public function preInit(&$config)
- {
- if (!isset($config['id'])) {
- throw new InvalidConfigException('The "id" configuration for the Application is required.');
- }
- if (isset($config['basePath'])) {
- $this->setBasePath($config['basePath']);
- unset($config['basePath']);
- } else {
- throw new InvalidConfigException('The "basePath" configuration for the Application is required.');
- }
-
- if (isset($config['vendorPath'])) {
- $this->setVendorPath($config['vendorPath']);
- unset($config['vendorPath']);
- } else {
-
- $this->getVendorPath();
- }
- if (isset($config['runtimePath'])) {
- $this->setRuntimePath($config['runtimePath']);
- unset($config['runtimePath']);
- } else {
-
- $this->getRuntimePath();
- }
-
- if (isset($config['timeZone'])) {
- $this->setTimeZone($config['timeZone']);
- unset($config['timeZone']);
- } elseif (!ini_get('date.timezone')) {
- $this->setTimeZone('UTC');
- }
-
-
- foreach ($this->coreComponents() as $id => $component) {
- if (!isset($config['components'][$id])) {
- $config['components'][$id] = $component;
- } elseif (is_array($config['components'][$id]) && !isset($config['components'][$id]['class'])) {
- $config['components'][$id]['class'] = $component['class'];
- }
- }
- }
-
-
-
- public function init()
- {
- $this->state = self::STATE_INIT;
- $this->bootstrap();
- }
-
-
-
- protected function bootstrap()
- {
- if ($this->extensions === null) {
- $file = Yii::getAlias('@vendor/yiisoft/extensions.php');
- $this->extensions = is_file($file) ? include($file) : [];
- }
- foreach ($this->extensions as $extension) {
- if (!empty($extension['alias'])) {
- foreach ($extension['alias'] as $name => $path) {
- Yii::setAlias($name, $path);
- }
- }
- if (isset($extension['bootstrap'])) {
- $component = Yii::createObject($extension['bootstrap']);
- if ($component instanceof BootstrapInterface) {
- Yii::trace('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__);
- $component->bootstrap($this);
- } else {
- Yii::trace('Bootstrap with ' . get_class($component), __METHOD__);
- }
- }
- }
-
- foreach ($this->bootstrap as $class) {
- $component = null;
- if (is_string($class)) {
- if ($this->has($class)) {
- $component = $this->get($class);
- } elseif ($this->hasModule($class)) {
- $component = $this->getModule($class);
- } elseif (strpos($class, '\\') === false) {
- throw new InvalidConfigException("Unknown bootstrapping component ID: $class");
- }
- }
- if (!isset($component)) {
- $component = Yii::createObject($class);
- }
-
- if ($component instanceof BootstrapInterface) {
- Yii::trace('Bootstrap with ' . get_class($component) . '::bootstrap()', __METHOD__);
- $component->bootstrap($this);
- } else {
- Yii::trace('Bootstrap with ' . get_class($component), __METHOD__);
- }
- }
- }
-
-
-
- protected function registerErrorHandler(&$config)
- {
- if (YII_ENABLE_ERROR_HANDLER) {
- if (!isset($config['components']['errorHandler']['class'])) {
- echo "Error: no errorHandler component is configured.\n";
- exit(1);
- }
- $this->set('errorHandler', $config['components']['errorHandler']);
- unset($config['components']['errorHandler']);
- $this->getErrorHandler()->register();
- }
- }
-
-
-
- public function getUniqueId()
- {
- return '';
- }
-
-
-
- public function setBasePath($path)
- {
- parent::setBasePath($path);
- Yii::setAlias('@app', $this->getBasePath());
- }
-
-
-
- public function run()
- {
- try {
-
- $this->state = self::STATE_BEFORE_REQUEST;
- $this->trigger(self::EVENT_BEFORE_REQUEST);
-
- $this->state = self::STATE_HANDLING_REQUEST;
- $response = $this->handleRequest($this->getRequest());
-
- $this->state = self::STATE_AFTER_REQUEST;
- $this->trigger(self::EVENT_AFTER_REQUEST);
-
- $this->state = self::STATE_SENDING_RESPONSE;
- $response->send();
-
- $this->state = self::STATE_END;
-
- return $response->exitStatus;
-
- } catch (ExitException $e) {
-
- $this->end($e->statusCode, isset($response) ? $response : null);
- return $e->statusCode;
-
- }
- }
-
-
-
- abstract public function handleRequest($request);
-
- private $_runtimePath;
-
-
-
- public function getRuntimePath()
- {
- if ($this->_runtimePath === null) {
- $this->setRuntimePath($this->getBasePath() . DIRECTORY_SEPARATOR . 'runtime');
- }
-
- return $this->_runtimePath;
- }
-
-
-
- public function setRuntimePath($path)
- {
- $this->_runtimePath = Yii::getAlias($path);
- Yii::setAlias('@runtime', $this->_runtimePath);
- }
-
- private $_vendorPath;
-
-
-
- public function getVendorPath()
- {
- if ($this->_vendorPath === null) {
- $this->setVendorPath($this->getBasePath() . DIRECTORY_SEPARATOR . 'vendor');
- }
-
- return $this->_vendorPath;
- }
-
-
-
- public function setVendorPath($path)
- {
- $this->_vendorPath = Yii::getAlias($path);
- Yii::setAlias('@vendor', $this->_vendorPath);
- Yii::setAlias('@bower', $this->_vendorPath . DIRECTORY_SEPARATOR . 'bower');
- Yii::setAlias('@npm', $this->_vendorPath . DIRECTORY_SEPARATOR . 'npm');
- }
-
-
-
- public function getTimeZone()
- {
- return date_default_timezone_get();
- }
-
-
-
- public function setTimeZone($value)
- {
- date_default_timezone_set($value);
- }
-
-
-
- public function getDb()
- {
- return $this->get('db');
- }
-
-
-
- public function getLog()
- {
- return $this->get('log');
- }
-
-
-
- public function getErrorHandler()
- {
- return $this->get('errorHandler');
- }
-
-
-
- public function getCache()
- {
- return $this->get('cache', false);
- }
-
-
-
- public function getFormatter()
- {
- return $this->get('formatter');
- }
-
-
-
- public function getRequest()
- {
- return $this->get('request');
- }
-
-
-
- public function getResponse()
- {
- return $this->get('response');
- }
-
-
-
- public function getView()
- {
- return $this->get('view');
- }
-
-
-
- public function getUrlManager()
- {
- return $this->get('urlManager');
- }
-
-
-
- public function getI18n()
- {
- return $this->get('i18n');
- }
-
-
-
- public function getMailer()
- {
- return $this->get('mailer');
- }
-
-
-
- public function getAuthManager()
- {
- return $this->get('authManager', false);
- }
-
-
-
- public function getAssetManager()
- {
- return $this->get('assetManager');
- }
-
-
-
- public function getSecurity()
- {
- return $this->get('security');
- }
-
-
-
- public function coreComponents()
- {
- return [
- 'log' => ['class' => 'yii\log\Dispatcher'],
- 'view' => ['class' => 'yii\web\View'],
- 'formatter' => ['class' => 'yii\i18n\Formatter'],
- 'i18n' => ['class' => 'yii\i18n\I18N'],
- 'mailer' => ['class' => 'yii\swiftmailer\Mailer'],
- 'urlManager' => ['class' => 'yii\web\UrlManager'],
- 'assetManager' => ['class' => 'yii\web\AssetManager'],
- 'security' => ['class' => 'yii\base\Security'],
- ];
- }
-
-
-
- public function end($status = 0, $response = null)
- {
- if ($this->state === self::STATE_BEFORE_REQUEST || $this->state === self::STATE_HANDLING_REQUEST) {
- $this->state = self::STATE_AFTER_REQUEST;
- $this->trigger(self::EVENT_AFTER_REQUEST);
- }
-
- if ($this->state !== self::STATE_SENDING_RESPONSE && $this->state !== self::STATE_END) {
- $this->state = self::STATE_END;
- $response = $response ? : $this->getResponse();
- $response->send();
- }
-
- if (YII_ENV_TEST) {
- throw new ExitException($status);
- } else {
- exit($status);
- }
- }
- }
|