Broker.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ f04vcN3k+F0v125Z/hSDEpJ6TqKt5ZBBOTaIsNJtnV0=
  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. * controller plugin broker class
  14. */
  15. namespace Cube\Controller\Plugin;
  16. use Cube\Controller\Request\AbstractRequest,
  17. Cube\Controller\Response\AbstractResponse;
  18. class Broker extends AbstractPlugin
  19. {
  20. /**
  21. *
  22. * array of objects of type \Cube\Controller\PluginAbstract
  23. *
  24. * @var array
  25. */
  26. protected $_plugins = array();
  27. /**
  28. *
  29. * add a new plugin to the plugins stack
  30. *
  31. * @param \Cube\Controller\Plugin\AbstractPlugin $plugin
  32. *
  33. * @return \Cube\Controller\Plugin\Broker
  34. * @throws \OverflowException
  35. */
  36. public function registerPlugin(AbstractPlugin $plugin)
  37. {
  38. if ($this->isRegisteredPlugin($plugin) === true) {
  39. throw new \OverflowException("The controller plugin is already registered");
  40. }
  41. else {
  42. $this->_plugins[get_class($plugin)] = $plugin;
  43. }
  44. return $this;
  45. }
  46. /**
  47. *
  48. * check if the plugin has already been registered
  49. *
  50. * @param string|AbstractPlugin $plugin
  51. *
  52. * @return bool
  53. */
  54. public function isRegisteredPlugin($plugin)
  55. {
  56. if ($plugin instanceof AbstractPlugin && array_search($plugin, $this->_plugins, true) !== false) {
  57. return true;
  58. }
  59. else if (is_string($plugin) && array_key_exists($plugin, $this->_plugins)) {
  60. return true;
  61. }
  62. return false;
  63. }
  64. /**
  65. *
  66. * set the request object to the broker and all registered plugins
  67. *
  68. * @param \Cube\Controller\Request\AbstractRequest $request
  69. *
  70. * @return \Cube\Controller\Plugin\Broker
  71. */
  72. public function setRequest(AbstractRequest $request)
  73. {
  74. $this->_request = $request;
  75. foreach ($this->_plugins as $plugin) {
  76. if ($plugin instanceof AbstractPlugin) {
  77. $plugin->setRequest($request);
  78. }
  79. }
  80. return $this;
  81. }
  82. /**
  83. *
  84. * set the response object to the broker and all registered plugins
  85. *
  86. * @param \Cube\Controller\Response\AbstractResponse $response
  87. *
  88. * @return \Cube\Controller\Plugin\Broker
  89. */
  90. public function setResponse(AbstractResponse $response)
  91. {
  92. $this->_response = $response;
  93. foreach ($this->_plugins as $plugin) {
  94. if ($plugin instanceof AbstractPlugin) {
  95. $plugin->setResponse($response);
  96. }
  97. }
  98. return $this;
  99. }
  100. /**
  101. *
  102. * this method will be run prior to routing the request
  103. */
  104. public function preRoute()
  105. {
  106. foreach ($this->_plugins as $plugin) {
  107. if ($plugin instanceof AbstractPlugin) {
  108. $plugin->preRoute();
  109. }
  110. }
  111. }
  112. /**
  113. *
  114. * this method will be run after the request has been routed
  115. */
  116. public function postRoute()
  117. {
  118. foreach ($this->_plugins as $plugin) {
  119. if ($plugin instanceof AbstractPlugin) {
  120. $plugin->postRoute();
  121. }
  122. }
  123. }
  124. /**
  125. *
  126. * this method will be run before starting the dispatch loop
  127. */
  128. public function preDispatcher()
  129. {
  130. foreach ($this->_plugins as $plugin) {
  131. if ($plugin instanceof AbstractPlugin) {
  132. $plugin->preDispatcher();
  133. }
  134. }
  135. }
  136. /**
  137. *
  138. * this method will be run after the dispatch loop has ended
  139. */
  140. public function postDispatcher()
  141. {
  142. foreach ($this->_plugins as $plugin) {
  143. if ($plugin instanceof AbstractPlugin) {
  144. $plugin->postDispatcher();
  145. }
  146. }
  147. }
  148. /**
  149. *
  150. * this method will be run each time an action is dispatched
  151. */
  152. public function preDispatch()
  153. {
  154. foreach ($this->_plugins as $plugin) {
  155. if ($plugin instanceof AbstractPlugin) {
  156. $plugin->preDispatch();
  157. }
  158. }
  159. }
  160. /**
  161. *
  162. * this method will be run after an action has been dispatched
  163. */
  164. public function postDispatch()
  165. {
  166. foreach ($this->_plugins as $plugin) {
  167. if ($plugin instanceof AbstractPlugin) {
  168. $plugin->postDispatch();
  169. }
  170. }
  171. }
  172. }