AbstractAction.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ CBEPj2TttQrY75HSDT6Q9fJrsTTxInXqMo0cRpSl3jY=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.0
  11. */
  12. /**
  13. * abstract action controller class
  14. * application controllers will extend from this class
  15. */
  16. namespace Cube\Controller\Action;
  17. use Cube\Controller\Request\AbstractRequest,
  18. Cube\Controller\Response\ResponseInterface;
  19. abstract class AbstractAction
  20. {
  21. /**
  22. *
  23. * request object
  24. *
  25. * @var \Cube\Controller\Request\AbstractRequest
  26. */
  27. protected $_request;
  28. /**
  29. *
  30. * response object
  31. *
  32. * @var \Cube\Controller\Response\ResponseInterface
  33. */
  34. protected $_response;
  35. /**
  36. *
  37. * action helper broker
  38. *
  39. * @var \Cube\Controller\Action\Helper\Broker
  40. */
  41. protected $_helper;
  42. /**
  43. *
  44. * class constructor
  45. *
  46. * @param \Cube\Controller\Request\AbstractRequest $request
  47. * @param \Cube\Controller\Response\ResponseInterface $response
  48. */
  49. public function __construct(AbstractRequest $request, ResponseInterface $response)
  50. {
  51. $this->setRequest($request)
  52. ->setResponse($response);
  53. $this->_helper = new Helper\Broker($this);
  54. $this->init();
  55. }
  56. /**
  57. *
  58. * function run when controller is initialized, can be overridden if functionality is needed to apply for
  59. * all actions
  60. */
  61. public function init()
  62. {
  63. }
  64. /**
  65. *
  66. * return request object
  67. *
  68. * @return \Cube\Controller\Request\AbstractRequest
  69. */
  70. public function getRequest()
  71. {
  72. return $this->_request;
  73. }
  74. /**
  75. *
  76. * set request object
  77. *
  78. * @param \Cube\Controller\Request\AbstractRequest $request
  79. *
  80. * @return \Cube\Controller\Action\AbstractAction
  81. */
  82. public function setRequest(AbstractRequest $request)
  83. {
  84. $this->_request = $request;
  85. return $this;
  86. }
  87. /**
  88. *
  89. * get response object
  90. *
  91. * @return \Cube\Controller\Response\ResponseInterface
  92. */
  93. public function getResponse()
  94. {
  95. return $this->_response;
  96. }
  97. /**
  98. *
  99. * set response object
  100. *
  101. * @param \Cube\Controller\Response\ResponseInterface $response
  102. *
  103. * @return \Cube\Controller\Action\AbstractAction
  104. */
  105. public function setResponse(ResponseInterface $response)
  106. {
  107. $this->_response = $response;
  108. return $this;
  109. }
  110. /**
  111. *
  112. * forward to another action/controller/module
  113. *
  114. * used by the front controller dispatcher and initiates a dispatch loop
  115. * the pre-dispatch and post-dispatch plugins will be run on each loop
  116. *
  117. * @param string $action
  118. * @param string $controller
  119. * @param string $module
  120. * @param array|null $params
  121. */
  122. final protected function _forward($action, $controller = null, $module = null, $params = null)
  123. {
  124. $request = $this->getRequest();
  125. if ($controller !== null) {
  126. $request->setController($controller);
  127. }
  128. if ($module !== null) {
  129. $request->setModule($module);
  130. }
  131. if ($params !== null) {
  132. $request->setParams($params);
  133. }
  134. $request->setAction($action)
  135. ->setDispatched(false);
  136. }
  137. }