Browse Source

...

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
d1611e66a8
  1. 28
      framework/base/Action.php
  2. 21
      framework/base/ActionFilter.php
  3. 8
      framework/base/Controller.php
  4. 30
      framework/base/InlineAction.php

28
framework/base/Action.php

@ -15,10 +15,10 @@ namespace yii\base;
* Action provides a way to divide a complex controller into * Action provides a way to divide a complex controller into
* smaller actions in separate class files. * smaller actions in separate class files.
* *
* Derived classes must implement {@link run()} which is invoked by * Derived classes must implement a method named `run()`. This method
* controller when the action is requested. * will be invoked by the controller when the action is requested.
* * The `run()` method can have parameters which will be filled up
* An action instance can access its controller via {@link getController controller} property. * automatically according to their names.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
@ -35,17 +35,21 @@ class Action extends Component
public $controller; public $controller;
/** /**
* Extracts the input parameters according to the signature of the "run()" method. * Runs the action with the supplied parameters.
* This method is invoked by controller when it attempts to run the action * This method is invoked by the controller.
* with the user supplied parameters. * @param array $params the input parameters in terms of name-value pairs.
* @param array $params the parameters in name-value pairs * @return boolean whether the input parameters are valid
* @return array|boolean the extracted parameters in the order as declared in the "run()" method.
* False is returned if the input parameters do not follow the method declaration.
*/ */
public function normalizeParams($params) public function runWithParams($params)
{ {
$method = new \ReflectionMethod($this, 'run'); $method = new \ReflectionMethod($this, 'run');
return $this->normalizeParamsByMethod($method, $params); $params = $this->normalizeParamsByMethod($method, $params);
if ($params !== false) {
call_user_func_array(array($this, 'run'), $params);
return true;
} else {
return false;
}
} }
/** /**

21
framework/base/ActionFilter.php

@ -1,15 +1,16 @@
<?php <?php
/** /**
* CFilter class file. * ActionFilter class file.
* *
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\base;
/** /**
* CFilter is the base class for all filters. * ActionFilter is the base class for all filters.
* *
* A filter can be applied before and after an action is executed. * A filter can be applied before and after an action is executed.
* It can modify the context that the action is to run or decorate the result that the * It can modify the context that the action is to run or decorate the result that the
@ -19,11 +20,9 @@
* before the action, and {@link postFilter()} for filtering logic after the action. * before the action, and {@link postFilter()} for filtering logic after the action.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$ * @since 2.0
* @package system.web.filters
* @since 1.0
*/ */
class CFilter extends CComponent implements IFilter class ActionFilter extends CComponent implements IFilter
{ {
/** /**
* Performs the filtering. * Performs the filtering.
@ -32,7 +31,7 @@ class CFilter extends CComponent implements IFilter
* child classes. If a child class needs to override this method, * child classes. If a child class needs to override this method,
* make sure it calls <code>$filterChain->run()</code> * make sure it calls <code>$filterChain->run()</code>
* if the action should be executed. * if the action should be executed.
* @param CFilterChain $filterChain the filter chain that the filter is on. * @param ActionFilterChain $filterChain the filter chain that the filter is on.
*/ */
public function filter($filterChain) public function filter($filterChain)
{ {
@ -56,7 +55,7 @@ class CFilter extends CComponent implements IFilter
/** /**
* Performs the pre-action filtering. * Performs the pre-action filtering.
* @param CFilterChain $filterChain the filter chain that the filter is on. * @param ActionFilterChain $filterChain the filter chain that the filter is on.
* @return boolean whether the filtering process should continue and the action * @return boolean whether the filtering process should continue and the action
* should be executed. * should be executed.
*/ */
@ -67,7 +66,7 @@ class CFilter extends CComponent implements IFilter
/** /**
* Performs the post-action filtering. * Performs the post-action filtering.
* @param CFilterChain $filterChain the filter chain that the filter is on. * @param ActionFilterChain $filterChain the filter chain that the filter is on.
*/ */
protected function postFilter($filterChain) protected function postFilter($filterChain)
{ {

8
framework/base/Controller.php

@ -252,12 +252,10 @@ abstract class Controller extends Component implements Initable
$priorAction = $this->_action; $priorAction = $this->_action;
$this->_action = $action; $this->_action = $action;
if ($this->beforeAction($action)) { if ($this->beforeAction($action)) {
$params = $action->normalizeParams($this->getActionParams()); if ($action->runWithParams($this->getActionParams())) {
if ($params === false) {
$this->invalidActionParams($action);
} else {
call_user_func_array(array($action, 'run'), $params);
$this->afterAction($action); $this->afterAction($action);
} else {
$this->invalidActionParams($action);
} }
} }
$this->_action = $priorAction; $this->_action = $priorAction;

30
framework/base/InlineAction.php

@ -12,7 +12,8 @@ namespace yii\base;
/** /**
* InlineAction represents an action that is defined as a controller method. * InlineAction represents an action that is defined as a controller method.
* *
* The method name is like 'actionXYZ' where 'XYZ' stands for the action name. * The name of the controller method should be in the format of `actionXyz`
* where `Xyz` stands for the action ID (e.g. `actionIndex`).
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
@ -20,29 +21,18 @@ namespace yii\base;
class InlineAction extends Action class InlineAction extends Action
{ {
/** /**
* Runs the action. * Runs the action with the supplied parameters.
* This method is invoked by the controller to run the action. * This method is invoked by the controller.
* @param array $params the input parameters * @param array $params the input parameters in terms of name-value pairs.
* @return boolean whether the input parameters are valid
*/ */
public function run($params) public function runWithParams($params)
{
call_user_func_array(array($this->controller, 'action' . $this->id), $params);
}
/**
* Extracts the input parameters according to the signature of the controller action method.
* This method is invoked by controller when it attempts to run the action
* with the user supplied parameters.
* @param array $params the parameters in name-value pairs
* @return array|boolean the extracted parameters in the order as declared in the controller action method.
* False is returned if the input parameters do not follow the method declaration.
*/
public function normalizeParams($params)
{ {
$method = new \ReflectionMethod($this->controller, 'action' . $this->id); $method = new \ReflectionMethod($this->controller, 'action' . $this->id);
$params = $this->normalizeParams($method, $params); $params = $this->normalizeParamsByMethod($method, $params);
if ($params !== false) { if ($params !== false) {
return array($params); call_user_func_array(array($this->controller, 'action' . $this->id), $params);
return true;
} else { } else {
return false; return false;
} }

Loading…
Cancel
Save