From 4c2fcd76082dfc3511efdce1898c846460403099 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Wed, 10 Apr 2013 10:48:27 -0400 Subject: [PATCH] refactored application. --- framework/base/Application.php | 30 ++++++++++-------------------- framework/base/Module.php | 1 - framework/web/Application.php | 14 ++++---------- framework/web/Request.php | 33 ++++++++++++++++++++++++++++----- 4 files changed, 42 insertions(+), 36 deletions(-) diff --git a/framework/base/Application.php b/framework/base/Application.php index 01e6df8..6b0dfa3 100644 --- a/framework/base/Application.php +++ b/framework/base/Application.php @@ -113,29 +113,27 @@ class Application extends Module if (isset($config['basePath'])) { $this->setBasePath($config['basePath']); unset($config['basePath']); + Yii::$aliases['@app'] = $this->getBasePath(); } else { throw new InvalidConfigException('The "basePath" configuration is required.'); } - if (YII_ENABLE_ERROR_HANDLER) { - ini_set('display_errors', 0); - set_exception_handler(array($this, 'handleException')); - set_error_handler(array($this, 'handleError'), error_reporting()); - } - - $this->registerDefaultAliases(); + $this->registerErrorHandlers(); $this->registerCoreComponents(); - Component::__construct($config); + parent::__construct($config); } /** - * Initializes the application by loading components declared in [[preload]]. - * If you override this method, make sure the parent implementation is invoked. + * Registers error handlers. */ - public function init() + public function registerErrorHandlers() { - $this->preloadComponents(); + if (YII_ENABLE_ERROR_HANDLER) { + ini_set('display_errors', 0); + set_exception_handler(array($this, 'handleException')); + set_error_handler(array($this, 'handleError'), error_reporting()); + } } /** @@ -339,14 +337,6 @@ class Application extends Module } /** - * Sets default path aliases. - */ - public function registerDefaultAliases() - { - Yii::$aliases['@app'] = $this->getBasePath(); - } - - /** * Registers the core application components. * @see setComponents */ diff --git a/framework/base/Module.php b/framework/base/Module.php index 296494d..e2fc1b5 100644 --- a/framework/base/Module.php +++ b/framework/base/Module.php @@ -170,7 +170,6 @@ abstract class Module extends Component */ public function init() { - Yii::setAlias('@' . $this->id, $this->getBasePath()); $this->preloadComponents(); } diff --git a/framework/web/Application.php b/framework/web/Application.php index b839d92..f9b615d 100644 --- a/framework/web/Application.php +++ b/framework/web/Application.php @@ -23,21 +23,15 @@ class Application extends \yii\base\Application public $defaultRoute = 'site'; /** - * Sets default path aliases. - */ - public function registerDefaultAliases() - { - parent::registerDefaultAliases(); - Yii::$aliases['@webroot'] = dirname($_SERVER['SCRIPT_FILENAME']); - } - - /** * Processes the request. * @return integer the exit status of the controller action (0 means normal, non-zero values mean abnormal) */ public function processRequest() { - list ($route, $params) = $this->getRequest()->resolve(); + $request = $this->getRequest(); + Yii::setAlias('@wwwroot', dirname($request->getScriptFile())); + Yii::setAlias('@www', $request->getBaseUrl()); + list ($route, $params) = $request->resolve(); return $this->runAction($route, $params); } diff --git a/framework/web/Request.php b/framework/web/Request.php index 093a394..369fa0c 100644 --- a/framework/web/Request.php +++ b/framework/web/Request.php @@ -43,8 +43,6 @@ class Request extends \yii\base\Request */ public function resolve() { - Yii::setAlias('@www', $this->getBaseUrl()); - $result = Yii::$app->getUrlManager()->parseRequest($this); if ($result !== false) { list ($route, $params) = $result; @@ -301,7 +299,8 @@ class Request extends \yii\base\Request public function getScriptUrl() { if ($this->_scriptUrl === null) { - $scriptName = basename($_SERVER['SCRIPT_FILENAME']); + $scriptFile = $this->getScriptFile(); + $scriptName = basename($scriptFile); if (basename($_SERVER['SCRIPT_NAME']) === $scriptName) { $this->_scriptUrl = $_SERVER['SCRIPT_NAME']; } elseif (basename($_SERVER['PHP_SELF']) === $scriptName) { @@ -310,8 +309,8 @@ class Request extends \yii\base\Request $this->_scriptUrl = $_SERVER['ORIG_SCRIPT_NAME']; } elseif (($pos = strpos($_SERVER['PHP_SELF'], '/' . $scriptName)) !== false) { $this->_scriptUrl = substr($_SERVER['SCRIPT_NAME'], 0, $pos) . '/' . $scriptName; - } elseif (isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'], $_SERVER['DOCUMENT_ROOT']) === 0) { - $this->_scriptUrl = str_replace('\\', '/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $_SERVER['SCRIPT_FILENAME'])); + } elseif (isset($_SERVER['DOCUMENT_ROOT']) && strpos($scriptFile, $_SERVER['DOCUMENT_ROOT']) === 0) { + $this->_scriptUrl = str_replace('\\', '/', str_replace($_SERVER['DOCUMENT_ROOT'], '', $scriptFile)); } else { throw new InvalidConfigException('Unable to determine the entry script URL.'); } @@ -330,6 +329,30 @@ class Request extends \yii\base\Request $this->_scriptUrl = '/' . trim($value, '/'); } + private $_scriptFile; + + /** + * Returns the entry script file path. + * The default implementation will simply return `$_SERVER['SCRIPT_FILENAME']`. + * @return string the entry script file path + */ + public function getScriptFile() + { + return isset($this->_scriptFile) ? $this->_scriptFile : $_SERVER['SCRIPT_FILENAME']; + } + + /** + * Sets the entry script file path. + * The entry script file path normally can be obtained from `$_SERVER['SCRIPT_FILENAME']`. + * If your server configuration does not return the correct value, you may configure + * this property to make it right. + * @param string $value the entry script file path. + */ + public function setScriptFile($value) + { + $this->_scriptFile = $value; + } + private $_pathInfo; /**