web/lib/Zend/Controller/Plugin/Broker.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  * @version    $Id: Broker.php 20255 2010-01-13 13:23:36Z matthew $
       
    21  */
       
    22 
       
    23 /** Zend_Controller_Plugin_Abstract */
       
    24 require_once 'Zend/Controller/Plugin/Abstract.php';
       
    25 
       
    26 /**
       
    27  * @category   Zend
       
    28  * @package    Zend_Controller
       
    29  * @subpackage Plugins
       
    30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    32  */
       
    33 class Zend_Controller_Plugin_Broker extends Zend_Controller_Plugin_Abstract
       
    34 {
       
    35 
       
    36     /**
       
    37      * Array of instance of objects extending Zend_Controller_Plugin_Abstract
       
    38      *
       
    39      * @var array
       
    40      */
       
    41     protected $_plugins = array();
       
    42 
       
    43 
       
    44     /**
       
    45      * Register a plugin.
       
    46      *
       
    47      * @param  Zend_Controller_Plugin_Abstract $plugin
       
    48      * @param  int $stackIndex
       
    49      * @return Zend_Controller_Plugin_Broker
       
    50      */
       
    51     public function registerPlugin(Zend_Controller_Plugin_Abstract $plugin, $stackIndex = null)
       
    52     {
       
    53         if (false !== array_search($plugin, $this->_plugins, true)) {
       
    54             require_once 'Zend/Controller/Exception.php';
       
    55             throw new Zend_Controller_Exception('Plugin already registered');
       
    56         }
       
    57 
       
    58         $stackIndex = (int) $stackIndex;
       
    59 
       
    60         if ($stackIndex) {
       
    61             if (isset($this->_plugins[$stackIndex])) {
       
    62                 require_once 'Zend/Controller/Exception.php';
       
    63                 throw new Zend_Controller_Exception('Plugin with stackIndex "' . $stackIndex . '" already registered');
       
    64             }
       
    65             $this->_plugins[$stackIndex] = $plugin;
       
    66         } else {
       
    67             $stackIndex = count($this->_plugins);
       
    68             while (isset($this->_plugins[$stackIndex])) {
       
    69                 ++$stackIndex;
       
    70             }
       
    71             $this->_plugins[$stackIndex] = $plugin;
       
    72         }
       
    73 
       
    74         $request = $this->getRequest();
       
    75         if ($request) {
       
    76             $this->_plugins[$stackIndex]->setRequest($request);
       
    77         }
       
    78         $response = $this->getResponse();
       
    79         if ($response) {
       
    80             $this->_plugins[$stackIndex]->setResponse($response);
       
    81         }
       
    82 
       
    83         ksort($this->_plugins);
       
    84 
       
    85         return $this;
       
    86     }
       
    87 
       
    88     /**
       
    89      * Unregister a plugin.
       
    90      *
       
    91      * @param string|Zend_Controller_Plugin_Abstract $plugin Plugin object or class name
       
    92      * @return Zend_Controller_Plugin_Broker
       
    93      */
       
    94     public function unregisterPlugin($plugin)
       
    95     {
       
    96         if ($plugin instanceof Zend_Controller_Plugin_Abstract) {
       
    97             // Given a plugin object, find it in the array
       
    98             $key = array_search($plugin, $this->_plugins, true);
       
    99             if (false === $key) {
       
   100                 require_once 'Zend/Controller/Exception.php';
       
   101                 throw new Zend_Controller_Exception('Plugin never registered.');
       
   102             }
       
   103             unset($this->_plugins[$key]);
       
   104         } elseif (is_string($plugin)) {
       
   105             // Given a plugin class, find all plugins of that class and unset them
       
   106             foreach ($this->_plugins as $key => $_plugin) {
       
   107                 $type = get_class($_plugin);
       
   108                 if ($plugin == $type) {
       
   109                     unset($this->_plugins[$key]);
       
   110                 }
       
   111             }
       
   112         }
       
   113         return $this;
       
   114     }
       
   115 
       
   116     /**
       
   117      * Is a plugin of a particular class registered?
       
   118      *
       
   119      * @param  string $class
       
   120      * @return bool
       
   121      */
       
   122     public function hasPlugin($class)
       
   123     {
       
   124         foreach ($this->_plugins as $plugin) {
       
   125             $type = get_class($plugin);
       
   126             if ($class == $type) {
       
   127                 return true;
       
   128             }
       
   129         }
       
   130 
       
   131         return false;
       
   132     }
       
   133 
       
   134     /**
       
   135      * Retrieve a plugin or plugins by class
       
   136      *
       
   137      * @param  string $class Class name of plugin(s) desired
       
   138      * @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
       
   139      */
       
   140     public function getPlugin($class)
       
   141     {
       
   142         $found = array();
       
   143         foreach ($this->_plugins as $plugin) {
       
   144             $type = get_class($plugin);
       
   145             if ($class == $type) {
       
   146                 $found[] = $plugin;
       
   147             }
       
   148         }
       
   149 
       
   150         switch (count($found)) {
       
   151             case 0:
       
   152                 return false;
       
   153             case 1:
       
   154                 return $found[0];
       
   155             default:
       
   156                 return $found;
       
   157         }
       
   158     }
       
   159 
       
   160     /**
       
   161      * Retrieve all plugins
       
   162      *
       
   163      * @return array
       
   164      */
       
   165     public function getPlugins()
       
   166     {
       
   167         return $this->_plugins;
       
   168     }
       
   169 
       
   170     /**
       
   171      * Set request object, and register with each plugin
       
   172      *
       
   173      * @param Zend_Controller_Request_Abstract $request
       
   174      * @return Zend_Controller_Plugin_Broker
       
   175      */
       
   176     public function setRequest(Zend_Controller_Request_Abstract $request)
       
   177     {
       
   178         $this->_request = $request;
       
   179 
       
   180         foreach ($this->_plugins as $plugin) {
       
   181             $plugin->setRequest($request);
       
   182         }
       
   183 
       
   184         return $this;
       
   185     }
       
   186 
       
   187     /**
       
   188      * Get request object
       
   189      *
       
   190      * @return Zend_Controller_Request_Abstract $request
       
   191      */
       
   192     public function getRequest()
       
   193     {
       
   194         return $this->_request;
       
   195     }
       
   196 
       
   197     /**
       
   198      * Set response object
       
   199      *
       
   200      * @param Zend_Controller_Response_Abstract $response
       
   201      * @return Zend_Controller_Plugin_Broker
       
   202      */
       
   203     public function setResponse(Zend_Controller_Response_Abstract $response)
       
   204     {
       
   205         $this->_response = $response;
       
   206 
       
   207         foreach ($this->_plugins as $plugin) {
       
   208             $plugin->setResponse($response);
       
   209         }
       
   210 
       
   211 
       
   212         return $this;
       
   213     }
       
   214 
       
   215     /**
       
   216      * Get response object
       
   217      *
       
   218      * @return Zend_Controller_Response_Abstract $response
       
   219      */
       
   220     public function getResponse()
       
   221     {
       
   222         return $this->_response;
       
   223     }
       
   224 
       
   225 
       
   226     /**
       
   227      * Called before Zend_Controller_Front begins evaluating the
       
   228      * request against its routes.
       
   229      *
       
   230      * @param Zend_Controller_Request_Abstract $request
       
   231      * @return void
       
   232      */
       
   233     public function routeStartup(Zend_Controller_Request_Abstract $request)
       
   234     {
       
   235         foreach ($this->_plugins as $plugin) {
       
   236             try {
       
   237                 $plugin->routeStartup($request);
       
   238             } catch (Exception $e) {
       
   239                 if (Zend_Controller_Front::getInstance()->throwExceptions()) {
       
   240                     throw $e;
       
   241                 } else {
       
   242                     $this->getResponse()->setException($e);
       
   243                 }
       
   244             }
       
   245         }
       
   246     }
       
   247 
       
   248 
       
   249     /**
       
   250      * Called before Zend_Controller_Front exits its iterations over
       
   251      * the route set.
       
   252      *
       
   253      * @param  Zend_Controller_Request_Abstract $request
       
   254      * @return void
       
   255      */
       
   256     public function routeShutdown(Zend_Controller_Request_Abstract $request)
       
   257     {
       
   258         foreach ($this->_plugins as $plugin) {
       
   259             try {
       
   260                 $plugin->routeShutdown($request);
       
   261             } catch (Exception $e) {
       
   262                 if (Zend_Controller_Front::getInstance()->throwExceptions()) {
       
   263                     throw $e;
       
   264                 } else {
       
   265                     $this->getResponse()->setException($e);
       
   266                 }
       
   267             }
       
   268         }
       
   269     }
       
   270 
       
   271 
       
   272     /**
       
   273      * Called before Zend_Controller_Front enters its dispatch loop.
       
   274      *
       
   275      * During the dispatch loop, Zend_Controller_Front keeps a
       
   276      * Zend_Controller_Request_Abstract object, and uses
       
   277      * Zend_Controller_Dispatcher to dispatch the
       
   278      * Zend_Controller_Request_Abstract object to controllers/actions.
       
   279      *
       
   280      * @param  Zend_Controller_Request_Abstract $request
       
   281      * @return void
       
   282      */
       
   283     public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
       
   284     {
       
   285         foreach ($this->_plugins as $plugin) {
       
   286             try {
       
   287                 $plugin->dispatchLoopStartup($request);
       
   288             } catch (Exception $e) {
       
   289                 if (Zend_Controller_Front::getInstance()->throwExceptions()) {
       
   290                     throw $e;
       
   291                 } else {
       
   292                     $this->getResponse()->setException($e);
       
   293                 }
       
   294             }
       
   295         }
       
   296     }
       
   297 
       
   298 
       
   299     /**
       
   300      * Called before an action is dispatched by Zend_Controller_Dispatcher.
       
   301      *
       
   302      * @param  Zend_Controller_Request_Abstract $request
       
   303      * @return void
       
   304      */
       
   305     public function preDispatch(Zend_Controller_Request_Abstract $request)
       
   306     {
       
   307         foreach ($this->_plugins as $plugin) {
       
   308             try {
       
   309                 $plugin->preDispatch($request);
       
   310             } catch (Exception $e) {
       
   311                 if (Zend_Controller_Front::getInstance()->throwExceptions()) {
       
   312                     throw $e;
       
   313                 } else {
       
   314                     $this->getResponse()->setException($e);
       
   315                 }
       
   316             }
       
   317         }
       
   318     }
       
   319 
       
   320 
       
   321     /**
       
   322      * Called after an action is dispatched by Zend_Controller_Dispatcher.
       
   323      *
       
   324      * @param  Zend_Controller_Request_Abstract $request
       
   325      * @return void
       
   326      */
       
   327     public function postDispatch(Zend_Controller_Request_Abstract $request)
       
   328     {
       
   329         foreach ($this->_plugins as $plugin) {
       
   330             try {
       
   331                 $plugin->postDispatch($request);
       
   332             } catch (Exception $e) {
       
   333                 if (Zend_Controller_Front::getInstance()->throwExceptions()) {
       
   334                     throw $e;
       
   335                 } else {
       
   336                     $this->getResponse()->setException($e);
       
   337                 }
       
   338             }
       
   339         }
       
   340     }
       
   341 
       
   342 
       
   343     /**
       
   344      * Called before Zend_Controller_Front exits its dispatch loop.
       
   345      *
       
   346      * @param  Zend_Controller_Request_Abstract $request
       
   347      * @return void
       
   348      */
       
   349     public function dispatchLoopShutdown()
       
   350     {
       
   351        foreach ($this->_plugins as $plugin) {
       
   352            try {
       
   353                 $plugin->dispatchLoopShutdown();
       
   354             } catch (Exception $e) {
       
   355                 if (Zend_Controller_Front::getInstance()->throwExceptions()) {
       
   356                     throw $e;
       
   357                 } else {
       
   358                     $this->getResponse()->setException($e);
       
   359                 }
       
   360             }
       
   361        }
       
   362     }
       
   363 }