PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/libraries/Zend/Controller/Plugin/ActionStack.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 280 lines | 128 code | 29 blank | 123 comment | 13 complexity | e7dfce952b045188b01fd5140bcaa937 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-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /** Zend_Controller_Plugin_Abstract */
  22. require_once 'Zend/Controller/Plugin/Abstract.php';
  23. /** Zend_Registry */
  24. require_once 'Zend/Registry.php';
  25. /**
  26. * Manage a stack of actions
  27. *
  28. * @uses Zend_Controller_Plugin_Abstract
  29. * @category Zend
  30. * @package Zend_Controller
  31. * @subpackage Plugins
  32. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. * @version $Id: ActionStack.php 23775 2011-03-01 17:25:24Z ralph $
  35. */
  36. class Zend_Controller_Plugin_ActionStack extends Zend_Controller_Plugin_Abstract
  37. {
  38. /** @var Zend_Registry */
  39. protected $_registry;
  40. /**
  41. * Registry key under which actions are stored
  42. * @var string
  43. */
  44. protected $_registryKey = 'Zend_Controller_Plugin_ActionStack';
  45. /**
  46. * Valid keys for stack items
  47. * @var array
  48. */
  49. protected $_validKeys = array(
  50. 'module',
  51. 'controller',
  52. 'action',
  53. 'params'
  54. );
  55. /**
  56. * Flag to determine whether request parameters are cleared between actions, or whether new parameters
  57. * are added to existing request parameters.
  58. *
  59. * @var Bool
  60. */
  61. protected $_clearRequestParams = false;
  62. /**
  63. * Constructor
  64. *
  65. * @param Zend_Registry $registry
  66. * @param string $key
  67. * @return void
  68. */
  69. public function __construct(Zend_Registry $registry = null, $key = null)
  70. {
  71. if (null === $registry) {
  72. $registry = Zend_Registry::getInstance();
  73. }
  74. $this->setRegistry($registry);
  75. if (null !== $key) {
  76. $this->setRegistryKey($key);
  77. } else {
  78. $key = $this->getRegistryKey();
  79. }
  80. $registry[$key] = array();
  81. }
  82. /**
  83. * Set registry object
  84. *
  85. * @param Zend_Registry $registry
  86. * @return Zend_Controller_Plugin_ActionStack
  87. */
  88. public function setRegistry(Zend_Registry $registry)
  89. {
  90. $this->_registry = $registry;
  91. return $this;
  92. }
  93. /**
  94. * Retrieve registry object
  95. *
  96. * @return Zend_Registry
  97. */
  98. public function getRegistry()
  99. {
  100. return $this->_registry;
  101. }
  102. /**
  103. * Retrieve registry key
  104. *
  105. * @return string
  106. */
  107. public function getRegistryKey()
  108. {
  109. return $this->_registryKey;
  110. }
  111. /**
  112. * Set registry key
  113. *
  114. * @param string $key
  115. * @return Zend_Controller_Plugin_ActionStack
  116. */
  117. public function setRegistryKey($key)
  118. {
  119. $this->_registryKey = (string) $key;
  120. return $this;
  121. }
  122. /**
  123. * Set clearRequestParams flag
  124. *
  125. * @param bool $clearRequestParams
  126. * @return Zend_Controller_Plugin_ActionStack
  127. */
  128. public function setClearRequestParams($clearRequestParams)
  129. {
  130. $this->_clearRequestParams = (bool) $clearRequestParams;
  131. return $this;
  132. }
  133. /**
  134. * Retrieve clearRequestParams flag
  135. *
  136. * @return bool
  137. */
  138. public function getClearRequestParams()
  139. {
  140. return $this->_clearRequestParams;
  141. }
  142. /**
  143. * Retrieve action stack
  144. *
  145. * @return array
  146. */
  147. public function getStack()
  148. {
  149. $registry = $this->getRegistry();
  150. $stack = $registry[$this->getRegistryKey()];
  151. return $stack;
  152. }
  153. /**
  154. * Save stack to registry
  155. *
  156. * @param array $stack
  157. * @return Zend_Controller_Plugin_ActionStack
  158. */
  159. protected function _saveStack(array $stack)
  160. {
  161. $registry = $this->getRegistry();
  162. $registry[$this->getRegistryKey()] = $stack;
  163. return $this;
  164. }
  165. /**
  166. * Push an item onto the stack
  167. *
  168. * @param Zend_Controller_Request_Abstract $next
  169. * @return Zend_Controller_Plugin_ActionStack
  170. */
  171. public function pushStack(Zend_Controller_Request_Abstract $next)
  172. {
  173. $stack = $this->getStack();
  174. array_push($stack, $next);
  175. return $this->_saveStack($stack);
  176. }
  177. /**
  178. * Pop an item off the action stack
  179. *
  180. * @return false|Zend_Controller_Request_Abstract
  181. */
  182. public function popStack()
  183. {
  184. $stack = $this->getStack();
  185. if (0 == count($stack)) {
  186. return false;
  187. }
  188. $next = array_pop($stack);
  189. $this->_saveStack($stack);
  190. if (!$next instanceof Zend_Controller_Request_Abstract) {
  191. require_once 'Zend/Controller/Exception.php';
  192. throw new Zend_Controller_Exception('ArrayStack should only contain request objects');
  193. }
  194. $action = $next->getActionName();
  195. if (empty($action)) {
  196. return $this->popStack($stack);
  197. }
  198. $request = $this->getRequest();
  199. $controller = $next->getControllerName();
  200. if (empty($controller)) {
  201. $next->setControllerName($request->getControllerName());
  202. }
  203. $module = $next->getModuleName();
  204. if (empty($module)) {
  205. $next->setModuleName($request->getModuleName());
  206. }
  207. return $next;
  208. }
  209. /**
  210. * postDispatch() plugin hook -- check for actions in stack, and dispatch if any found
  211. *
  212. * @param Zend_Controller_Request_Abstract $request
  213. * @return void
  214. */
  215. public function postDispatch(Zend_Controller_Request_Abstract $request)
  216. {
  217. // Don't move on to next request if this is already an attempt to
  218. // forward
  219. if (!$request->isDispatched()) {
  220. return;
  221. }
  222. $this->setRequest($request);
  223. $stack = $this->getStack();
  224. if (empty($stack)) {
  225. return;
  226. }
  227. $next = $this->popStack();
  228. if (!$next) {
  229. return;
  230. }
  231. $this->forward($next);
  232. }
  233. /**
  234. * Forward request with next action
  235. *
  236. * @param array $next
  237. * @return void
  238. */
  239. public function forward(Zend_Controller_Request_Abstract $next)
  240. {
  241. $request = $this->getRequest();
  242. if ($this->getClearRequestParams()) {
  243. $request->clearParams();
  244. }
  245. $request->setModuleName($next->getModuleName())
  246. ->setControllerName($next->getControllerName())
  247. ->setActionName($next->getActionName())
  248. ->setParams($next->getParams())
  249. ->setDispatched(false);
  250. }
  251. }