PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Controller/Plugin/Broker.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 365 lines | 193 code | 37 blank | 135 comment | 28 complexity | 4558d24e01418e1a7a4a45f8a0419b05 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-2015 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-2015 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 new Zend_Controller_Exception($e->getMessage() . $e->getTraceAsString(), $e->getCode(), $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 new Zend_Controller_Exception($e->getMessage() . $e->getTraceAsString(), $e->getCode(), $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 new Zend_Controller_Exception($e->getMessage() . $e->getTraceAsString(), $e->getCode(), $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 new Zend_Controller_Exception($e->getMessage() . $e->getTraceAsString(), $e->getCode(), $e);
  280. } else {
  281. $this->getResponse()->setException($e);
  282. // skip rendering of normal dispatch give the error handler a try
  283. $this->getRequest()->setDispatched(false);
  284. }
  285. }
  286. }
  287. }
  288. /**
  289. * Called after an action is dispatched by Zend_Controller_Dispatcher.
  290. *
  291. * @param Zend_Controller_Request_Abstract $request
  292. * @return void
  293. */
  294. public function postDispatch(Zend_Controller_Request_Abstract $request)
  295. {
  296. foreach ($this->_plugins as $plugin) {
  297. try {
  298. $plugin->postDispatch($request);
  299. } catch (Exception $e) {
  300. if (Zend_Controller_Front::getInstance()->throwExceptions()) {
  301. throw new Zend_Controller_Exception($e->getMessage() . $e->getTraceAsString(), $e->getCode(), $e);
  302. } else {
  303. $this->getResponse()->setException($e);
  304. }
  305. }
  306. }
  307. }
  308. /**
  309. * Called before Zend_Controller_Front exits its dispatch loop.
  310. *
  311. * @param Zend_Controller_Request_Abstract $request
  312. * @return void
  313. */
  314. public function dispatchLoopShutdown()
  315. {
  316. foreach ($this->_plugins as $plugin) {
  317. try {
  318. $plugin->dispatchLoopShutdown();
  319. } catch (Exception $e) {
  320. if (Zend_Controller_Front::getInstance()->throwExceptions()) {
  321. throw new Zend_Controller_Exception($e->getMessage() . $e->getTraceAsString(), $e->getCode(), $e);
  322. } else {
  323. $this->getResponse()->setException($e);
  324. }
  325. }
  326. }
  327. }
  328. }