Commit b2662c0c by Qiang Xue

clean up help command.

parent 2d7f048b
......@@ -56,6 +56,15 @@ class Action extends Component
}
/**
* Returns the unique ID of this action among the whole application.
* @return string the unique ID of this action among the whole application.
*/
public function getUniqueId()
{
return $this->controller->getUniqueId() . '/' . $this->id;
}
/**
* Runs this action with the specified parameters.
* This method is mainly invoked by the controller.
* @param array $params the parameters to be bound to the action's run() method.
......
......@@ -250,7 +250,7 @@ class Controller extends Component
*/
public function getRoute()
{
return $this->action !== null ? $this->getUniqueId() . '/' . $this->action->id : $this->getUniqueId();
return $this->action !== null ? $this->action->getUniqueId() : $this->getUniqueId();
}
/**
......
......@@ -9,7 +9,6 @@
namespace yii\console;
use yii\base\Exception;
use yii\base\InvalidRouteException;
/**
......@@ -85,6 +84,7 @@ class Application extends \yii\base\Application
* Processes the request.
* The request is represented in terms of a controller route and action parameters.
* @return integer the exit status of the controller action (0 means normal, non-zero values mean abnormal)
* @throws Exception if the script is not running from the command line
*/
public function processRequest()
{
......@@ -93,7 +93,7 @@ class Application extends \yii\base\Application
if ($request->getIsConsoleRequest()) {
return $this->runAction($request->route, $request->params);
} else {
throw new BadUsageException(\Yii::t('yii', 'this script must be run from the command line.'));
throw new Exception(\Yii::t('yii', 'this script must be run from the command line.'));
}
}
......@@ -105,14 +105,14 @@ class Application extends \yii\base\Application
* @param string $route the route that specifies the action.
* @param array $params the parameters to be passed to the action
* @return integer the status code returned by the action execution. 0 means normal, and other values mean abnormal.
* @throws BadUsageException if the route is invalid
* @throws Exception if the route is invalid
*/
public function runAction($route, $params = array())
{
try {
return parent::runAction($route, $params);
} catch (InvalidRouteException $e) {
throw new BadUsageException(\Yii::t('yii', 'Unknown command "{command}".', array('{command}' => $route)));
throw new Exception(\Yii::t('yii', 'Unknown command "{command}".', array('{command}' => $route)));
}
}
......
......@@ -48,14 +48,11 @@ class Controller extends \yii\base\Controller
public function runAction($id, $params = array())
{
if ($params !== array()) {
$class = new \ReflectionClass($this);
$options = $this->globalOptions();
foreach ($params as $name => $value) {
if ($class->hasProperty($name)) {
$property = $class->getProperty($name);
if ($property->isPublic() && !$property->isStatic() && $property->getDeclaringClass()->getName() === get_class($this)) {
$this->$name = $value;
unset($params[$name]);
}
if (in_array($name, $options, true)) {
$this->$name = $value;
unset($params[$name]);
}
}
}
......@@ -69,16 +66,19 @@ class Controller extends \yii\base\Controller
* @param Action $action the currently requested action
* @param array $missingParams the names of the missing parameters
* @param array $unknownParams the unknown parameters (name=>value)
* @throws BadUsageException if there are missing or unknown parameters
* @throws Exception if there are missing or unknown parameters
*/
public function validateActionParams($action, $missingParams, $unknownParams)
{
unset($missingParams[Request::ANONYMOUS_PARAMS], $unknownParams[Request::ANONYMOUS_PARAMS]);
if (!empty($missingParams)) {
throw new BadUsageException(Yii::t('yii', 'Missing required options: {params}', array(
throw new Exception(Yii::t('yii', 'Missing required options: {params}', array(
'{params}' => implode(', ', $missingParams),
)));
} elseif (!empty($unknownParams)) {
throw new BadUsageException(Yii::t('yii', 'Unknown options: {params}', array(
}
if (!empty($unknownParams)) {
throw new Exception(Yii::t('yii', 'Unknown options: {params}', array(
'{params}' => implode(', ', $unknownParams),
)));
}
......@@ -102,6 +102,13 @@ class Controller extends \yii\base\Controller
}
}
/**
* Returns the names of the global options for this command.
* A global option requires the existence of a global member variable whose
* name is the option name.
* Child classes may override this method to specify possible global options.
* @return array the names of the global options for this command.
*/
public function globalOptions()
{
return array();
......
<?php
/**
* BadUsageException class file.
* Exception class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
......@@ -10,12 +10,12 @@
namespace yii\console;
/**
* BadUsageException represents an exception caused by incorrect usage of the end user.
* Exception represents an exception caused by incorrect usage of a console command.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class BadUsageException extends \yii\base\Exception
class Exception extends \yii\base\Exception
{
/**
* @var boolean whether this exception is caused by end user's mistake (e.g. wrong URL)
......@@ -27,7 +27,7 @@ class BadUsageException extends \yii\base\Exception
*/
public function getName()
{
return \Yii::t('yii', 'Bad Usage');
return \Yii::t('yii', 'Error');
}
}
......@@ -15,6 +15,8 @@ namespace yii\console;
*/
class Request extends \yii\base\Request
{
const ANONYMOUS_PARAMS = 'args';
/**
* @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.
......@@ -50,13 +52,13 @@ class Request extends \yii\base\Request
$this->route = '';
}
$this->params = array();
$this->params = array(self::ANONYMOUS_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;
$this->params[self::ANONYMOUS_PARAMS][] = $param;
}
}
}
......
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