1: <?php
2: /**
3: * CAction class file.
4: *
5: * @author Qiang Xue <qiang.xue@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: /**
12: * CAction is the base class for all controller action classes.
13: *
14: * CAction provides a way to divide a complex controller into
15: * smaller actions in separate class files.
16: *
17: * Derived classes must implement {@link run()} which is invoked by
18: * controller when the action is requested.
19: *
20: * An action instance can access its controller via {@link getController controller} property.
21: *
22: * @property CController $controller The controller who owns this action.
23: * @property string $id Id of this action.
24: *
25: * @method run() executes action
26: *
27: * @author Qiang Xue <qiang.xue@gmail.com>
28: * @package system.web.actions
29: * @since 1.0
30: */
31: abstract class CAction extends CComponent implements IAction
32: {
33: private $_id;
34: private $_controller;
35:
36: /**
37: * Constructor.
38: * @param CController $controller the controller who owns this action.
39: * @param string $id id of the action.
40: */
41: public function __construct($controller,$id)
42: {
43: $this->_controller=$controller;
44: $this->_id=$id;
45: }
46:
47: /**
48: * @return CController the controller who owns this action.
49: */
50: public function getController()
51: {
52: return $this->_controller;
53: }
54:
55: /**
56: * @return string id of this action
57: */
58: public function getId()
59: {
60: return $this->_id;
61: }
62:
63: /**
64: * Runs the action with the supplied request parameters.
65: * This method is internally called by {@link CController::runAction()}.
66: * @param array $params the request parameters (name=>value)
67: * @return boolean whether the request parameters are valid
68: * @since 1.1.7
69: */
70: public function runWithParams($params)
71: {
72: $method=new ReflectionMethod($this, 'run');
73: if($method->getNumberOfParameters()>0)
74: return $this->runWithParamsInternal($this, $method, $params);
75:
76: $this->run();
77: return true;
78: }
79:
80: /**
81: * Executes a method of an object with the supplied named parameters.
82: * This method is internally used.
83: * @param mixed $object the object whose method is to be executed
84: * @param ReflectionMethod $method the method reflection
85: * @param array $params the named parameters
86: * @return boolean whether the named parameters are valid
87: * @since 1.1.7
88: */
89: protected function runWithParamsInternal($object, $method, $params)
90: {
91: $ps=array();
92: foreach($method->getParameters() as $i=>$param)
93: {
94: $name=$param->getName();
95: if(isset($params[$name]))
96: {
97: if($param->isArray())
98: $ps[]=is_array($params[$name]) ? $params[$name] : array($params[$name]);
99: elseif(!is_array($params[$name]))
100: $ps[]=$params[$name];
101: else
102: return false;
103: }
104: elseif($param->isDefaultValueAvailable())
105: $ps[]=$param->getDefaultValue();
106: else
107: return false;
108: }
109: $method->invokeArgs($object,$ps);
110: return true;
111: }
112: }
113: