Commit 4c2fcd76 by Qiang Xue

refactored application.

parent 55e8db9b
......@@ -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
*/
......
......@@ -170,7 +170,6 @@ abstract class Module extends Component
*/
public function init()
{
Yii::setAlias('@' . $this->id, $this->getBasePath());
$this->preloadComponents();
}
......
......@@ -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);
}
......
......@@ -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;
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment