2
0

Broker.php 10 KB

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