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

/tine20/library/Zend/Controller/Plugin/ActionStack.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 245 lines | 114 code | 25 blank | 106 comment | 12 complexity | dd418f8ec3d16fa481563914c1c109f2 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-2009 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-2009 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 10020 2009-08-18 14:34:09Z j.fischer@metaways.de $
  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. * Constructor
  57. *
  58. * @param Zend_Registry $registry
  59. * @param string $key
  60. * @return void
  61. */
  62. public function __construct(Zend_Registry $registry = null, $key = null)
  63. {
  64. if (null === $registry) {
  65. $registry = Zend_Registry::getInstance();
  66. }
  67. $this->setRegistry($registry);
  68. if (null !== $key) {
  69. $this->setRegistryKey($key);
  70. } else {
  71. $key = $this->getRegistryKey();
  72. }
  73. $registry[$key] = array();
  74. }
  75. /**
  76. * Set registry object
  77. *
  78. * @param Zend_Registry $registry
  79. * @return Zend_Controller_Plugin_ActionStack
  80. */
  81. public function setRegistry(Zend_Registry $registry)
  82. {
  83. $this->_registry = $registry;
  84. return $this;
  85. }
  86. /**
  87. * Retrieve registry object
  88. *
  89. * @return Zend_Registry
  90. */
  91. public function getRegistry()
  92. {
  93. return $this->_registry;
  94. }
  95. /**
  96. * Retrieve registry key
  97. *
  98. * @return string
  99. */
  100. public function getRegistryKey()
  101. {
  102. return $this->_registryKey;
  103. }
  104. /**
  105. * Set registry key
  106. *
  107. * @param string $key
  108. * @return Zend_Controller_Plugin_ActionStack
  109. */
  110. public function setRegistryKey($key)
  111. {
  112. $this->_registryKey = (string) $key;
  113. return $this;
  114. }
  115. /**
  116. * Retrieve action stack
  117. *
  118. * @return array
  119. */
  120. public function getStack()
  121. {
  122. $registry = $this->getRegistry();
  123. $stack = $registry[$this->getRegistryKey()];
  124. return $stack;
  125. }
  126. /**
  127. * Save stack to registry
  128. *
  129. * @param array $stack
  130. * @return Zend_Controller_Plugin_ActionStack
  131. */
  132. protected function _saveStack(array $stack)
  133. {
  134. $registry = $this->getRegistry();
  135. $registry[$this->getRegistryKey()] = $stack;
  136. return $this;
  137. }
  138. /**
  139. * Push an item onto the stack
  140. *
  141. * @param Zend_Controller_Request_Abstract $next
  142. * @return Zend_Controller_Plugin_ActionStack
  143. */
  144. public function pushStack(Zend_Controller_Request_Abstract $next)
  145. {
  146. $stack = $this->getStack();
  147. array_push($stack, $next);
  148. return $this->_saveStack($stack);
  149. }
  150. /**
  151. * Pop an item off the action stack
  152. *
  153. * @return false|Zend_Controller_Request_Abstract
  154. */
  155. public function popStack()
  156. {
  157. $stack = $this->getStack();
  158. if (0 == count($stack)) {
  159. return false;
  160. }
  161. $next = array_pop($stack);
  162. $this->_saveStack($stack);
  163. if (!$next instanceof Zend_Controller_Request_Abstract) {
  164. require_once 'Zend/Controller/Exception.php';
  165. throw new Zend_Controller_Exception('ArrayStack should only contain request objects');
  166. }
  167. $action = $next->getActionName();
  168. if (empty($action)) {
  169. return $this->popStack($stack);
  170. }
  171. $request = $this->getRequest();
  172. $controller = $next->getControllerName();
  173. if (empty($controller)) {
  174. $next->setControllerName($request->getControllerName());
  175. }
  176. $module = $next->getModuleName();
  177. if (empty($module)) {
  178. $next->setModuleName($request->getModuleName());
  179. }
  180. return $next;
  181. }
  182. /**
  183. * postDispatch() plugin hook -- check for actions in stack, and dispatch if any found
  184. *
  185. * @param Zend_Controller_Request_Abstract $request
  186. * @return void
  187. */
  188. public function postDispatch(Zend_Controller_Request_Abstract $request)
  189. {
  190. // Don't move on to next request if this is already an attempt to
  191. // forward
  192. if (!$request->isDispatched()) {
  193. return;
  194. }
  195. $this->setRequest($request);
  196. $stack = $this->getStack();
  197. if (empty($stack)) {
  198. return;
  199. }
  200. $next = $this->popStack();
  201. if (!$next) {
  202. return;
  203. }
  204. $this->forward($next);
  205. }
  206. /**
  207. * Forward request with next action
  208. *
  209. * @param array $next
  210. * @return void
  211. */
  212. public function forward(Zend_Controller_Request_Abstract $next)
  213. {
  214. $this->getRequest()->setModuleName($next->getModuleName())
  215. ->setControllerName($next->getControllerName())
  216. ->setActionName($next->getActionName())
  217. ->setParams($next->getParams())
  218. ->setDispatched(false);
  219. }
  220. }