Broker.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @subpackage Plugins
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Controller_Plugin_Abstract */
  22. require_once 'Zend/Controller/Plugin/Abstract.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Controller
  26. * @subpackage Plugins
  27. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_Controller_Plugin_Broker extends Zend_Controller_Plugin_Abstract
  31. {
  32. /**
  33. * Array of instance of objects extending Zend_Controller_Plugin_Abstract
  34. *
  35. * @var array
  36. */
  37. protected $_plugins = array();
  38. /**
  39. * Register a plugin.
  40. *
  41. * @param Zend_Controller_Plugin_Abstract $plugin
  42. * @param int $stackIndex
  43. * @return Zend_Controller_Plugin_Broker
  44. */
  45. public function registerPlugin(Zend_Controller_Plugin_Abstract $plugin, $stackIndex = null)
  46. {
  47. if (false !== array_search($plugin, $this->_plugins, true)) {
  48. require_once 'Zend/Controller/Exception.php';
  49. throw new Zend_Controller_Exception('Plugin already registered');
  50. }
  51. $stackIndex = (int) $stackIndex;
  52. if ($stackIndex) {
  53. if (isset($this->_plugins[$stackIndex])) {
  54. require_once 'Zend/Controller/Exception.php';
  55. throw new Zend_Controller_Exception('Plugin with stackIndex "' . $stackIndex . '" already registered');
  56. }
  57. $this->_plugins[$stackIndex] = $plugin;
  58. } else {
  59. $stackIndex = count($this->_plugins);
  60. while (isset($this->_plugins[$stackIndex])) {
  61. ++$stackIndex;
  62. }
  63. $this->_plugins[$stackIndex] = $plugin;
  64. }
  65. $request = $this->getRequest();
  66. if ($request) {
  67. $this->_plugins[$stackIndex]->setRequest($request);
  68. }
  69. $response = $this->getResponse();
  70. if ($response) {
  71. $this->_plugins[$stackIndex]->setResponse($response);
  72. }
  73. ksort($this->_plugins);
  74. return $this;
  75. }
  76. /**
  77. * Unregister a plugin.
  78. *
  79. * @param string|Zend_Controller_Plugin_Abstract $plugin Plugin object or class name
  80. * @return Zend_Controller_Plugin_Broker
  81. */
  82. public function unregisterPlugin($plugin)
  83. {
  84. if ($plugin instanceof Zend_Controller_Plugin_Abstract) {
  85. // Given a plugin object, find it in the array
  86. $key = array_search($plugin, $this->_plugins, true);
  87. if (false === $key) {
  88. require_once 'Zend/Controller/Exception.php';
  89. throw new Zend_Controller_Exception('Plugin never registered.');
  90. }
  91. unset($this->_plugins[$key]);
  92. } elseif (is_string($plugin)) {
  93. // Given a plugin class, find all plugins of that class and unset them
  94. foreach ($this->_plugins as $key => $_plugin) {
  95. $type = get_class($_plugin);
  96. if ($plugin == $type) {
  97. unset($this->_plugins[$key]);
  98. }
  99. }
  100. }
  101. return $this;
  102. }
  103. /**
  104. * Is a plugin of a particular class registered?
  105. *
  106. * @param string $class
  107. * @return bool
  108. */
  109. public function hasPlugin($class)
  110. {
  111. foreach ($this->_plugins as $plugin) {
  112. $type = get_class($plugin);
  113. if ($class == $type) {
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119. /**
  120. * Retrieve a plugin or plugins by class
  121. *
  122. * @param string $class Class name of plugin(s) desired
  123. * @return false|Zend_Controller_Plugin_Abstract|array Returns false if none found, plugin if only one found, and array of plugins if multiple plugins of same class found
  124. */
  125. public function getPlugin($class)
  126. {
  127. $found = array();
  128. foreach ($this->_plugins as $plugin) {
  129. $type = get_class($plugin);
  130. if ($class == $type) {
  131. $found[] = $plugin;
  132. }
  133. }
  134. switch (count($found)) {
  135. case 0:
  136. return false;
  137. case 1:
  138. return $found[0];
  139. default:
  140. return $found;
  141. }
  142. }
  143. /**
  144. * Retrieve all plugins
  145. *
  146. * @return array
  147. */
  148. public function getPlugins()
  149. {
  150. return $this->_plugins;
  151. }
  152. /**
  153. * Set request object, and register with each plugin
  154. *
  155. * @param Zend_Controller_Request_Abstract $request
  156. * @return Zend_Controller_Plugin_Broker
  157. */
  158. public function setRequest(Zend_Controller_Request_Abstract $request)
  159. {
  160. $this->_request = $request;
  161. foreach ($this->_plugins as $plugin) {
  162. $plugin->setRequest($request);
  163. }
  164. return $this;
  165. }
  166. /**
  167. * Get request object
  168. *
  169. * @return Zend_Controller_Request_Abstract $request
  170. */
  171. public function getRequest()
  172. {
  173. return $this->_request;
  174. }
  175. /**
  176. * Set response object
  177. *
  178. * @param Zend_Controller_Response_Abstract $response
  179. * @return Zend_Controller_Plugin_Broker
  180. */
  181. public function setResponse(Zend_Controller_Response_Abstract $response)
  182. {
  183. $this->_response = $response;
  184. foreach ($this->_plugins as $plugin) {
  185. $plugin->setResponse($response);
  186. }
  187. return $this;
  188. }
  189. /**
  190. * Get response object
  191. *
  192. * @return Zend_Controller_Response_Abstract $response
  193. */
  194. public function getResponse()
  195. {
  196. return $this->_response;
  197. }
  198. /**
  199. * Called before Zend_Controller_Front begins evaluating the
  200. * request against its routes.
  201. *
  202. * @param Zend_Controller_Request_Abstract $request
  203. * @return void
  204. */
  205. public function routeStartup(Zend_Controller_Request_Abstract $request)
  206. {
  207. foreach ($this->_plugins as $plugin) {
  208. try {
  209. $plugin->routeStartup($request);
  210. } catch (Exception $e) {
  211. if (Zend_Controller_Front::getInstance()->throwExceptions()) {
  212. throw $e;
  213. } else {
  214. $this->getResponse()->setException($e);
  215. }
  216. }
  217. }
  218. }
  219. /**
  220. * Called before Zend_Controller_Front exits its iterations over
  221. * the route set.
  222. *
  223. * @param Zend_Controller_Request_Abstract $request
  224. * @return void
  225. */
  226. public function routeShutdown(Zend_Controller_Request_Abstract $request)
  227. {
  228. foreach ($this->_plugins as $plugin) {
  229. try {
  230. $plugin->routeShutdown($request);
  231. } catch (Exception $e) {
  232. if (Zend_Controller_Front::getInstance()->throwExceptions()) {
  233. throw $e;
  234. } else {
  235. $this->getResponse()->setException($e);
  236. }
  237. }
  238. }
  239. }
  240. /**
  241. * Called before Zend_Controller_Front enters its dispatch loop.
  242. *
  243. * During the dispatch loop, Zend_Controller_Front keeps a
  244. * Zend_Controller_Request_Abstract object, and uses
  245. * Zend_Controller_Dispatcher to dispatch the
  246. * Zend_Controller_Request_Abstract object to controllers/actions.
  247. *
  248. * @param Zend_Controller_Request_Abstract $request
  249. * @return void
  250. */
  251. public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
  252. {
  253. foreach ($this->_plugins as $plugin) {
  254. try {
  255. $plugin->dispatchLoopStartup($request);
  256. } catch (Exception $e) {
  257. if (Zend_Controller_Front::getInstance()->throwExceptions()) {
  258. throw $e;
  259. } else {
  260. $this->getResponse()->setException($e);
  261. }
  262. }
  263. }
  264. }
  265. /**
  266. * Called before an action is dispatched by Zend_Controller_Dispatcher.
  267. *
  268. * @param Zend_Controller_Request_Abstract $request
  269. * @return void
  270. */
  271. public function preDispatch(Zend_Controller_Request_Abstract $request)
  272. {
  273. foreach ($this->_plugins as $plugin) {
  274. try {
  275. $plugin->preDispatch($request);
  276. } catch (Exception $e) {
  277. if (Zend_Controller_Front::getInstance()->throwExceptions()) {
  278. throw $e;
  279. } else {
  280. $this->getResponse()->setException($e);
  281. }
  282. }
  283. }
  284. }
  285. /**
  286. * Called after an action is dispatched by Zend_Controller_Dispatcher.
  287. *
  288. * @param Zend_Controller_Request_Abstract $request
  289. * @return void
  290. */
  291. public function postDispatch(Zend_Controller_Request_Abstract $request)
  292. {
  293. foreach ($this->_plugins as $plugin) {
  294. try {
  295. $plugin->postDispatch($request);
  296. } catch (Exception $e) {
  297. if (Zend_Controller_Front::getInstance()->throwExceptions()) {
  298. throw $e;
  299. } else {
  300. $this->getResponse()->setException($e);
  301. }
  302. }
  303. }
  304. }
  305. /**
  306. * Called before Zend_Controller_Front exits its dispatch loop.
  307. *
  308. * @param Zend_Controller_Request_Abstract $request
  309. * @return void
  310. */
  311. public function dispatchLoopShutdown()
  312. {
  313. foreach ($this->_plugins as $plugin) {
  314. try {
  315. $plugin->dispatchLoopShutdown();
  316. } catch (Exception $e) {
  317. if (Zend_Controller_Front::getInstance()->throwExceptions()) {
  318. throw $e;
  319. } else {
  320. $this->getResponse()->setException($e);
  321. }
  322. }
  323. }
  324. }
  325. }