Commit 769c1dee by Qiang Xue

...

parent be8dd940
...@@ -55,11 +55,14 @@ class Action extends Component ...@@ -55,11 +55,14 @@ class Action extends Component
public function runWithParams($params) public function runWithParams($params)
{ {
try { try {
$params = ReflectionHelper::extractMethodParams($this, 'run', $params); $ps = ReflectionHelper::extractMethodParams($this, 'run', $params);
return (int)call_user_func_array(array($this, 'run'), $params);
} catch (Exception $e) { } catch (Exception $e) {
$this->controller->invalidActionParams($this, $e); $this->controller->invalidActionParams($this, $e);
return 1; return 1;
} }
if ($params !== $ps) {
$this->controller->extraActionParams($this, $ps, $params);
}
return (int)call_user_func_array(array($this, 'run'), $ps);
} }
} }
...@@ -175,6 +175,17 @@ class Controller extends Component implements Initable ...@@ -175,6 +175,17 @@ class Controller extends Component implements Initable
} }
/** /**
* This method is invoked when extra parameters are provided to an action when it is executed.
* The default implementation does nothing.
* @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)
*/
public function extraActionParams($action, $expected, $actual)
{
}
/**
* Handles the request whose action is not recognized. * Handles the request whose action is not recognized.
* This method is invoked when the controller cannot find the requested action. * This method is invoked when the controller cannot find the requested action.
* The default implementation simply throws an exception. * The default implementation simply throws an exception.
......
...@@ -32,11 +32,14 @@ class InlineAction extends Action ...@@ -32,11 +32,14 @@ class InlineAction extends Action
{ {
try { try {
$method = 'action' . $this->id; $method = 'action' . $this->id;
$params = ReflectionHelper::extractMethodParams($this->controller, $method, $params); $ps = ReflectionHelper::extractMethodParams($this->controller, $method, $params);
return (int)call_user_func_array(array($this->controller, $method), $params);
} catch (Exception $e) { } catch (Exception $e) {
$this->controller->invalidActionParams($this, $e); $this->controller->invalidActionParams($this, $e);
return 1; return 1;
} }
if ($params !== $ps) {
$this->controller->extraActionParams($this, $ps, $params);
}
return (int)call_user_func_array(array($this->controller, $method), $ps);
} }
} }
...@@ -9,9 +9,8 @@ ...@@ -9,9 +9,8 @@
namespace yii\console; namespace yii\console;
use yii\base\InlineAction; use yii\base\Action;
use yii\base\Exception; use yii\base\Exception;
use yii\util\ReflectionHelper;
/** /**
* Command represents an executable console command. * Command represents an executable console command.
...@@ -51,6 +50,28 @@ use yii\util\ReflectionHelper; ...@@ -51,6 +50,28 @@ use yii\util\ReflectionHelper;
class Controller extends \yii\base\Controller class Controller extends \yii\base\Controller
{ {
/** /**
* This method is invoked when the request parameters do not satisfy the requirement of the specified action.
* The default implementation will throw an exception.
* @param Action $action the action being executed
* @param Exception $exception the exception about the invalid parameters
* @throws Exception whenever this method is invoked
*/
public function invalidActionParams($action, $exception)
{
}
/**
* This method is invoked when extra parameters are provided to an action when it is executed.
* The default implementation does nothing.
* @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)
*/
public function extraActionParams($action, $expected, $actual)
{
}
/**
* Provides the command description. * Provides the command description.
* This method may be overridden to return the actual command description. * This method may be overridden to return the actual command description.
* @return string the command description. Defaults to 'Usage: php entry-script.php command-name'. * @return string the command description. Defaults to 'Usage: php entry-script.php command-name'.
......
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