Yii2 framework backup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

445 lines
14 KiB

13 years ago
<?php
/**
* Application class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
use yii\base\InvalidCallException;
13 years ago
13 years ago
/**
* Application is the base class for all application classes.
*
* An application serves as the global context that the user request
* is being processed. It manages a set of application components that
* provide specific functionalities to the whole application.
*
* The core application components provided by Application are the following:
* <ul>
* <li>{@link getErrorHandler errorHandler}: handles PHP errors and
* uncaught exceptions. This application component is dynamically loaded when needed.</li>
* <li>{@link getSecurityManager securityManager}: provides security-related
* services, such as hashing, encryption. This application component is dynamically
* loaded when needed.</li>
* <li>{@link getStatePersister statePersister}: provides global state
* persistence method. This application component is dynamically loaded when needed.</li>
* <li>{@link getCache cache}: provides caching feature. This application component is
* disabled by default.</li>
* <li>{@link getMessages messages}: provides the message source for translating
* application messages. This application component is dynamically loaded when needed.</li>
* <li>{@link getCoreMessages coreMessages}: provides the message source for translating
* Yii framework messages. This application component is dynamically loaded when needed.</li>
* </ul>
*
* Application will undergo the following lifecycles when processing a user request:
* <ol>
* <li>load application configuration;</li>
* <li>set up class autoloader and error handling;</li>
* <li>load static application components;</li>
* <li>{@link beforeRequest}: preprocess the user request; `beforeRequest` event raised.</li>
13 years ago
* <li>{@link processRequest}: process the user request;</li>
* <li>{@link afterRequest}: postprocess the user request; `afterRequest` event raised.</li>
13 years ago
* </ol>
*
* Starting from lifecycle 3, if a PHP error or an uncaught exception occurs,
* the application will switch to its error handling logic and jump to step 6 afterwards.
*
* @property string $basePath Returns the root path of the application.
* @property CCache $cache Returns the cache component.
* @property CPhpMessageSource $coreMessages Returns the core message translations.
* @property CDateFormatter $dateFormatter Returns the locale-dependent date formatter.
* @property \yii\db\Connection $db Returns the database connection component.
13 years ago
* @property CErrorHandler $errorHandler Returns the error handler component.
* @property string $extensionPath Returns the root directory that holds all third-party extensions.
* @property string $id Returns the unique identifier for the application.
* @property string $language Returns the language that the user is using and the application should be targeted to.
* @property CLocale $locale Returns the locale instance.
* @property string $localeDataPath Returns the directory that contains the locale data.
* @property CMessageSource $messages Returns the application message translations component.
* @property CNumberFormatter $numberFormatter The locale-dependent number formatter.
* @property CHttpRequest $request Returns the request component.
* @property string $runtimePath Returns the directory that stores runtime files.
* @property CSecurityManager $securityManager Returns the security manager component.
* @property CStatePersister $statePersister Returns the state persister component.
* @property string $timeZone Returns the time zone used by this application.
* @property UrlManager $urlManager Returns the URL manager component.
13 years ago
* @property string $baseUrl Returns the relative URL for the application
* @property string $homeUrl the homepage URL
13 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
13 years ago
*/
13 years ago
class Application extends Module
13 years ago
{
const EVENT_BEFORE_REQUEST = 'beforeRequest';
const EVENT_AFTER_REQUEST = 'afterRequest';
13 years ago
/**
* @var string the application name. Defaults to 'My Application'.
*/
public $name = 'My Application';
/**
13 years ago
* @var string the version of this application. Defaults to '1.0'.
*/
public $version = '1.0';
/**
13 years ago
* @var string the charset currently used for the application. Defaults to 'UTF-8'.
*/
public $charset = 'UTF-8';
/**
* @var string the language that the application is written in. This mainly refers to
* the language that the messages and view files are in. Defaults to 'en_us' (US English).
13 years ago
* @see language
13 years ago
*/
public $sourceLanguage = 'en_us';
13 years ago
/**
* @var array IDs of application components that need to be loaded when the application starts.
* The default value is `array('errorHandler')`, which loads the [[errorHandler]] component
* to ensure errors and exceptions can be handled nicely.
*/
13 years ago
public $preload = array('errorHandler');
/**
* @var Controller the currently active controller instance
*/
public $controller;
13 years ago
/**
* @var mixed the layout that should be applied for views in this application. Defaults to 'main'.
* If this is false, layout will be disabled.
*/
public $layout = 'main';
13 years ago
// todo
13 years ago
public $localeDataPath = '@yii/i18n/data';
13 years ago
private $_runtimePath;
private $_ended = false;
private $_language;
/**
* Constructor.
13 years ago
* @param string $id the ID of this application. The ID should uniquely identify the application from others.
13 years ago
* @param string $basePath the base path of this application. This should point to
* the directory containing all application logic, template and data.
* @param array $config name-value pairs that will be used to initialize the object properties
13 years ago
*/
public function __construct($id, $basePath, $config = array())
13 years ago
{
13 years ago
\Yii::$application = $this;
13 years ago
$this->id = $id;
13 years ago
$this->setBasePath($basePath);
13 years ago
$this->registerDefaultAliases();
13 years ago
$this->registerCoreComponents();
parent::__construct($id, $this, $config);
13 years ago
}
13 years ago
13 years ago
/**
* Initializes the application by loading components declared in [[preload]].
* If you override this method, make sure the parent implementation is invoked.
13 years ago
*/
public function init()
{
13 years ago
$this->preloadComponents();
}
/**
* Terminates the application.
13 years ago
* This method replaces PHP's exit() function by calling [[afterRequest()]] before exiting.
13 years ago
* @param integer $status exit status (value 0 means normal exit while other values mean abnormal exit).
13 years ago
* @param boolean $exit whether to exit the current request.
13 years ago
* It defaults to true, meaning the PHP's exit() function will be called at the end of this method.
*/
public function end($status = 0, $exit = true)
{
13 years ago
if (!$this->_ended) {
$this->_ended = true;
$this->afterRequest();
13 years ago
}
if ($exit) {
13 years ago
exit($status);
13 years ago
}
13 years ago
}
/**
13 years ago
* Runs the application.
* This is the main entrance of an application.
* @return integer the exit status (0 means normal, non-zero values mean abnormal)
*/
public function run()
{
$this->beforeRequest();
$status = $this->processRequest();
$this->afterRequest();
return $status;
}
/**
13 years ago
* Raises the [[beforeRequest]] event right BEFORE the application processes the request.
13 years ago
*/
13 years ago
public function beforeRequest()
13 years ago
{
$this->trigger(self::EVENT_BEFORE_REQUEST);
13 years ago
}
/**
* Raises the [[afterRequest]] event right AFTER the application processes the request.
13 years ago
*/
public function afterRequest()
13 years ago
{
$this->trigger(self::EVENT_AFTER_REQUEST);
13 years ago
}
/**
* Processes the request.
13 years ago
* Child classes should override this method with actual request processing logic.
* @return integer the exit status of the controller action (0 means normal, non-zero values mean abnormal)
13 years ago
*/
public function processRequest()
{
return 0;
}
/**
* Runs a controller with the given route and parameters.
* @param string $route the route (e.g. `post/create`)
* @param array $params the parameters to be passed to the controller action
* @return integer the exit status (0 means normal, non-zero values mean abnormal)
* @throws BadRequestException if the route cannot be resolved into a controller
13 years ago
*/
13 years ago
public function runController($route, $params = array())
13 years ago
{
13 years ago
$result = $this->createController($route);
if ($result === false) {
throw new BadRequestException(\Yii::t('yii', 'Unable to resolve the request.'));
}
/** @var $controller Controller */
list($controller, $action) = $result;
13 years ago
$priorController = $this->controller;
$this->controller = $controller;
$status = $controller->run($action, $params);
13 years ago
$this->controller = $priorController;
return $status;
13 years ago
}
/**
* Returns the directory that stores runtime files.
* @return string the directory that stores runtime files. Defaults to 'protected/runtime'.
*/
public function getRuntimePath()
{
13 years ago
if ($this->_runtimePath !== null) {
13 years ago
$this->setRuntimePath($this->getBasePath() . DIRECTORY_SEPARATOR . 'runtime');
}
return $this->_runtimePath;
13 years ago
}
/**
* Sets the directory that stores runtime files.
* @param string $path the directory that stores runtime files.
* @throws InvalidCallException if the directory does not exist or is not writable
13 years ago
*/
public function setRuntimePath($path)
{
13 years ago
$p = \Yii::getAlias($path);
if ($p === false || !is_dir($p) || !is_writable($path)) {
throw new InvalidCallException("Application runtime path \"$path\" is invalid. Please make sure it is a directory writable by the Web server process.");
13 years ago
} else {
$this->_runtimePath = $p;
13 years ago
}
13 years ago
}
/**
13 years ago
* Returns the language that the end user is using.
* @return string the language that the user is using (e.g. 'en_US', 'zh_CN').
* Defaults to the value of [[sourceLanguage]].
13 years ago
*/
public function getLanguage()
{
return $this->_language === null ? $this->sourceLanguage : $this->_language;
}
/**
13 years ago
* Specifies which language the end user is using.
* This is the language that the application should use to display to end users.
* By default, [[language]] and [[sourceLanguage]] are the same.
* Do not set this property unless your application needs to support multiple languages.
13 years ago
* @param string $language the user language (e.g. 'en_US', 'zh_CN').
* If it is null, the [[sourceLanguage]] will be used.
13 years ago
*/
public function setLanguage($language)
{
$this->_language = $language;
}
/**
* Returns the time zone used by this application.
* This is a simple wrapper of PHP function date_default_timezone_get().
* @return string the time zone used by this application.
* @see http://php.net/manual/en/function.date-default-timezone-get.php
*/
public function getTimeZone()
{
return date_default_timezone_get();
}
/**
* Sets the time zone used by this application.
* This is a simple wrapper of PHP function date_default_timezone_set().
* @param string $value the time zone used by this application.
* @see http://php.net/manual/en/function.date-default-timezone-set.php
*/
public function setTimeZone($value)
{
date_default_timezone_set($value);
}
/**
* Returns the locale instance.
* @param string $localeID the locale ID (e.g. en_US). If null, the {@link getLanguage application language ID} will be used.
* @return CLocale the locale instance
*/
public function getLocale($localeID = null)
{
return CLocale::getInstance($localeID === null ? $this->getLanguage() : $localeID);
}
/**
* @return CNumberFormatter the locale-dependent number formatter.
* The current {@link getLocale application locale} will be used.
*/
public function getNumberFormatter()
{
return $this->getLocale()->getNumberFormatter();
}
/**
* Returns the locale-dependent date formatter.
* @return CDateFormatter the locale-dependent date formatter.
* The current {@link getLocale application locale} will be used.
*/
public function getDateFormatter()
{
return $this->getLocale()->getDateFormatter();
}
/**
* Returns the database connection component.
* @return \yii\db\Connection the database connection
13 years ago
*/
public function getDb()
{
return $this->getComponent('db');
}
/**
* Returns the error handler component.
13 years ago
* @return ErrorHandler the error handler application component.
13 years ago
*/
public function getErrorHandler()
{
return $this->getComponent('errorHandler');
}
/**
13 years ago
* Returns the application theme.
* @return Theme the theme that this application is currently using.
*/
public function getTheme()
{
return $this->getComponent('theme');
}
/**
13 years ago
* Returns the security manager component.
13 years ago
* @return SecurityManager the security manager application component.
13 years ago
*/
public function getSecurityManager()
{
return $this->getComponent('securityManager');
}
/**
* Returns the cache component.
13 years ago
* @return \yii\caching\Cache the cache application component. Null if the component is not enabled.
13 years ago
*/
public function getCache()
{
return $this->getComponent('cache');
}
/**
* Returns the core message translations component.
13 years ago
* @return \yii\i18n\MessageSource the core message translations
13 years ago
*/
public function getCoreMessages()
{
return $this->getComponent('coreMessages');
}
/**
* Returns the application message translations component.
13 years ago
* @return \yii\i18n\MessageSource the application message translations
13 years ago
*/
public function getMessages()
{
return $this->getComponent('messages');
}
/**
* Returns the request component.
13 years ago
* @return Request the request component
13 years ago
*/
public function getRequest()
{
return $this->getComponent('request');
}
/**
13 years ago
* Sets default path aliases.
*/
public function registerDefaultAliases()
{
\Yii::$aliases['@application'] = $this->getBasePath();
\Yii::$aliases['@entry'] = dirname($_SERVER['SCRIPT_FILENAME']);
\Yii::$aliases['@www'] = '';
}
/**
13 years ago
* Registers the core application components.
* @see setComponents
*/
13 years ago
public function registerCoreComponents()
13 years ago
{
13 years ago
$this->setComponents(array(
'errorHandler' => array(
'class' => 'yii\base\ErrorHandler',
),
'request' => array(
'class' => 'yii\base\Request',
),
'response' => array(
13 years ago
'class' => 'yii\base\Response',
13 years ago
),
'format' => array(
'class' => 'yii\base\Formatter',
),
13 years ago
'coreMessages' => array(
13 years ago
'class' => 'yii\i18n\PhpMessageSource',
13 years ago
'language' => 'en_us',
13 years ago
'basePath' => '@yii/messages',
13 years ago
),
'messages' => array(
13 years ago
'class' => 'yii\i18n\PhpMessageSource',
13 years ago
),
'securityManager' => array(
13 years ago
'class' => 'yii\base\SecurityManager',
13 years ago
),
'urlManager' => array(
'class' => 'yii\web\UrlManager',
),
13 years ago
));
13 years ago
}
}