/Zend/Controller/Plugin/Broker.php

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