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
/**
* CAction class file.
* Action class file.
*
* @author Qiang Xue <qiang.xue@gmail.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/
*/
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.
*
* 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.
*
* @property CController $controller The controller who owns this action.
* @property string $id Id of this action.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.web.actions
* @since 1.0
* @since 2.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()
{
return $this->_controller;
}
public $id;
/**
* @return string id of this action
* @var Controller the controller that owns this action
*/
public function getId()
{
return $this->_id;
}
public $controller;
/**
* Runs the action with the supplied request parameters.
* This method is internally called by {@link CController::runAction()}.
* @param array $params the request parameters (name=>value)
* @return boolean whether the request parameters are valid
* @since 1.1.7
* Extracts the input parameters according to the signature of the "run()" 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 "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');
if($method->getNumberOfParameters()>0)
return $this->runWithParamsInternal($this, $method, $params);
else
return $this->run();
$method = new \ReflectionMethod($this, 'run');
return $this->normalizeParamsByMethod($method, $params);
}
/**
* Executes a method of an object with the supplied named parameters.
* This method is internally used.
* @param mixed $object the object whose method is to be executed
* @param ReflectionMethod $method the method reflection
* @param array $params the named parameters
* @return boolean whether the named parameters are valid
* @since 1.1.7
* Extracts the input parameters according to the specified method signature.
* @param \ReflectionMethod $method the method reflection
* @param array $params the parameters in name-value pairs
* @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.
*/
protected function runWithParamsInternal($object, $method, $params)
protected function normalizeParamsByMethod($method, $params)
{
$ps=array();
foreach($method->getParameters() as $i=>$param)
{
$name=$param->getName();
if(isset($params[$name]))
{
if($param->isArray())
$ps[]=is_array($params[$name]) ? $params[$name] : array($params[$name]);
else if(!is_array($params[$name]))
$ps[]=$params[$name];
else
$ps = array();
foreach ($method->getParameters() as $param) {
$name = $param->getName();
if (isset($params[$name])) {
if ($param->isArray()) {
$ps[] = is_array($params[$name]) ? $params[$name] : array($params[$name]);
} elseif (!is_array($params[$name])) {
$ps[] = $params[$name];
} else {
return false;
}
else if($param->isDefaultValueAvailable())
$ps[]=$param->getDefaultValue();
else
} elseif ($param->isDefaultValueAvailable()) {
$ps[] = $param->getDefaultValue();
} else {
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.
* A filter chain will be created based on the specified filters
* 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.
* @see filters
* @see createAction
@ -245,18 +245,18 @@ abstract class Controller extends Component implements Initable
* Runs the action after passing through all filters.
* This method is invoked by {@link runActionWithFilters} after all possible filters have been executed
* and the action starts to run.
* @param CAction $action action to run
* @param Action $action action to run
*/
public function runAction($action)
{
$priorAction = $this->_action;
$this->_action = $action;
if ($this->beforeAction($action)) {
if ($action->runWithParams($this->getActionParams()) === false) {
$params = $action->normalizeParams($this->getActionParams());
if ($params === false) {
$this->invalidActionParams($action);
}
else
{
} else {
call_user_func_array(array($action, 'run'), $params);
$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.
* By default, this method will return $_GET. You may override this method if you
* want to use other request parameters (e.g. $_GET+$_POST).
* @return array the request parameters to be used for action parameter binding
* @since 1.1.7
* Default implementation simply returns an empty array.
* Child classes may override this method to customize the parameters to be provided
* for action parameter binding (e.g. `$_GET`).
* @return array the request parameters (name-value pairs) to be used for action parameter binding
*/
public function getActionParams()
{
return $_GET;
return array();
}
/**
* 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.
* @param CAction $action the action being executed
* @since 1.1.7
* @param Action $action the action being executed
* @throws HttpException a 400 HTTP exception
*/
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 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.
* @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
*/
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 $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
* @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())
{
@ -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()
{
@ -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)
{
@ -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.)
* 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.
*/
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.
* 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)
{

51
framework/base/InlineAction.php

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

Loading…
Cancel
Save