web/lib/Zend/Controller/Plugin/ActionStack.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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  */
       
    21 
       
    22 /** Zend_Controller_Plugin_Abstract */
       
    23 require_once 'Zend/Controller/Plugin/Abstract.php';
       
    24 
       
    25 /** Zend_Registry */
       
    26 require_once 'Zend/Registry.php';
       
    27 
       
    28 /**
       
    29  * Manage a stack of actions
       
    30  *
       
    31  * @uses       Zend_Controller_Plugin_Abstract
       
    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  * @version    $Id: ActionStack.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    38  */
       
    39 class Zend_Controller_Plugin_ActionStack extends Zend_Controller_Plugin_Abstract
       
    40 {
       
    41     /** @var Zend_Registry */
       
    42     protected $_registry;
       
    43 
       
    44     /**
       
    45      * Registry key under which actions are stored
       
    46      * @var string
       
    47      */
       
    48     protected $_registryKey = 'Zend_Controller_Plugin_ActionStack';
       
    49 
       
    50     /**
       
    51      * Valid keys for stack items
       
    52      * @var array
       
    53      */
       
    54     protected $_validKeys = array(
       
    55         'module',
       
    56         'controller',
       
    57         'action',
       
    58         'params'
       
    59     );
       
    60 
       
    61     /**
       
    62      * Flag to determine whether request parameters are cleared between actions, or whether new parameters
       
    63      * are added to existing request parameters.
       
    64      *
       
    65      * @var Bool
       
    66      */
       
    67     protected $_clearRequestParams = false;
       
    68 
       
    69     /**
       
    70      * Constructor
       
    71      *
       
    72      * @param  Zend_Registry $registry
       
    73      * @param  string $key
       
    74      * @return void
       
    75      */
       
    76     public function __construct(Zend_Registry $registry = null, $key = null)
       
    77     {
       
    78         if (null === $registry) {
       
    79             $registry = Zend_Registry::getInstance();
       
    80         }
       
    81         $this->setRegistry($registry);
       
    82 
       
    83         if (null !== $key) {
       
    84             $this->setRegistryKey($key);
       
    85         } else {
       
    86             $key = $this->getRegistryKey();
       
    87         }
       
    88 
       
    89         $registry[$key] = array();
       
    90     }
       
    91 
       
    92     /**
       
    93      * Set registry object
       
    94      *
       
    95      * @param  Zend_Registry $registry
       
    96      * @return Zend_Controller_Plugin_ActionStack
       
    97      */
       
    98     public function setRegistry(Zend_Registry $registry)
       
    99     {
       
   100         $this->_registry = $registry;
       
   101         return $this;
       
   102     }
       
   103 
       
   104     /**
       
   105      * Retrieve registry object
       
   106      *
       
   107      * @return Zend_Registry
       
   108      */
       
   109     public function getRegistry()
       
   110     {
       
   111         return $this->_registry;
       
   112     }
       
   113 
       
   114     /**
       
   115      * Retrieve registry key
       
   116      *
       
   117      * @return string
       
   118      */
       
   119     public function getRegistryKey()
       
   120     {
       
   121         return $this->_registryKey;
       
   122     }
       
   123 
       
   124     /**
       
   125      * Set registry key
       
   126      *
       
   127      * @param  string $key
       
   128      * @return Zend_Controller_Plugin_ActionStack
       
   129      */
       
   130     public function setRegistryKey($key)
       
   131     {
       
   132         $this->_registryKey = (string) $key;
       
   133         return $this;
       
   134     }
       
   135 
       
   136     /**
       
   137      *  Set clearRequestParams flag
       
   138      *
       
   139      *  @param  bool $clearRequestParams
       
   140      *  @return Zend_Controller_Plugin_ActionStack
       
   141      */
       
   142     public function setClearRequestParams($clearRequestParams)
       
   143     {
       
   144         $this->_clearRequestParams = (bool) $clearRequestParams;
       
   145         return $this;
       
   146     }
       
   147 
       
   148     /**
       
   149      * Retrieve clearRequestParams flag
       
   150      *
       
   151      * @return bool
       
   152      */
       
   153     public function getClearRequestParams()
       
   154     {
       
   155         return $this->_clearRequestParams;
       
   156     }
       
   157 
       
   158     /**
       
   159      * Retrieve action stack
       
   160      *
       
   161      * @return array
       
   162      */
       
   163     public function getStack()
       
   164     {
       
   165         $registry = $this->getRegistry();
       
   166         $stack    = $registry[$this->getRegistryKey()];
       
   167         return $stack;
       
   168     }
       
   169 
       
   170     /**
       
   171      * Save stack to registry
       
   172      *
       
   173      * @param  array $stack
       
   174      * @return Zend_Controller_Plugin_ActionStack
       
   175      */
       
   176     protected function _saveStack(array $stack)
       
   177     {
       
   178         $registry = $this->getRegistry();
       
   179         $registry[$this->getRegistryKey()] = $stack;
       
   180         return $this;
       
   181     }
       
   182 
       
   183     /**
       
   184      * Push an item onto the stack
       
   185      *
       
   186      * @param  Zend_Controller_Request_Abstract $next
       
   187      * @return Zend_Controller_Plugin_ActionStack
       
   188      */
       
   189     public function pushStack(Zend_Controller_Request_Abstract $next)
       
   190     {
       
   191         $stack = $this->getStack();
       
   192         array_push($stack, $next);
       
   193         return $this->_saveStack($stack);
       
   194     }
       
   195 
       
   196     /**
       
   197      * Pop an item off the action stack
       
   198      *
       
   199      * @return false|Zend_Controller_Request_Abstract
       
   200      */
       
   201     public function popStack()
       
   202     {
       
   203         $stack = $this->getStack();
       
   204         if (0 == count($stack)) {
       
   205             return false;
       
   206         }
       
   207 
       
   208         $next = array_pop($stack);
       
   209         $this->_saveStack($stack);
       
   210 
       
   211         if (!$next instanceof Zend_Controller_Request_Abstract) {
       
   212             require_once 'Zend/Controller/Exception.php';
       
   213             throw new Zend_Controller_Exception('ArrayStack should only contain request objects');
       
   214         }
       
   215         $action = $next->getActionName();
       
   216         if (empty($action)) {
       
   217             return $this->popStack($stack);
       
   218         }
       
   219 
       
   220         $request    = $this->getRequest();
       
   221         $controller = $next->getControllerName();
       
   222         if (empty($controller)) {
       
   223             $next->setControllerName($request->getControllerName());
       
   224         }
       
   225 
       
   226         $module = $next->getModuleName();
       
   227         if (empty($module)) {
       
   228             $next->setModuleName($request->getModuleName());
       
   229         }
       
   230 
       
   231         return $next;
       
   232     }
       
   233 
       
   234     /**
       
   235      * postDispatch() plugin hook -- check for actions in stack, and dispatch if any found
       
   236      *
       
   237      * @param  Zend_Controller_Request_Abstract $request
       
   238      * @return void
       
   239      */
       
   240     public function postDispatch(Zend_Controller_Request_Abstract $request)
       
   241     {
       
   242         // Don't move on to next request if this is already an attempt to
       
   243         // forward
       
   244         if (!$request->isDispatched()) {
       
   245             return;
       
   246         }
       
   247 
       
   248         $this->setRequest($request);
       
   249         $stack = $this->getStack();
       
   250         if (empty($stack)) {
       
   251             return;
       
   252         }
       
   253         $next = $this->popStack();
       
   254         if (!$next) {
       
   255             return;
       
   256         }
       
   257 
       
   258         $this->forward($next);
       
   259     }
       
   260 
       
   261     /**
       
   262      * Forward request with next action
       
   263      *
       
   264      * @param  array $next
       
   265      * @return void
       
   266      */
       
   267     public function forward(Zend_Controller_Request_Abstract $next)
       
   268     {
       
   269         $request = $this->getRequest();
       
   270         if ($this->getClearRequestParams()) {
       
   271             $request->clearParams();
       
   272         }
       
   273 
       
   274         $request->setModuleName($next->getModuleName())
       
   275                 ->setControllerName($next->getControllerName())
       
   276                 ->setActionName($next->getActionName())
       
   277                 ->setParams($next->getParams())
       
   278                 ->setDispatched(false);
       
   279     }
       
   280 }