File indexing completed on 2024-05-12 06:02:23

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  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 
0023 /** Zend_Loader */
0024 // require_once 'Zend/Loader.php';
0025 
0026 /** Zend_Controller_Action_HelperBroker */
0027 // require_once 'Zend/Controller/Action/HelperBroker.php';
0028 
0029 /** Zend_Controller_Plugin_Broker */
0030 // require_once 'Zend/Controller/Plugin/Broker.php';
0031 
0032 /**
0033  * @category   Zend
0034  * @package    Zend_Controller
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  */
0038 class Zend_Controller_Front
0039 {
0040     /**
0041      * Base URL
0042      * @var string
0043      */
0044     protected $_baseUrl = null;
0045 
0046     /**
0047      * Directory|ies where controllers are stored
0048      *
0049      * @var string|array
0050      */
0051     protected $_controllerDir = null;
0052 
0053     /**
0054      * Instance of Zend_Controller_Dispatcher_Interface
0055      * @var Zend_Controller_Dispatcher_Interface
0056      */
0057     protected $_dispatcher = null;
0058 
0059     /**
0060      * Singleton instance
0061      *
0062      * Marked only as protected to allow extension of the class. To extend,
0063      * simply override {@link getInstance()}.
0064      *
0065      * @var Zend_Controller_Front
0066      */
0067     protected static $_instance = null;
0068 
0069     /**
0070      * Array of invocation parameters to use when instantiating action
0071      * controllers
0072      * @var array
0073      */
0074     protected $_invokeParams = array();
0075 
0076     /**
0077      * Subdirectory within a module containing controllers; defaults to 'controllers'
0078      * @var string
0079      */
0080     protected $_moduleControllerDirectoryName = 'controllers';
0081 
0082     /**
0083      * Instance of Zend_Controller_Plugin_Broker
0084      * @var Zend_Controller_Plugin_Broker
0085      */
0086     protected $_plugins = null;
0087 
0088     /**
0089      * Instance of Zend_Controller_Request_Abstract
0090      * @var Zend_Controller_Request_Abstract
0091      */
0092     protected $_request = null;
0093 
0094     /**
0095      * Instance of Zend_Controller_Response_Abstract
0096      * @var Zend_Controller_Response_Abstract
0097      */
0098     protected $_response = null;
0099 
0100     /**
0101      * Whether or not to return the response prior to rendering output while in
0102      * {@link dispatch()}; default is to send headers and render output.
0103      * @var boolean
0104      */
0105     protected $_returnResponse = false;
0106 
0107     /**
0108      * Instance of Zend_Controller_Router_Interface
0109      * @var Zend_Controller_Router_Interface
0110      */
0111     protected $_router = null;
0112 
0113     /**
0114      * Whether or not exceptions encountered in {@link dispatch()} should be
0115      * thrown or trapped in the response object
0116      * @var boolean
0117      */
0118     protected $_throwExceptions = false;
0119 
0120     /**
0121      * Constructor
0122      *
0123      * Instantiate using {@link getInstance()}; front controller is a singleton
0124      * object.
0125      *
0126      * Instantiates the plugin broker.
0127      *
0128      * @return void
0129      */
0130     protected function __construct()
0131     {
0132         $this->_plugins = new Zend_Controller_Plugin_Broker();
0133     }
0134 
0135     /**
0136      * Enforce singleton; disallow cloning
0137      *
0138      * @return void
0139      */
0140     private function __clone()
0141     {
0142     }
0143 
0144     /**
0145      * Singleton instance
0146      *
0147      * @return Zend_Controller_Front
0148      */
0149     public static function getInstance()
0150     {
0151         if (null === self::$_instance) {
0152             self::$_instance = new self();
0153         }
0154 
0155         return self::$_instance;
0156     }
0157 
0158     /**
0159      * Resets all object properties of the singleton instance
0160      *
0161      * Primarily used for testing; could be used to chain front controllers.
0162      *
0163      * Also resets action helper broker, clearing all registered helpers.
0164      *
0165      * @return void
0166      */
0167     public function resetInstance()
0168     {
0169         $reflection = new ReflectionObject($this);
0170         foreach ($reflection->getProperties() as $property) {
0171             $name = $property->getName();
0172             switch ($name) {
0173                 case '_instance':
0174                     break;
0175                 case '_controllerDir':
0176                 case '_invokeParams':
0177                     $this->{$name} = array();
0178                     break;
0179                 case '_plugins':
0180                     $this->{$name} = new Zend_Controller_Plugin_Broker();
0181                     break;
0182                 case '_throwExceptions':
0183                 case '_returnResponse':
0184                     $this->{$name} = false;
0185                     break;
0186                 case '_moduleControllerDirectoryName':
0187                     $this->{$name} = 'controllers';
0188                     break;
0189                 default:
0190                     $this->{$name} = null;
0191                     break;
0192             }
0193         }
0194         Zend_Controller_Action_HelperBroker::resetHelpers();
0195     }
0196 
0197     /**
0198      * Convenience feature, calls setControllerDirectory()->setRouter()->dispatch()
0199      *
0200      * In PHP 5.1.x, a call to a static method never populates $this -- so run()
0201      * may actually be called after setting up your front controller.
0202      *
0203      * @param string|array $controllerDirectory Path to Zend_Controller_Action
0204      * controller classes or array of such paths
0205      * @return void
0206      * @throws Zend_Controller_Exception if called from an object instance
0207      */
0208     public static function run($controllerDirectory)
0209     {
0210         self::getInstance()
0211             ->setControllerDirectory($controllerDirectory)
0212             ->dispatch();
0213     }
0214 
0215     /**
0216      * Add a controller directory to the controller directory stack
0217      *
0218      * If $args is presented and is a string, uses it for the array key mapping
0219      * to the directory specified.
0220      *
0221      * @param string $directory
0222      * @param string $module Optional argument; module with which to associate directory. If none provided, assumes 'default'
0223      * @return Zend_Controller_Front
0224      * @throws Zend_Controller_Exception if directory not found or readable
0225      */
0226     public function addControllerDirectory($directory, $module = null)
0227     {
0228         $this->getDispatcher()->addControllerDirectory($directory, $module);
0229         return $this;
0230     }
0231 
0232     /**
0233      * Set controller directory
0234      *
0235      * Stores controller directory(ies) in dispatcher. May be an array of
0236      * directories or a string containing a single directory.
0237      *
0238      * @param string|array $directory Path to Zend_Controller_Action controller
0239      * classes or array of such paths
0240      * @param  string $module Optional module name to use with string $directory
0241      * @return Zend_Controller_Front
0242      */
0243     public function setControllerDirectory($directory, $module = null)
0244     {
0245         $this->getDispatcher()->setControllerDirectory($directory, $module);
0246         return $this;
0247     }
0248 
0249     /**
0250      * Retrieve controller directory
0251      *
0252      * Retrieves:
0253      * - Array of all controller directories if no $name passed
0254      * - String path if $name passed and exists as a key in controller directory array
0255      * - null if $name passed but does not exist in controller directory keys
0256      *
0257      * @param  string $name Default null
0258      * @return array|string|null
0259      */
0260     public function getControllerDirectory($name = null)
0261     {
0262         return $this->getDispatcher()->getControllerDirectory($name);
0263     }
0264 
0265     /**
0266      * Remove a controller directory by module name
0267      *
0268      * @param  string $module
0269      * @return bool
0270      */
0271     public function removeControllerDirectory($module)
0272     {
0273         return $this->getDispatcher()->removeControllerDirectory($module);
0274     }
0275 
0276     /**
0277      * Specify a directory as containing modules
0278      *
0279      * Iterates through the directory, adding any subdirectories as modules;
0280      * the subdirectory within each module named after {@link $_moduleControllerDirectoryName}
0281      * will be used as the controller directory path.
0282      *
0283      * @param  string $path
0284      * @return Zend_Controller_Front
0285      */
0286     public function addModuleDirectory($path)
0287     {
0288         try{
0289             $dir = new DirectoryIterator($path);
0290         } catch(Exception $e) {
0291             // require_once 'Zend/Controller/Exception.php';
0292             throw new Zend_Controller_Exception("Directory $path not readable", 0, $e);
0293         }
0294         foreach ($dir as $file) {
0295             if ($file->isDot() || !$file->isDir()) {
0296                 continue;
0297             }
0298 
0299             $module    = $file->getFilename();
0300 
0301             // Don't use SCCS directories as modules
0302             if (preg_match('/^[^a-z]/i', $module) || ('CVS' == $module)) {
0303                 continue;
0304             }
0305 
0306             $moduleDir = $file->getPathname() . DIRECTORY_SEPARATOR . $this->getModuleControllerDirectoryName();
0307             $this->addControllerDirectory($moduleDir, $module);
0308         }
0309 
0310         return $this;
0311     }
0312 
0313     /**
0314      * Return the path to a module directory (but not the controllers directory within)
0315      *
0316      * @param  string $module
0317      * @return string|null
0318      */
0319     public function getModuleDirectory($module = null)
0320     {
0321         if (null === $module) {
0322             $request = $this->getRequest();
0323             if (null !== $request) {
0324                 $module = $this->getRequest()->getModuleName();
0325             }
0326             if (empty($module)) {
0327                 $module = $this->getDispatcher()->getDefaultModule();
0328             }
0329         }
0330 
0331         $controllerDir = $this->getControllerDirectory($module);
0332 
0333         if ((null === $controllerDir) || !is_string($controllerDir)) {
0334             return null;
0335         }
0336 
0337         return dirname($controllerDir);
0338     }
0339 
0340     /**
0341      * Set the directory name within a module containing controllers
0342      *
0343      * @param  string $name
0344      * @return Zend_Controller_Front
0345      */
0346     public function setModuleControllerDirectoryName($name = 'controllers')
0347     {
0348         $this->_moduleControllerDirectoryName = (string) $name;
0349 
0350         return $this;
0351     }
0352 
0353     /**
0354      * Return the directory name within a module containing controllers
0355      *
0356      * @return string
0357      */
0358     public function getModuleControllerDirectoryName()
0359     {
0360         return $this->_moduleControllerDirectoryName;
0361     }
0362 
0363     /**
0364      * Set the default controller (unformatted string)
0365      *
0366      * @param string $controller
0367      * @return Zend_Controller_Front
0368      */
0369     public function setDefaultControllerName($controller)
0370     {
0371         $dispatcher = $this->getDispatcher();
0372         $dispatcher->setDefaultControllerName($controller);
0373         return $this;
0374     }
0375 
0376     /**
0377      * Retrieve the default controller (unformatted string)
0378      *
0379      * @return string
0380      */
0381     public function getDefaultControllerName()
0382     {
0383         return $this->getDispatcher()->getDefaultControllerName();
0384     }
0385 
0386     /**
0387      * Set the default action (unformatted string)
0388      *
0389      * @param string $action
0390      * @return Zend_Controller_Front
0391      */
0392     public function setDefaultAction($action)
0393     {
0394         $dispatcher = $this->getDispatcher();
0395         $dispatcher->setDefaultAction($action);
0396         return $this;
0397     }
0398 
0399     /**
0400      * Retrieve the default action (unformatted string)
0401      *
0402      * @return string
0403      */
0404     public function getDefaultAction()
0405     {
0406         return $this->getDispatcher()->getDefaultAction();
0407     }
0408 
0409     /**
0410      * Set the default module name
0411      *
0412      * @param string $module
0413      * @return Zend_Controller_Front
0414      */
0415     public function setDefaultModule($module)
0416     {
0417         $dispatcher = $this->getDispatcher();
0418         $dispatcher->setDefaultModule($module);
0419         return $this;
0420     }
0421 
0422     /**
0423      * Retrieve the default module
0424      *
0425      * @return string
0426      */
0427     public function getDefaultModule()
0428     {
0429         return $this->getDispatcher()->getDefaultModule();
0430     }
0431 
0432     /**
0433      * Set request class/object
0434      *
0435      * Set the request object.  The request holds the request environment.
0436      *
0437      * If a class name is provided, it will instantiate it
0438      *
0439      * @param string|Zend_Controller_Request_Abstract $request
0440      * @throws Zend_Controller_Exception if invalid request class
0441      * @return Zend_Controller_Front
0442      */
0443     public function setRequest($request)
0444     {
0445         if (is_string($request)) {
0446             if (!class_exists($request)) {
0447                 // require_once 'Zend/Loader.php';
0448                 Zend_Loader::loadClass($request);
0449             }
0450             $request = new $request();
0451         }
0452         if (!$request instanceof Zend_Controller_Request_Abstract) {
0453             // require_once 'Zend/Controller/Exception.php';
0454             throw new Zend_Controller_Exception('Invalid request class');
0455         }
0456 
0457         $this->_request = $request;
0458 
0459         return $this;
0460     }
0461 
0462     /**
0463      * Return the request object.
0464      *
0465      * @return null|Zend_Controller_Request_Abstract
0466      */
0467     public function getRequest()
0468     {
0469         return $this->_request;
0470     }
0471 
0472     /**
0473      * Set router class/object
0474      *
0475      * Set the router object.  The router is responsible for mapping
0476      * the request to a controller and action.
0477      *
0478      * If a class name is provided, instantiates router with any parameters
0479      * registered via {@link setParam()} or {@link setParams()}.
0480      *
0481      * @param string|Zend_Controller_Router_Interface $router
0482      * @throws Zend_Controller_Exception if invalid router class
0483      * @return Zend_Controller_Front
0484      */
0485     public function setRouter($router)
0486     {
0487         if (is_string($router)) {
0488             if (!class_exists($router)) {
0489                 // require_once 'Zend/Loader.php';
0490                 Zend_Loader::loadClass($router);
0491             }
0492             $router = new $router();
0493         }
0494 
0495         if (!$router instanceof Zend_Controller_Router_Interface) {
0496             // require_once 'Zend/Controller/Exception.php';
0497             throw new Zend_Controller_Exception('Invalid router class');
0498         }
0499 
0500         $router->setFrontController($this);
0501         $this->_router = $router;
0502 
0503         return $this;
0504     }
0505 
0506     /**
0507      * Return the router object.
0508      *
0509      * Instantiates a Zend_Controller_Router_Rewrite object if no router currently set.
0510      *
0511      * @return Zend_Controller_Router_Interface
0512      */
0513     public function getRouter()
0514     {
0515         if (null == $this->_router) {
0516             // require_once 'Zend/Controller/Router/Rewrite.php';
0517             $this->setRouter(new Zend_Controller_Router_Rewrite());
0518         }
0519 
0520         return $this->_router;
0521     }
0522 
0523     /**
0524      * Set the base URL used for requests
0525      *
0526      * Use to set the base URL segment of the REQUEST_URI to use when
0527      * determining PATH_INFO, etc. Examples:
0528      * - /admin
0529      * - /myapp
0530      * - /subdir/index.php
0531      *
0532      * Note that the URL should not include the full URI. Do not use:
0533      * - http://example.com/admin
0534      * - http://example.com/myapp
0535      * - http://example.com/subdir/index.php
0536      *
0537      * If a null value is passed, this can be used as well for autodiscovery (default).
0538      *
0539      * @param string $base
0540      * @return Zend_Controller_Front
0541      * @throws Zend_Controller_Exception for non-string $base
0542      */
0543     public function setBaseUrl($base = null)
0544     {
0545         if (!is_string($base) && (null !== $base)) {
0546             // require_once 'Zend/Controller/Exception.php';
0547             throw new Zend_Controller_Exception('Rewrite base must be a string');
0548         }
0549 
0550         $this->_baseUrl = $base;
0551 
0552         if ((null !== ($request = $this->getRequest())) && (method_exists($request, 'setBaseUrl'))) {
0553             $request->setBaseUrl($base);
0554         }
0555 
0556         return $this;
0557     }
0558 
0559     /**
0560      * Retrieve the currently set base URL
0561      *
0562      * @return string
0563      */
0564     public function getBaseUrl()
0565     {
0566         $request = $this->getRequest();
0567         if ((null !== $request) && method_exists($request, 'getBaseUrl')) {
0568             return $request->getBaseUrl();
0569         }
0570 
0571         return $this->_baseUrl;
0572     }
0573 
0574     /**
0575      * Set the dispatcher object.  The dispatcher is responsible for
0576      * taking a Zend_Controller_Dispatcher_Token object, instantiating the controller, and
0577      * call the action method of the controller.
0578      *
0579      * @param Zend_Controller_Dispatcher_Interface $dispatcher
0580      * @return Zend_Controller_Front
0581      */
0582     public function setDispatcher(Zend_Controller_Dispatcher_Interface $dispatcher)
0583     {
0584         $this->_dispatcher = $dispatcher;
0585         return $this;
0586     }
0587 
0588     /**
0589      * Return the dispatcher object.
0590      *
0591      * @return Zend_Controller_Dispatcher_Interface
0592      */
0593     public function getDispatcher()
0594     {
0595         /**
0596          * Instantiate the default dispatcher if one was not set.
0597          */
0598         if (!$this->_dispatcher instanceof Zend_Controller_Dispatcher_Interface) {
0599             // require_once 'Zend/Controller/Dispatcher/Standard.php';
0600             $this->_dispatcher = new Zend_Controller_Dispatcher_Standard();
0601         }
0602         return $this->_dispatcher;
0603     }
0604 
0605     /**
0606      * Set response class/object
0607      *
0608      * Set the response object.  The response is a container for action
0609      * responses and headers. Usage is optional.
0610      *
0611      * If a class name is provided, instantiates a response object.
0612      *
0613      * @param string|Zend_Controller_Response_Abstract $response
0614      * @throws Zend_Controller_Exception if invalid response class
0615      * @return Zend_Controller_Front
0616      */
0617     public function setResponse($response)
0618     {
0619         if (is_string($response)) {
0620             if (!class_exists($response)) {
0621                 // require_once 'Zend/Loader.php';
0622                 Zend_Loader::loadClass($response);
0623             }
0624             $response = new $response();
0625         }
0626         if (!$response instanceof Zend_Controller_Response_Abstract) {
0627             // require_once 'Zend/Controller/Exception.php';
0628             throw new Zend_Controller_Exception('Invalid response class');
0629         }
0630 
0631         $this->_response = $response;
0632 
0633         return $this;
0634     }
0635 
0636     /**
0637      * Return the response object.
0638      *
0639      * @return null|Zend_Controller_Response_Abstract
0640      */
0641     public function getResponse()
0642     {
0643         return $this->_response;
0644     }
0645 
0646     /**
0647      * Add or modify a parameter to use when instantiating an action controller
0648      *
0649      * @param string $name
0650      * @param mixed $value
0651      * @return Zend_Controller_Front
0652      */
0653     public function setParam($name, $value)
0654     {
0655         $name = (string) $name;
0656         $this->_invokeParams[$name] = $value;
0657         return $this;
0658     }
0659 
0660     /**
0661      * Set parameters to pass to action controller constructors
0662      *
0663      * @param array $params
0664      * @return Zend_Controller_Front
0665      */
0666     public function setParams(array $params)
0667     {
0668         $this->_invokeParams = array_merge($this->_invokeParams, $params);
0669         return $this;
0670     }
0671 
0672     /**
0673      * Retrieve a single parameter from the controller parameter stack
0674      *
0675      * @param string $name
0676      * @return mixed
0677      */
0678     public function getParam($name)
0679     {
0680         if(isset($this->_invokeParams[$name])) {
0681             return $this->_invokeParams[$name];
0682         }
0683 
0684         return null;
0685     }
0686 
0687     /**
0688      * Retrieve action controller instantiation parameters
0689      *
0690      * @return array
0691      */
0692     public function getParams()
0693     {
0694         return $this->_invokeParams;
0695     }
0696 
0697     /**
0698      * Clear the controller parameter stack
0699      *
0700      * By default, clears all parameters. If a parameter name is given, clears
0701      * only that parameter; if an array of parameter names is provided, clears
0702      * each.
0703      *
0704      * @param null|string|array single key or array of keys for params to clear
0705      * @return Zend_Controller_Front
0706      */
0707     public function clearParams($name = null)
0708     {
0709         if (null === $name) {
0710             $this->_invokeParams = array();
0711         } elseif (is_string($name) && isset($this->_invokeParams[$name])) {
0712             unset($this->_invokeParams[$name]);
0713         } elseif (is_array($name)) {
0714             foreach ($name as $key) {
0715                 if (is_string($key) && isset($this->_invokeParams[$key])) {
0716                     unset($this->_invokeParams[$key]);
0717                 }
0718             }
0719         }
0720 
0721         return $this;
0722     }
0723 
0724     /**
0725      * Register a plugin.
0726      *
0727      * @param  Zend_Controller_Plugin_Abstract $plugin
0728      * @param  int $stackIndex Optional; stack index for plugin
0729      * @return Zend_Controller_Front
0730      */
0731     public function registerPlugin(Zend_Controller_Plugin_Abstract $plugin, $stackIndex = null)
0732     {
0733         $this->_plugins->registerPlugin($plugin, $stackIndex);
0734         return $this;
0735     }
0736 
0737     /**
0738      * Unregister a plugin.
0739      *
0740      * @param  string|Zend_Controller_Plugin_Abstract $plugin Plugin class or object to unregister
0741      * @return Zend_Controller_Front
0742      */
0743     public function unregisterPlugin($plugin)
0744     {
0745         $this->_plugins->unregisterPlugin($plugin);
0746         return $this;
0747     }
0748 
0749     /**
0750      * Is a particular plugin registered?
0751      *
0752      * @param  string $class
0753      * @return bool
0754      */
0755     public function hasPlugin($class)
0756     {
0757         return $this->_plugins->hasPlugin($class);
0758     }
0759 
0760     /**
0761      * Retrieve a plugin or plugins by class
0762      *
0763      * @param  string $class
0764      * @return false|Zend_Controller_Plugin_Abstract|array
0765      */
0766     public function getPlugin($class)
0767     {
0768         return $this->_plugins->getPlugin($class);
0769     }
0770 
0771     /**
0772      * Retrieve all plugins
0773      *
0774      * @return array
0775      */
0776     public function getPlugins()
0777     {
0778         return $this->_plugins->getPlugins();
0779     }
0780 
0781     /**
0782      * Set the throwExceptions flag and retrieve current status
0783      *
0784      * Set whether exceptions encounted in the dispatch loop should be thrown
0785      * or caught and trapped in the response object.
0786      *
0787      * Default behaviour is to trap them in the response object; call this
0788      * method to have them thrown.
0789      *
0790      * Passing no value will return the current value of the flag; passing a
0791      * boolean true or false value will set the flag and return the current
0792      * object instance.
0793      *
0794      * @param boolean $flag Defaults to null (return flag state)
0795      * @return boolean|Zend_Controller_Front Used as a setter, returns object; as a getter, returns boolean
0796      */
0797     public function throwExceptions($flag = null)
0798     {
0799         if ($flag !== null) {
0800             $this->_throwExceptions = (bool) $flag;
0801             return $this;
0802         }
0803 
0804         return $this->_throwExceptions;
0805     }
0806 
0807     /**
0808      * Set whether {@link dispatch()} should return the response without first
0809      * rendering output. By default, output is rendered and dispatch() returns
0810      * nothing.
0811      *
0812      * @param boolean $flag
0813      * @return boolean|Zend_Controller_Front Used as a setter, returns object; as a getter, returns boolean
0814      */
0815     public function returnResponse($flag = null)
0816     {
0817         if (true === $flag) {
0818             $this->_returnResponse = true;
0819             return $this;
0820         } elseif (false === $flag) {
0821             $this->_returnResponse = false;
0822             return $this;
0823         }
0824 
0825         return $this->_returnResponse;
0826     }
0827 
0828     /**
0829      * Dispatch an HTTP request to a controller/action.
0830      *
0831      * @param Zend_Controller_Request_Abstract|null $request
0832      * @param Zend_Controller_Response_Abstract|null $response
0833      * @return void|Zend_Controller_Response_Abstract Returns response object if returnResponse() is true
0834      */
0835     public function dispatch(Zend_Controller_Request_Abstract $request = null, Zend_Controller_Response_Abstract $response = null)
0836     {
0837         if (!$this->getParam('noErrorHandler') && !$this->_plugins->hasPlugin('Zend_Controller_Plugin_ErrorHandler')) {
0838             // Register with stack index of 100
0839             // require_once 'Zend/Controller/Plugin/ErrorHandler.php';
0840             $this->_plugins->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(), 100);
0841         }
0842 
0843         if (!$this->getParam('noViewRenderer') && !Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer')) {
0844             // require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
0845             Zend_Controller_Action_HelperBroker::getStack()->offsetSet(-80, new Zend_Controller_Action_Helper_ViewRenderer());
0846         }
0847 
0848         /**
0849          * Instantiate default request object (HTTP version) if none provided
0850          */
0851         if (null !== $request) {
0852             $this->setRequest($request);
0853         } elseif ((null === $request) && (null === ($request = $this->getRequest()))) {
0854             // require_once 'Zend/Controller/Request/Http.php';
0855             $request = new Zend_Controller_Request_Http();
0856             $this->setRequest($request);
0857         }
0858 
0859         /**
0860          * Set base URL of request object, if available
0861          */
0862         if (is_callable(array($this->_request, 'setBaseUrl'))) {
0863             if (null !== $this->_baseUrl) {
0864                 $this->_request->setBaseUrl($this->_baseUrl);
0865             }
0866         }
0867 
0868         /**
0869          * Instantiate default response object (HTTP version) if none provided
0870          */
0871         if (null !== $response) {
0872             $this->setResponse($response);
0873         } elseif ((null === $this->_response) && (null === ($this->_response = $this->getResponse()))) {
0874             // require_once 'Zend/Controller/Response/Http.php';
0875             $response = new Zend_Controller_Response_Http();
0876             $this->setResponse($response);
0877         }
0878 
0879         /**
0880          * Register request and response objects with plugin broker
0881          */
0882         $this->_plugins
0883              ->setRequest($this->_request)
0884              ->setResponse($this->_response);
0885 
0886         /**
0887          * Initialize router
0888          */
0889         $router = $this->getRouter();
0890         $router->setParams($this->getParams());
0891 
0892         /**
0893          * Initialize dispatcher
0894          */
0895         $dispatcher = $this->getDispatcher();
0896         $dispatcher->setParams($this->getParams())
0897                    ->setResponse($this->_response);
0898 
0899         // Begin dispatch
0900         try {
0901             /**
0902              * Route request to controller/action, if a router is provided
0903              */
0904 
0905             /**
0906             * Notify plugins of router startup
0907             */
0908             $this->_plugins->routeStartup($this->_request);
0909 
0910             try {
0911                 $router->route($this->_request);
0912             }  catch (Exception $e) {
0913                 if ($this->throwExceptions()) {
0914                     throw $e;
0915                 }
0916 
0917                 $this->_response->setException($e);
0918             }
0919 
0920             /**
0921             * Notify plugins of router completion
0922             */
0923             $this->_plugins->routeShutdown($this->_request);
0924 
0925             /**
0926              * Notify plugins of dispatch loop startup
0927              */
0928             $this->_plugins->dispatchLoopStartup($this->_request);
0929 
0930             /**
0931              *  Attempt to dispatch the controller/action. If the $this->_request
0932              *  indicates that it needs to be dispatched, move to the next
0933              *  action in the request.
0934              */
0935             do {
0936                 $this->_request->setDispatched(true);
0937 
0938                 /**
0939                  * Notify plugins of dispatch startup
0940                  */
0941                 $this->_plugins->preDispatch($this->_request);
0942 
0943                 /**
0944                  * Skip requested action if preDispatch() has reset it
0945                  */
0946                 if (!$this->_request->isDispatched()) {
0947                     continue;
0948                 }
0949 
0950                 /**
0951                  * Dispatch request
0952                  */
0953                 try {
0954                     $dispatcher->dispatch($this->_request, $this->_response);
0955                 } catch (Exception $e) {
0956                     if ($this->throwExceptions()) {
0957                         throw $e;
0958                     }
0959                     $this->_response->setException($e);
0960                 }
0961 
0962                 /**
0963                  * Notify plugins of dispatch completion
0964                  */
0965                 $this->_plugins->postDispatch($this->_request);
0966             } while (!$this->_request->isDispatched());
0967         } catch (Exception $e) {
0968             if ($this->throwExceptions()) {
0969                 throw $e;
0970             }
0971 
0972             $this->_response->setException($e);
0973         }
0974 
0975         /**
0976          * Notify plugins of dispatch loop completion
0977          */
0978         try {
0979             $this->_plugins->dispatchLoopShutdown();
0980         } catch (Exception $e) {
0981             if ($this->throwExceptions()) {
0982                 throw $e;
0983             }
0984 
0985             $this->_response->setException($e);
0986         }
0987 
0988         if ($this->returnResponse()) {
0989             return $this->_response;
0990         }
0991 
0992         $this->_response->sendResponse();
0993     }
0994 }