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

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