diff --git a/framework/base/Request.php b/framework/base/Request.php index 4f3b8b3..dacc468 100644 --- a/framework/base/Request.php +++ b/framework/base/Request.php @@ -16,135 +16,47 @@ namespace yii\base; class Request extends ApplicationComponent { private $_scriptFile; + private $_isConsoleRequest; /** - * Initializes the application component. - * This method overrides the parent implementation by preprocessing - * the user request data. + * Returns a value indicating whether the current request is made via command line + * @return boolean the value indicating whether the current request is made via console */ - public function init() + public function getIsConsoleRequest() { + return isset($this->_isConsoleRequest) ? $this->_isConsoleRequest : PHP_SAPI === 'cli'; } /** - * Returns the relative URL of the entry script. - * The implementation of this method referenced Zend_Controller_Request_Http in Zend Framework. - * @return string the relative URL of the entry script. + * Sets the value indicating whether the current request is made via command line + * @param boolean $value the value indicating whether the current request is made via command line */ - public function getScriptUrl() + public function setIsConsoleRequest($value) { - if($this->_scriptUrl===null) - { - $scriptName=basename($_SERVER['SCRIPT_FILENAME']); - if(basename($_SERVER['SCRIPT_NAME'])===$scriptName) - $this->_scriptUrl=$_SERVER['SCRIPT_NAME']; - else if(basename($_SERVER['PHP_SELF'])===$scriptName) - $this->_scriptUrl=$_SERVER['PHP_SELF']; - else if(isset($_SERVER['ORIG_SCRIPT_NAME']) && basename($_SERVER['ORIG_SCRIPT_NAME'])===$scriptName) - $this->_scriptUrl=$_SERVER['ORIG_SCRIPT_NAME']; - else if(($pos=strpos($_SERVER['PHP_SELF'],'/'.$scriptName))!==false) - $this->_scriptUrl=substr($_SERVER['SCRIPT_NAME'],0,$pos).'/'.$scriptName; - else if(isset($_SERVER['DOCUMENT_ROOT']) && strpos($_SERVER['SCRIPT_FILENAME'],$_SERVER['DOCUMENT_ROOT'])===0) - $this->_scriptUrl=str_replace('\\','/',str_replace($_SERVER['DOCUMENT_ROOT'],'',$_SERVER['SCRIPT_FILENAME'])); - else - throw new Exception(Yii::t('yii','CHttpRequest is unable to determine the entry script URL.')); - } - return $this->_scriptUrl; - } - - /** - * Sets the relative URL for the application entry script. - * This setter is provided in case the entry script URL cannot be determined - * on certain Web servers. - * @param string $value the relative URL for the application entry script. - */ - public function setScriptUrl($value) - { - $this->_scriptUrl='/'.trim($value,'/'); - } - - /** - * Returns whether this is an AJAX (XMLHttpRequest) request. - * @return boolean whether this is an AJAX (XMLHttpRequest) request. - */ - public function getIsAjaxRequest() - { - return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest'; - } - - /** - * Returns whether this is an Adobe Flash or Adobe Flex request. - * @return boolean whether this is an Adobe Flash or Adobe Flex request. - * @since 1.1.11 - */ - public function getIsFlashRequest() - { - return isset($_SERVER['HTTP_USER_AGENT']) && (stripos($_SERVER['HTTP_USER_AGENT'],'Shockwave')!==false || stripos($_SERVER['HTTP_USER_AGENT'],'Flash')!==false); - } - - /** - * Returns the server name. - * @return string server name - */ - public function getServerName() - { - return $_SERVER['SERVER_NAME']; - } - - /** - * Returns the server port number. - * @return integer server port number - */ - public function getServerPort() - { - return $_SERVER['SERVER_PORT']; - } - - /** - * Returns the URL referrer, null if not present - * @return string URL referrer, null if not present - */ - public function getUrlReferrer() - { - return isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:null; - } - - /** - * Returns the user agent, null if not present. - * @return string user agent, null if not present - */ - public function getUserAgent() - { - return isset($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:null; + $this->_isConsoleRequest = $value; } /** - * Returns the user IP address. - * @return string user IP address - */ - public function getUserHostAddress() - { - return isset($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:'127.0.0.1'; - } - - /** - * Returns the user host name, null if it cannot be determined. - * @return string user host name, null if cannot be determined + * Returns entry script file path. + * @return string entry script file path (processed w/ realpath()) */ - public function getUserHost() + public function getScriptFile() { - return isset($_SERVER['REMOTE_HOST'])?$_SERVER['REMOTE_HOST']:null; + if ($this->_scriptFile === null) { + $this->_scriptFile = realpath($_SERVER['SCRIPT_FILENAME']); + } + return $this->_scriptFile; } /** - * Returns entry script file path. - * @return string entry script file path (processed w/ realpath()) + * Sets the entry script file path. + * This can be an absolute or relative file path, or a path alias. + * Note that you normally do not have to set the script file path + * as [[getScriptFile()]] can determine it based on `$_SERVER['SCRIPT_FILENAME']`. + * @param string $value the entry script file */ - public function getScriptFile() + public function setScriptFile($value) { - if($this->_scriptFile!==null) - return $this->_scriptFile; - else - return $this->_scriptFile=realpath($_SERVER['SCRIPT_FILENAME']); + $this->_scriptFile = realpath(\Yii::getAlias($value)); } } diff --git a/framework/console/Application.php b/framework/console/Application.php index 1bac8c5..1098ab1 100644 --- a/framework/console/Application.php +++ b/framework/console/Application.php @@ -25,21 +25,21 @@ use yii\util\ReflectionHelper; * - The command processes the user request with the specified parameters. * * The command classes reside in the directory specified by [[controllerPath]]. - * Their naming should follow the same naming as controllers. For example, the `help` command + * Their naming should follow the same naming convention as controllers. For example, the `help` command * is implemented using the `HelpController` class. * * To run the console application, enter the following on the command line: * * ~~~ - * yiic [...options...] + * yiic [--param1=value1 --param2 ...] * ~~~ * * where `` refers to a controller route in the form of `ModuleID/ControllerID/ActionID` - * (e.g. `sitemap/create`), and `options` refers to a set of named parameters that will be used - * to initialize the command controller instance and the corresponding action (e.g. `--since=0` - * specifies a `since` parameter whose value is 0). + * (e.g. `sitemap/create`), and `param1`, `param2` refers to a set of named parameters that + * will be used to initialize the controller action (e.g. `--since=0` specifies a `since` parameter + * whose value is 0 and a corresponding `$since` parameter is passed to the action method). * - * A `help` command is provided by default, which may list available commands and show their usage. + * A `help` command is provided by default, which lists available commands and shows their usage. * To use this command, simply type: * * ~~~ @@ -89,11 +89,13 @@ class Application extends \yii\base\Application */ public function processRequest() { - if (!isset($_SERVER['argv'])) { + /** @var $request Request */ + $request = $this->getRequest(); + if ($request->getIsConsoleRequest()) { + return $this->runController($request->route, $request->params); + } else { die('This script must be run from the command line.'); } - list($route, $params) = $this->resolveRequest($_SERVER['argv']); - return $this->runController($route, $params); } /** @@ -120,35 +122,6 @@ class Application extends \yii\base\Application } /** - * Resolves the request. - * @param array $args the arguments passed via the command line - * @return array the controller route and the parameters for the controller action - */ - protected function resolveRequest($args) - { - array_shift($args); // the 1st argument is the yiic script name - - if (isset($args[0])) { - $route = $args[0]; - array_shift($args); - } else { - $route = ''; - } - - $params = array(); - foreach ($args as $arg) { - if (preg_match('/^--(\w+)(=(.*))?$/', $arg, $matches)) { - $name = $matches[1]; - $params[$name] = isset($matches[3]) ? $matches[3] : true; - } else { - $params['args'][] = $arg; - } - } - - return array($route, $params); - } - - /** * Returns the configuration of the built-in commands. * @return array the configuration of the built-in commands. */ diff --git a/framework/console/Controller.php b/framework/console/Controller.php index 210820b..472bb49 100644 --- a/framework/console/Controller.php +++ b/framework/console/Controller.php @@ -20,7 +20,7 @@ use yii\base\Exception; * The `yiic` program is used when calling a console command, like the following: * * ~~~ - * yiic [...options...] + * yiic [--param1=value1 --param2 ...] * ~~~ * * @author Qiang Xue @@ -36,13 +36,14 @@ class Controller extends \yii\base\Controller */ public function invalidActionParams($action, $exception) { - echo "Error: " . $exception->getMessage() . "\n"; + echo \Yii::t('yii', 'Error: {message}', array( + '{message}' => $exception->getMessage(), + )); \Yii::$application->end(1); } /** - * This method is invoked when extra parameters are provided to an action when it is executed. - * The default implementation does nothing. + * This method is invoked when extra parameters are provided to an action while it is executed. * @param Action $action the action being executed * @param array $expected the expected action parameters (name => value) * @param array $actual the actual action parameters (name => value) @@ -53,7 +54,7 @@ class Controller extends \yii\base\Controller $keys = array_diff(array_keys($actual), array_keys($expected)); if (!empty($keys)) { - echo "Error: " . \Yii::t('yii', 'Unknown parameters: {params}', array( + echo \Yii::t('yii', 'Error: Unknown parameter(s): {params}', array( '{params}' => implode(', ', $keys), )) . "\n"; \Yii::$application->end(1); diff --git a/framework/console/Request.php b/framework/console/Request.php new file mode 100644 index 0000000..6133414 --- /dev/null +++ b/framework/console/Request.php @@ -0,0 +1,63 @@ + + * @since 2.0 + */ +class Request extends \yii\base\ApplicationComponent +{ + /** + * @var string the controller route specified by this request. If this is an empty string, + * it means the [[Application::defaultRoute|default route]] will be used. + * Note that the value of this property may not be a correct route. The console application + * will determine it is valid or not when it attempts to execute with this route. + */ + public $route; + /** + * @var array + */ + public $params; + + public function init() + { + parent::init(); + $this->resolveRequest(); + } + + public function getRawParams() + { + return isset($_SERVER['argv']) ? $_SERVER['argv'] : array(); + } + + protected function resolveRequest() + { + $rawParams = $this->getRawParams(); + array_shift($rawParams); // the 1st argument is the yiic script name + + if (isset($rawParams[0])) { + $this->route = $rawParams[0]; + array_shift($rawParams); + } else { + $this->route = ''; + } + + $this->params = array(); + foreach ($rawParams as $param) { + if (preg_match('/^--(\w+)(=(.*))?$/', $param, $matches)) { + $name = $matches[1]; + $this->params[$name] = isset($matches[3]) ? $matches[3] : true; + } else { + $this->params['--args'][] = $param; + } + } + } +}