File indexing completed on 2024-12-22 05:36:33
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Controller 0017 * @subpackage Plugins 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 */ 0021 0022 /** Zend_Controller_Plugin_Abstract */ 0023 // require_once 'Zend/Controller/Plugin/Abstract.php'; 0024 0025 /** Zend_Registry */ 0026 // require_once 'Zend/Registry.php'; 0027 0028 /** 0029 * Manage a stack of actions 0030 * 0031 * @uses Zend_Controller_Plugin_Abstract 0032 * @category Zend 0033 * @package Zend_Controller 0034 * @subpackage Plugins 0035 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0036 * @license http://framework.zend.com/license/new-bsd New BSD License 0037 * @version $Id$ 0038 */ 0039 class Zend_Controller_Plugin_ActionStack extends Zend_Controller_Plugin_Abstract 0040 { 0041 /** @var Zend_Registry */ 0042 protected $_registry; 0043 0044 /** 0045 * Registry key under which actions are stored 0046 * @var string 0047 */ 0048 protected $_registryKey = 'Zend_Controller_Plugin_ActionStack'; 0049 0050 /** 0051 * Valid keys for stack items 0052 * @var array 0053 */ 0054 protected $_validKeys = array( 0055 'module', 0056 'controller', 0057 'action', 0058 'params' 0059 ); 0060 0061 /** 0062 * Flag to determine whether request parameters are cleared between actions, or whether new parameters 0063 * are added to existing request parameters. 0064 * 0065 * @var Bool 0066 */ 0067 protected $_clearRequestParams = false; 0068 0069 /** 0070 * Constructor 0071 * 0072 * @param Zend_Registry $registry 0073 * @param string $key 0074 * @return void 0075 */ 0076 public function __construct(Zend_Registry $registry = null, $key = null) 0077 { 0078 if (null === $registry) { 0079 $registry = Zend_Registry::getInstance(); 0080 } 0081 $this->setRegistry($registry); 0082 0083 if (null !== $key) { 0084 $this->setRegistryKey($key); 0085 } else { 0086 $key = $this->getRegistryKey(); 0087 } 0088 0089 $registry[$key] = array(); 0090 } 0091 0092 /** 0093 * Set registry object 0094 * 0095 * @param Zend_Registry $registry 0096 * @return Zend_Controller_Plugin_ActionStack 0097 */ 0098 public function setRegistry(Zend_Registry $registry) 0099 { 0100 $this->_registry = $registry; 0101 return $this; 0102 } 0103 0104 /** 0105 * Retrieve registry object 0106 * 0107 * @return Zend_Registry 0108 */ 0109 public function getRegistry() 0110 { 0111 return $this->_registry; 0112 } 0113 0114 /** 0115 * Retrieve registry key 0116 * 0117 * @return string 0118 */ 0119 public function getRegistryKey() 0120 { 0121 return $this->_registryKey; 0122 } 0123 0124 /** 0125 * Set registry key 0126 * 0127 * @param string $key 0128 * @return Zend_Controller_Plugin_ActionStack 0129 */ 0130 public function setRegistryKey($key) 0131 { 0132 $this->_registryKey = (string) $key; 0133 return $this; 0134 } 0135 0136 /** 0137 * Set clearRequestParams flag 0138 * 0139 * @param bool $clearRequestParams 0140 * @return Zend_Controller_Plugin_ActionStack 0141 */ 0142 public function setClearRequestParams($clearRequestParams) 0143 { 0144 $this->_clearRequestParams = (bool) $clearRequestParams; 0145 return $this; 0146 } 0147 0148 /** 0149 * Retrieve clearRequestParams flag 0150 * 0151 * @return bool 0152 */ 0153 public function getClearRequestParams() 0154 { 0155 return $this->_clearRequestParams; 0156 } 0157 0158 /** 0159 * Retrieve action stack 0160 * 0161 * @return array 0162 */ 0163 public function getStack() 0164 { 0165 $registry = $this->getRegistry(); 0166 $stack = $registry[$this->getRegistryKey()]; 0167 return $stack; 0168 } 0169 0170 /** 0171 * Save stack to registry 0172 * 0173 * @param array $stack 0174 * @return Zend_Controller_Plugin_ActionStack 0175 */ 0176 protected function _saveStack(array $stack) 0177 { 0178 $registry = $this->getRegistry(); 0179 $registry[$this->getRegistryKey()] = $stack; 0180 return $this; 0181 } 0182 0183 /** 0184 * Push an item onto the stack 0185 * 0186 * @param Zend_Controller_Request_Abstract $next 0187 * @return Zend_Controller_Plugin_ActionStack 0188 */ 0189 public function pushStack(Zend_Controller_Request_Abstract $next) 0190 { 0191 $stack = $this->getStack(); 0192 array_push($stack, $next); 0193 return $this->_saveStack($stack); 0194 } 0195 0196 /** 0197 * Pop an item off the action stack 0198 * 0199 * @return false|Zend_Controller_Request_Abstract 0200 */ 0201 public function popStack() 0202 { 0203 $stack = $this->getStack(); 0204 if (0 == count($stack)) { 0205 return false; 0206 } 0207 0208 $next = array_pop($stack); 0209 $this->_saveStack($stack); 0210 0211 if (!$next instanceof Zend_Controller_Request_Abstract) { 0212 // require_once 'Zend/Controller/Exception.php'; 0213 throw new Zend_Controller_Exception('ArrayStack should only contain request objects'); 0214 } 0215 $action = $next->getActionName(); 0216 if (empty($action)) { 0217 return $this->popStack($stack); 0218 } 0219 0220 $request = $this->getRequest(); 0221 $controller = $next->getControllerName(); 0222 if (empty($controller)) { 0223 $next->setControllerName($request->getControllerName()); 0224 } 0225 0226 $module = $next->getModuleName(); 0227 if (empty($module)) { 0228 $next->setModuleName($request->getModuleName()); 0229 } 0230 0231 return $next; 0232 } 0233 0234 /** 0235 * postDispatch() plugin hook -- check for actions in stack, and dispatch if any found 0236 * 0237 * @param Zend_Controller_Request_Abstract $request 0238 * @return void 0239 */ 0240 public function postDispatch(Zend_Controller_Request_Abstract $request) 0241 { 0242 // Don't move on to next request if this is already an attempt to 0243 // forward 0244 if (!$request->isDispatched()) { 0245 return; 0246 } 0247 0248 $this->setRequest($request); 0249 $stack = $this->getStack(); 0250 if (empty($stack)) { 0251 return; 0252 } 0253 $next = $this->popStack(); 0254 if (!$next) { 0255 return; 0256 } 0257 0258 $this->forward($next); 0259 } 0260 0261 /** 0262 * Forward request with next action 0263 * 0264 * @param array $next 0265 * @return void 0266 */ 0267 public function forward(Zend_Controller_Request_Abstract $next) 0268 { 0269 $request = $this->getRequest(); 0270 if ($this->getClearRequestParams()) { 0271 $request->clearParams(); 0272 } 0273 0274 $request->setModuleName($next->getModuleName()) 0275 ->setControllerName($next->getControllerName()) 0276 ->setActionName($next->getActionName()) 0277 ->setParams($next->getParams()) 0278 ->setDispatched(false); 0279 } 0280 }