Browse Source

implemented action.

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
1db738ef1e
  1. 113
      framework/base/Action.php
  2. 40
      framework/base/Controller.php
  3. 51
      framework/base/InlineAction.php

113
framework/base/Action.php

@ -1,17 +1,18 @@
<?php <?php
/** /**
* CAction class file. * Action 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;
/** /**
* CAction is the base class for all controller action classes. * Action is the base class for all controller action classes.
* *
* CAction 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 {@link run()} which is invoked by
@ -19,92 +20,60 @@
* *
* An action instance can access its controller via {@link getController controller} property. * An action instance can access its controller via {@link getController controller} property.
* *
* @property CController $controller The controller who owns this action.
* @property string $id Id of this action.
*
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$ * @since 2.0
* @package system.web.actions
* @since 1.0
*/ */
abstract class CAction extends CComponent implements IAction class Action extends Component
{ {
private $_id;
private $_controller;
/**
* Constructor.
* @param CController $controller the controller who owns this action.
* @param string $id id of the action.
*/
public function __construct($controller,$id)
{
$this->_controller=$controller;
$this->_id=$id;
}
/** /**
* @return CController the controller who owns this action. * @var string ID of the action
*/ */
public function getController() public $id;
{
return $this->_controller;
}
/** /**
* @return string id of this action * @var Controller the controller that owns this action
*/ */
public function getId() public $controller;
{
return $this->_id;
}
/** /**
* Runs the action with the supplied request parameters. * Extracts the input parameters according to the signature of the "run()" method.
* This method is internally called by {@link CController::runAction()}. * This method is invoked by controller when it attempts to run the action
* @param array $params the request parameters (name=>value) * with the user supplied parameters.
* @return boolean whether the request parameters are valid * @param array $params the parameters in name-value pairs
* @since 1.1.7 * @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 runWithParams($params) public function normalizeParams($params)
{ {
$method=new ReflectionMethod($this, 'run'); $method = new \ReflectionMethod($this, 'run');
if($method->getNumberOfParameters()>0) return $this->normalizeParamsByMethod($method, $params);
return $this->runWithParamsInternal($this, $method, $params);
else
return $this->run();
} }
/** /**
* Executes a method of an object with the supplied named parameters. * Extracts the input parameters according to the specified method signature.
* This method is internally used. * @param \ReflectionMethod $method the method reflection
* @param mixed $object the object whose method is to be executed * @param array $params the parameters in name-value pairs
* @param ReflectionMethod $method the method reflection * @return array|boolean the extracted parameters in the order as declared in the "run()" method.
* @param array $params the named parameters * False is returned if the input parameters do not follow the method declaration.
* @return boolean whether the named parameters are valid
* @since 1.1.7
*/ */
protected function runWithParamsInternal($object, $method, $params) protected function normalizeParamsByMethod($method, $params)
{ {
$ps=array(); $ps = array();
foreach($method->getParameters() as $i=>$param) foreach ($method->getParameters() as $param) {
{ $name = $param->getName();
$name=$param->getName(); if (isset($params[$name])) {
if(isset($params[$name])) if ($param->isArray()) {
{ $ps[] = is_array($params[$name]) ? $params[$name] : array($params[$name]);
if($param->isArray()) } elseif (!is_array($params[$name])) {
$ps[]=is_array($params[$name]) ? $params[$name] : array($params[$name]); $ps[] = $params[$name];
else if(!is_array($params[$name])) } else {
$ps[]=$params[$name];
else
return false; return false;
} }
else if($param->isDefaultValueAvailable()) } elseif ($param->isDefaultValueAvailable()) {
$ps[]=$param->getDefaultValue(); $ps[] = $param->getDefaultValue();
else } else {
return false; return false;
} }
$method->invokeArgs($object,$ps); }
return true; return false;
} }
} }

40
framework/base/Controller.php

@ -221,7 +221,7 @@ abstract class Controller extends Component implements Initable
* Runs an action with the specified filters. * Runs an action with the specified filters.
* A filter chain will be created based on the specified filters * A filter chain will be created based on the specified filters
* and the action will be executed then. * and the action will be executed then.
* @param CAction $action the action to be executed. * @param Action $action the action to be executed.
* @param array $filters list of filters to be applied to the action. * @param array $filters list of filters to be applied to the action.
* @see filters * @see filters
* @see createAction * @see createAction
@ -245,18 +245,18 @@ abstract class Controller extends Component implements Initable
* Runs the action after passing through all filters. * Runs the action after passing through all filters.
* This method is invoked by {@link runActionWithFilters} after all possible filters have been executed * This method is invoked by {@link runActionWithFilters} after all possible filters have been executed
* and the action starts to run. * and the action starts to run.
* @param CAction $action action to run * @param Action $action action to run
*/ */
public function runAction($action) public function runAction($action)
{ {
$priorAction = $this->_action; $priorAction = $this->_action;
$this->_action = $action; $this->_action = $action;
if ($this->beforeAction($action)) { if ($this->beforeAction($action)) {
if ($action->runWithParams($this->getActionParams()) === false) { $params = $action->normalizeParams($this->getActionParams());
if ($params === false) {
$this->invalidActionParams($action); $this->invalidActionParams($action);
} } else {
else call_user_func_array(array($action, 'run'), $params);
{
$this->afterAction($action); $this->afterAction($action);
} }
} }
@ -265,25 +265,25 @@ abstract class Controller extends Component implements Initable
/** /**
* Returns the request parameters that will be used for action parameter binding. * Returns the request parameters that will be used for action parameter binding.
* By default, this method will return $_GET. You may override this method if you * Default implementation simply returns an empty array.
* want to use other request parameters (e.g. $_GET+$_POST). * Child classes may override this method to customize the parameters to be provided
* @return array the request parameters to be used for action parameter binding * for action parameter binding (e.g. `$_GET`).
* @since 1.1.7 * @return array the request parameters (name-value pairs) to be used for action parameter binding
*/ */
public function getActionParams() public function getActionParams()
{ {
return $_GET; return array();
} }
/** /**
* This method is invoked when the request parameters do not satisfy the requirement of the specified action. * This method is invoked when the request parameters do not satisfy the requirement of the specified action.
* The default implementation will throw a 400 HTTP exception. * The default implementation will throw a 400 HTTP exception.
* @param CAction $action the action being executed * @param Action $action the action being executed
* @since 1.1.7 * @throws HttpException a 400 HTTP exception
*/ */
public function invalidActionParams($action) public function invalidActionParams($action)
{ {
throw new CHttpException(400, Yii::t('yii', 'Your request is invalid.')); throw new HttpException(400, \Yii::t('yii', 'Your request is invalid.'));
} }
/** /**
@ -291,7 +291,7 @@ abstract class Controller extends Component implements Initable
* The action can be either an inline action or an object. * The action can be either an inline action or an object.
* The latter is created by looking up the action map specified in {@link actions}. * The latter is created by looking up the action map specified in {@link actions}.
* @param string $actionID ID of the action. If empty, the {@link defaultAction default action} will be used. * @param string $actionID ID of the action. If empty, the {@link defaultAction default action} will be used.
* @return CAction the action instance, null if the action does not exist. * @return Action the action instance, null if the action does not exist.
* @see actions * @see actions
*/ */
public function createAction($actionID) public function createAction($actionID)
@ -322,7 +322,7 @@ abstract class Controller extends Component implements Initable
* @param string $actionID the action ID that has its prefix stripped off * @param string $actionID the action ID that has its prefix stripped off
* @param string $requestActionID the originally requested action ID * @param string $requestActionID the originally requested action ID
* @param array $config the action configuration that should be applied on top of the configuration specified in the map * @param array $config the action configuration that should be applied on top of the configuration specified in the map
* @return CAction the action instance, null if the action does not exist. * @return Action the action instance, null if the action does not exist.
*/ */
protected function createActionFromMap($actionMap, $actionID, $requestActionID, $config = array()) protected function createActionFromMap($actionMap, $actionID, $requestActionID, $config = array())
{ {
@ -386,7 +386,7 @@ abstract class Controller extends Component implements Initable
} }
/** /**
* @return CAction the action currently being executed, null if no active action. * @return Action the action currently being executed, null if no active action.
*/ */
public function getAction() public function getAction()
{ {
@ -394,7 +394,7 @@ abstract class Controller extends Component implements Initable
} }
/** /**
* @param CAction $value the action currently being executed. * @param Action $value the action currently being executed.
*/ */
public function setAction($value) public function setAction($value)
{ {
@ -471,7 +471,7 @@ abstract class Controller extends Component implements Initable
/** /**
* This method is invoked right before an action is to be executed (after all possible filters.) * This method is invoked right before an action is to be executed (after all possible filters.)
* You may override this method to do last-minute preparation for the action. * You may override this method to do last-minute preparation for the action.
* @param CAction $action the action to be executed. * @param Action $action the action to be executed.
* @return boolean whether the action should be executed. * @return boolean whether the action should be executed.
*/ */
protected function beforeAction($action) protected function beforeAction($action)
@ -482,7 +482,7 @@ abstract class Controller extends Component implements Initable
/** /**
* This method is invoked right after an action is executed. * This method is invoked right after an action is executed.
* You may override this method to do some postprocessing for the action. * You may override this method to do some postprocessing for the action.
* @param CAction $action the action just executed. * @param Action $action the action just executed.
*/ */
protected function afterAction($action) protected function afterAction($action)
{ {

51
framework/base/InlineAction.php

@ -1,53 +1,50 @@
<?php <?php
/** /**
* CInlineAction class file. * InlineAction 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;
/** /**
* CInlineAction 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 method name is like 'actionXYZ' where 'XYZ' stands for the action name.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$ * @since 2.0
* @package system.web.actions
* @since 1.0
*/ */
class CInlineAction extends CAction class InlineAction extends Action
{ {
/** /**
* Runs the action. * Runs the action.
* The action method defined in the controller is invoked. * This method is invoked by the controller to run the action.
* This method is required by {@link CAction}. * @param array $params the input parameters
*/ */
public function run() public function run($params)
{ {
$method='action'.$this->getId(); call_user_func_array(array($this->controller, 'action' . $this->id), $params);
$this->getController()->$method();
} }
/** /**
* Runs the action with the supplied request parameters. * Extracts the input parameters according to the signature of the controller action method.
* This method is internally called by {@link CController::runAction()}. * This method is invoked by controller when it attempts to run the action
* @param array $params the request parameters (name=>value) * with the user supplied parameters.
* @return boolean whether the request parameters are valid * @param array $params the parameters in name-value pairs
* @since 1.1.7 * @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 runWithParams($params) public function normalizeParams($params)
{ {
$methodName='action'.$this->getId(); $method = new \ReflectionMethod($this->controller, 'action' . $this->id);
$controller=$this->getController(); $params = $this->normalizeParams($method, $params);
$method=new ReflectionMethod($controller, $methodName); if ($params !== false) {
if($method->getNumberOfParameters()>0) return array($params);
return $this->runWithParamsInternal($controller, $method, $params); } else {
else return false;
return $controller->$methodName(); }
} }
} }

Loading…
Cancel
Save