/library/Zend/Controller/Plugin/Broker.php

https://github.com/Shreef/zf2 · PHP · 367 lines · 196 code · 37 blank · 134 comment · 28 complexity · a024a262b1e10bc9a905e1161dd50775 MD5 · raw file

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