File indexing completed on 2024-06-16 05:29:56

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  * @version    $Id$
0021  */
0022 
0023 /** Zend_Controller_Plugin_Abstract */
0024 // require_once 'Zend/Controller/Plugin/Abstract.php';
0025 
0026 /**
0027  * @category   Zend
0028  * @package    Zend_Controller
0029  * @subpackage Plugins
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Controller_Plugin_Broker extends Zend_Controller_Plugin_Abstract
0034 {
0035 
0036     /**
0037      * Array of instance of objects extending Zend_Controller_Plugin_Abstract
0038      *
0039      * @var array
0040      */
0041     protected $_plugins = array();
0042 
0043 
0044     /**
0045      * Register a plugin.
0046      *
0047      * @param  Zend_Controller_Plugin_Abstract $plugin
0048      * @param  int $stackIndex
0049      * @return Zend_Controller_Plugin_Broker
0050      */
0051     public function registerPlugin(Zend_Controller_Plugin_Abstract $plugin, $stackIndex = null)
0052     {
0053         if (false !== array_search($plugin, $this->_plugins, true)) {
0054             // require_once 'Zend/Controller/Exception.php';
0055             throw new Zend_Controller_Exception('Plugin already registered');
0056         }
0057 
0058         $stackIndex = (int) $stackIndex;
0059 
0060         if ($stackIndex) {
0061             if (isset($this->_plugins[$stackIndex])) {
0062                 // require_once 'Zend/Controller/Exception.php';
0063                 throw new Zend_Controller_Exception('Plugin with stackIndex "' . $stackIndex . '" already registered');
0064             }
0065             $this->_plugins[$stackIndex] = $plugin;
0066         } else {
0067             $stackIndex = count($this->_plugins);
0068             while (isset($this->_plugins[$stackIndex])) {
0069                 ++$stackIndex;
0070             }
0071             $this->_plugins[$stackIndex] = $plugin;
0072         }
0073 
0074         $request = $this->getRequest();
0075         if ($request) {
0076             $this->_plugins[$stackIndex]->setRequest($request);
0077         }
0078         $response = $this->getResponse();
0079         if ($response) {
0080             $this->_plugins[$stackIndex]->setResponse($response);
0081         }
0082 
0083         ksort($this->_plugins);
0084 
0085         return $this;
0086     }
0087 
0088     /**
0089      * Unregister a plugin.
0090      *
0091      * @param string|Zend_Controller_Plugin_Abstract $plugin Plugin object or class name
0092      * @return Zend_Controller_Plugin_Broker
0093      */
0094     public function unregisterPlugin($plugin)
0095     {
0096         if ($plugin instanceof Zend_Controller_Plugin_Abstract) {
0097             // Given a plugin object, find it in the array
0098             $key = array_search($plugin, $this->_plugins, true);
0099             if (false === $key) {
0100                 // require_once 'Zend/Controller/Exception.php';
0101                 throw new Zend_Controller_Exception('Plugin never registered.');
0102             }
0103             unset($this->_plugins[$key]);
0104         } elseif (is_string($plugin)) {
0105             // Given a plugin class, find all plugins of that class and unset them
0106             foreach ($this->_plugins as $key => $_plugin) {
0107                 $type = get_class($_plugin);
0108                 if ($plugin == $type) {
0109                     unset($this->_plugins[$key]);
0110                 }
0111             }
0112         }
0113         return $this;
0114     }
0115 
0116     /**
0117      * Is a plugin of a particular class registered?
0118      *
0119      * @param  string $class
0120      * @return bool
0121      */
0122     public function hasPlugin($class)
0123     {
0124         foreach ($this->_plugins as $plugin) {
0125             $type = get_class($plugin);
0126             if ($class == $type) {
0127                 return true;
0128             }
0129         }
0130 
0131         return false;
0132     }
0133 
0134     /**
0135      * Retrieve a plugin or plugins by class
0136      *
0137      * @param  string $class Class name of plugin(s) desired
0138      * @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
0139      */
0140     public function getPlugin($class)
0141     {
0142         $found = array();
0143         foreach ($this->_plugins as $plugin) {
0144             $type = get_class($plugin);
0145             if ($class == $type) {
0146                 $found[] = $plugin;
0147             }
0148         }
0149 
0150         switch (count($found)) {
0151             case 0:
0152                 return false;
0153             case 1:
0154                 return $found[0];
0155             default:
0156                 return $found;
0157         }
0158     }
0159 
0160     /**
0161      * Retrieve all plugins
0162      *
0163      * @return array
0164      */
0165     public function getPlugins()
0166     {
0167         return $this->_plugins;
0168     }
0169 
0170     /**
0171      * Set request object, and register with each plugin
0172      *
0173      * @param Zend_Controller_Request_Abstract $request
0174      * @return Zend_Controller_Plugin_Broker
0175      */
0176     public function setRequest(Zend_Controller_Request_Abstract $request)
0177     {
0178         $this->_request = $request;
0179 
0180         foreach ($this->_plugins as $plugin) {
0181             $plugin->setRequest($request);
0182         }
0183 
0184         return $this;
0185     }
0186 
0187     /**
0188      * Get request object
0189      *
0190      * @return Zend_Controller_Request_Abstract $request
0191      */
0192     public function getRequest()
0193     {
0194         return $this->_request;
0195     }
0196 
0197     /**
0198      * Set response object
0199      *
0200      * @param Zend_Controller_Response_Abstract $response
0201      * @return Zend_Controller_Plugin_Broker
0202      */
0203     public function setResponse(Zend_Controller_Response_Abstract $response)
0204     {
0205         $this->_response = $response;
0206 
0207         foreach ($this->_plugins as $plugin) {
0208             $plugin->setResponse($response);
0209         }
0210 
0211 
0212         return $this;
0213     }
0214 
0215     /**
0216      * Get response object
0217      *
0218      * @return Zend_Controller_Response_Abstract $response
0219      */
0220     public function getResponse()
0221     {
0222         return $this->_response;
0223     }
0224 
0225 
0226     /**
0227      * Called before Zend_Controller_Front begins evaluating the
0228      * request against its routes.
0229      *
0230      * @param Zend_Controller_Request_Abstract $request
0231      * @return void
0232      */
0233     public function routeStartup(Zend_Controller_Request_Abstract $request)
0234     {
0235         foreach ($this->_plugins as $plugin) {
0236             try {
0237                 $plugin->routeStartup($request);
0238             } catch (Exception $e) {
0239                 if (Zend_Controller_Front::getInstance()->throwExceptions()) {
0240                     throw new Zend_Controller_Exception($e->getMessage() . $e->getTraceAsString(), $e->getCode(), $e);
0241                 } else {
0242                     $this->getResponse()->setException($e);
0243                 }
0244             }
0245         }
0246     }
0247 
0248 
0249     /**
0250      * Called before Zend_Controller_Front exits its iterations over
0251      * the route set.
0252      *
0253      * @param  Zend_Controller_Request_Abstract $request
0254      * @return void
0255      */
0256     public function routeShutdown(Zend_Controller_Request_Abstract $request)
0257     {
0258         foreach ($this->_plugins as $plugin) {
0259             try {
0260                 $plugin->routeShutdown($request);
0261             } catch (Exception $e) {
0262                 if (Zend_Controller_Front::getInstance()->throwExceptions()) {
0263                     throw new Zend_Controller_Exception($e->getMessage() . $e->getTraceAsString(), $e->getCode(), $e);
0264                 } else {
0265                     $this->getResponse()->setException($e);
0266                 }
0267             }
0268         }
0269     }
0270 
0271 
0272     /**
0273      * Called before Zend_Controller_Front enters its dispatch loop.
0274      *
0275      * During the dispatch loop, Zend_Controller_Front keeps a
0276      * Zend_Controller_Request_Abstract object, and uses
0277      * Zend_Controller_Dispatcher to dispatch the
0278      * Zend_Controller_Request_Abstract object to controllers/actions.
0279      *
0280      * @param  Zend_Controller_Request_Abstract $request
0281      * @return void
0282      */
0283     public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
0284     {
0285         foreach ($this->_plugins as $plugin) {
0286             try {
0287                 $plugin->dispatchLoopStartup($request);
0288             } catch (Exception $e) {
0289                 if (Zend_Controller_Front::getInstance()->throwExceptions()) {
0290                     throw new Zend_Controller_Exception($e->getMessage() . $e->getTraceAsString(), $e->getCode(), $e);
0291                 } else {
0292                     $this->getResponse()->setException($e);
0293                 }
0294             }
0295         }
0296     }
0297 
0298 
0299     /**
0300      * Called before an action is dispatched by Zend_Controller_Dispatcher.
0301      *
0302      * @param  Zend_Controller_Request_Abstract $request
0303      * @return void
0304      */
0305     public function preDispatch(Zend_Controller_Request_Abstract $request)
0306     {
0307         foreach ($this->_plugins as $plugin) {
0308             try {
0309                 $plugin->preDispatch($request);
0310             } catch (Exception $e) {
0311                 if (Zend_Controller_Front::getInstance()->throwExceptions()) {
0312                     throw new Zend_Controller_Exception($e->getMessage() . $e->getTraceAsString(), $e->getCode(), $e);
0313                 } else {
0314                     $this->getResponse()->setException($e);
0315           // skip rendering of normal dispatch give the error handler a try
0316           $this->getRequest()->setDispatched(false);
0317                 }
0318             }
0319         }
0320     }
0321 
0322 
0323     /**
0324      * Called after an action is dispatched by Zend_Controller_Dispatcher.
0325      *
0326      * @param  Zend_Controller_Request_Abstract $request
0327      * @return void
0328      */
0329     public function postDispatch(Zend_Controller_Request_Abstract $request)
0330     {
0331         foreach ($this->_plugins as $plugin) {
0332             try {
0333                 $plugin->postDispatch($request);
0334             } catch (Exception $e) {
0335                 if (Zend_Controller_Front::getInstance()->throwExceptions()) {
0336                     throw new Zend_Controller_Exception($e->getMessage() . $e->getTraceAsString(), $e->getCode(), $e);
0337                 } else {
0338                     $this->getResponse()->setException($e);
0339                 }
0340             }
0341         }
0342     }
0343 
0344 
0345     /**
0346      * Called before Zend_Controller_Front exits its dispatch loop.
0347      *
0348      * @param  Zend_Controller_Request_Abstract $request
0349      * @return void
0350      */
0351     public function dispatchLoopShutdown()
0352     {
0353        foreach ($this->_plugins as $plugin) {
0354            try {
0355                 $plugin->dispatchLoopShutdown();
0356             } catch (Exception $e) {
0357                 if (Zend_Controller_Front::getInstance()->throwExceptions()) {
0358                     throw new Zend_Controller_Exception($e->getMessage() . $e->getTraceAsString(), $e->getCode(), $e);
0359                 } else {
0360                     $this->getResponse()->setException($e);
0361                 }
0362             }
0363        }
0364     }
0365 }