Commit b2662c0c by Qiang Xue

clean up help command.

parent 2d7f048b
...@@ -56,6 +56,15 @@ class Action extends Component ...@@ -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. * Runs this action with the specified parameters.
* This method is mainly invoked by the controller. * This method is mainly invoked by the controller.
* @param array $params the parameters to be bound to the action's run() method. * @param array $params the parameters to be bound to the action's run() method.
......
...@@ -250,7 +250,7 @@ class Controller extends Component ...@@ -250,7 +250,7 @@ class Controller extends Component
*/ */
public function getRoute() 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 @@ ...@@ -9,7 +9,6 @@
namespace yii\console; namespace yii\console;
use yii\base\Exception;
use yii\base\InvalidRouteException; use yii\base\InvalidRouteException;
/** /**
...@@ -85,6 +84,7 @@ class Application extends \yii\base\Application ...@@ -85,6 +84,7 @@ class Application extends \yii\base\Application
* Processes the request. * Processes the request.
* The request is represented in terms of a controller route and action parameters. * 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) * @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() public function processRequest()
{ {
...@@ -93,7 +93,7 @@ class Application extends \yii\base\Application ...@@ -93,7 +93,7 @@ class Application extends \yii\base\Application
if ($request->getIsConsoleRequest()) { if ($request->getIsConsoleRequest()) {
return $this->runAction($request->route, $request->params); return $this->runAction($request->route, $request->params);
} else { } 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 ...@@ -105,14 +105,14 @@ class Application extends \yii\base\Application
* @param string $route the route that specifies the action. * @param string $route the route that specifies the action.
* @param array $params the parameters to be passed to 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. * @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()) public function runAction($route, $params = array())
{ {
try { try {
return parent::runAction($route, $params); return parent::runAction($route, $params);
} catch (InvalidRouteException $e) { } 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 ...@@ -48,14 +48,11 @@ class Controller extends \yii\base\Controller
public function runAction($id, $params = array()) public function runAction($id, $params = array())
{ {
if ($params !== array()) { if ($params !== array()) {
$class = new \ReflectionClass($this); $options = $this->globalOptions();
foreach ($params as $name => $value) { foreach ($params as $name => $value) {
if ($class->hasProperty($name)) { if (in_array($name, $options, true)) {
$property = $class->getProperty($name); $this->$name = $value;
if ($property->isPublic() && !$property->isStatic() && $property->getDeclaringClass()->getName() === get_class($this)) { unset($params[$name]);
$this->$name = $value;
unset($params[$name]);
}
} }
} }
} }
...@@ -69,16 +66,19 @@ class Controller extends \yii\base\Controller ...@@ -69,16 +66,19 @@ class Controller extends \yii\base\Controller
* @param Action $action the currently requested action * @param Action $action the currently requested action
* @param array $missingParams the names of the missing parameters * @param array $missingParams the names of the missing parameters
* @param array $unknownParams the unknown parameters (name=>value) * @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) public function validateActionParams($action, $missingParams, $unknownParams)
{ {
unset($missingParams[Request::ANONYMOUS_PARAMS], $unknownParams[Request::ANONYMOUS_PARAMS]);
if (!empty($missingParams)) { 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), '{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), '{params}' => implode(', ', $unknownParams),
))); )));
} }
...@@ -102,6 +102,13 @@ class Controller extends \yii\base\Controller ...@@ -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() public function globalOptions()
{ {
return array(); return array();
......
<?php <?php
/** /**
* BadUsageException class file. * Exception class file.
* *
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC * @copyright Copyright &copy; 2008 Yii Software LLC
...@@ -10,12 +10,12 @@ ...@@ -10,12 +10,12 @@
namespace yii\console; 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> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @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) * @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 ...@@ -27,7 +27,7 @@ class BadUsageException extends \yii\base\Exception
*/ */
public function getName() public function getName()
{ {
return \Yii::t('yii', 'Bad Usage'); return \Yii::t('yii', 'Error');
} }
} }
...@@ -15,6 +15,8 @@ namespace yii\console; ...@@ -15,6 +15,8 @@ namespace yii\console;
*/ */
class Request extends \yii\base\Request 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, * @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. * it means the [[Application::defaultRoute|default route]] will be used.
...@@ -50,13 +52,13 @@ class Request extends \yii\base\Request ...@@ -50,13 +52,13 @@ class Request extends \yii\base\Request
$this->route = ''; $this->route = '';
} }
$this->params = array(); $this->params = array(self::ANONYMOUS_PARAMS => array());
foreach ($rawParams as $param) { foreach ($rawParams as $param) {
if (preg_match('/^--(\w+)(=(.*))?$/', $param, $matches)) { if (preg_match('/^--(\w+)(=(.*))?$/', $param, $matches)) {
$name = $matches[1]; $name = $matches[1];
$this->params[$name] = isset($matches[3]) ? $matches[3] : true; $this->params[$name] = isset($matches[3]) ? $matches[3] : true;
} else { } 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