CAction.php 2.8 KB

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