PageRenderTime 73ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/devblocks/libs/ZendFramework/Zend/Controller/Front.php

https://github.com/sluther/portsensor
PHP | 969 lines | 448 code | 103 blank | 418 comment | 47 complexity | 34c78e103e051b10cf3055b7fc93eb4d MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  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. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Loader */
  21. require_once 'Zend/Loader.php';
  22. /** Zend_Controller_Action_HelperBroker */
  23. require_once 'Zend/Controller/Action/HelperBroker.php';
  24. /** Zend_Controller_Action_Helper_ViewRenderer */
  25. require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
  26. /** Zend_Controller_Exception */
  27. require_once 'Zend/Controller/Exception.php';
  28. /** Zend_Controller_Plugin_Broker */
  29. require_once 'Zend/Controller/Plugin/Broker.php';
  30. /** Zend_Controller_Request_Abstract */
  31. require_once 'Zend/Controller/Request/Abstract.php';
  32. /** Zend_Controller_Router_Interface */
  33. require_once 'Zend/Controller/Router/Interface.php';
  34. /** Zend_Controller_Dispatcher_Interface */
  35. require_once 'Zend/Controller/Dispatcher/Interface.php';
  36. /** Zend_Controller_Plugin_ErrorHandler */
  37. require_once 'Zend/Controller/Plugin/ErrorHandler.php';
  38. /** Zend_Controller_Response_Abstract */
  39. require_once 'Zend/Controller/Response/Abstract.php';
  40. /**
  41. * @category Zend
  42. * @package Zend_Controller
  43. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  44. * @license http://framework.zend.com/license/new-bsd New BSD License
  45. */
  46. class Zend_Controller_Front
  47. {
  48. /**
  49. * Base URL
  50. * @var string
  51. */
  52. protected $_baseUrl = null;
  53. /**
  54. * Directory|ies where controllers are stored
  55. *
  56. * @var string|array
  57. */
  58. protected $_controllerDir = null;
  59. /**
  60. * Instance of Zend_Controller_Dispatcher_Interface
  61. * @var Zend_Controller_Dispatcher_Interface
  62. */
  63. protected $_dispatcher = null;
  64. /**
  65. * Singleton instance
  66. *
  67. * Marked only as protected to allow extension of the class. To extend,
  68. * simply override {@link getInstance()}.
  69. *
  70. * @var Zend_Controller_Front
  71. */
  72. protected static $_instance = null;
  73. /**
  74. * Array of invocation parameters to use when instantiating action
  75. * controllers
  76. * @var array
  77. */
  78. protected $_invokeParams = array();
  79. /**
  80. * Subdirectory within a module containing controllers; defaults to 'controllers'
  81. * @var string
  82. */
  83. protected $_moduleControllerDirectoryName = 'controllers';
  84. /**
  85. * Instance of Zend_Controller_Plugin_Broker
  86. * @var Zend_Controller_Plugin_Broker
  87. */
  88. protected $_plugins = null;
  89. /**
  90. * Instance of Zend_Controller_Request_Abstract
  91. * @var Zend_Controller_Request_Abstract
  92. */
  93. protected $_request = null;
  94. /**
  95. * Instance of Zend_Controller_Response_Abstract
  96. * @var Zend_Controller_Response_Abstract
  97. */
  98. protected $_response = null;
  99. /**
  100. * Whether or not to return the response prior to rendering output while in
  101. * {@link dispatch()}; default is to send headers and render output.
  102. * @var boolean
  103. */
  104. protected $_returnResponse = false;
  105. /**
  106. * Instance of Zend_Controller_Router_Interface
  107. * @var Zend_Controller_Router_Interface
  108. */
  109. protected $_router = null;
  110. /**
  111. * Whether or not exceptions encountered in {@link dispatch()} should be
  112. * thrown or trapped in the response object
  113. * @var boolean
  114. */
  115. protected $_throwExceptions = false;
  116. /**
  117. * Constructor
  118. *
  119. * Instantiate using {@link getInstance()}; front controller is a singleton
  120. * object.
  121. *
  122. * Instantiates the plugin broker.
  123. *
  124. * @return void
  125. */
  126. private function __construct()
  127. {
  128. $this->_plugins = new Zend_Controller_Plugin_Broker();
  129. }
  130. /**
  131. * Enforce singleton; disallow cloning
  132. *
  133. * @return void
  134. */
  135. private function __clone()
  136. {
  137. }
  138. /**
  139. * Singleton instance
  140. *
  141. * @return Zend_Controller_Front
  142. */
  143. public static function getInstance()
  144. {
  145. if (null === self::$_instance) {
  146. self::$_instance = new self();
  147. }
  148. return self::$_instance;
  149. }
  150. /**
  151. * Resets all object properties of the singleton instance
  152. *
  153. * Primarily used for testing; could be used to chain front controllers.
  154. *
  155. * @return void
  156. */
  157. public function resetInstance()
  158. {
  159. $reflection = new ReflectionObject($this);
  160. foreach ($reflection->getProperties() as $property) {
  161. $name = $property->getName();
  162. switch ($name) {
  163. case '_instance':
  164. break;
  165. case '_controllerDir':
  166. case '_invokeParams':
  167. $this->{$name} = array();
  168. break;
  169. case '_plugins':
  170. $this->{$name} = new Zend_Controller_Plugin_Broker();
  171. break;
  172. case '_throwExceptions':
  173. case '_returnResponse':
  174. $this->{$name} = false;
  175. break;
  176. case '_moduleControllerDirectoryName':
  177. $this->{$name} = 'controllers';
  178. break;
  179. default:
  180. $this->{$name} = null;
  181. break;
  182. }
  183. }
  184. if (!Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer')) {
  185. Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_ViewRenderer());
  186. }
  187. }
  188. /**
  189. * Convenience feature, calls setControllerDirectory()->setRouter()->dispatch()
  190. *
  191. * In PHP 5.1.x, a call to a static method never populates $this -- so run()
  192. * may actually be called after setting up your front controller.
  193. *
  194. * @param string|array $controllerDirectory Path to Zend_Controller_Action
  195. * controller classes or array of such paths
  196. * @return void
  197. * @throws Zend_Controller_Exception if called from an object instance
  198. */
  199. public static function run($controllerDirectory)
  200. {
  201. self::getInstance()
  202. ->setControllerDirectory($controllerDirectory)
  203. ->dispatch();
  204. }
  205. /**
  206. * Add a controller directory to the controller directory stack
  207. *
  208. * If $args is presented and is a string, uses it for the array key mapping
  209. * to the directory specified.
  210. *
  211. * @param string $directory
  212. * @param string $module Optional argument; module with which to associate directory. If none provided, assumes 'defualt'
  213. * @return Zend_Controller_Front
  214. * @throws Zend_Controller_Exception if directory not found or readable
  215. */
  216. public function addControllerDirectory($directory, $module = null)
  217. {
  218. if (empty($module) || is_numeric($module) || !is_string($module)) {
  219. $module = $this->getDispatcher()->getDefaultModule();
  220. }
  221. $this->_controllerDir[$module] = rtrim((string) $directory, '/\\');
  222. return $this;
  223. }
  224. /**
  225. * Set controller directory
  226. *
  227. * Stores controller directory to pass to dispatcher. May be an array of
  228. * directories or a string containing a single directory.
  229. *
  230. * @param string|array $directory Path to Zend_Controller_Action controller
  231. * classes or array of such paths
  232. * @param string $module Optional module name to use with string $directory
  233. * @return Zend_Controller_Front
  234. */
  235. public function setControllerDirectory($directory, $module = null)
  236. {
  237. $this->_controllerDir = array();
  238. if (is_string($directory)) {
  239. $this->addControllerDirectory($directory, $module);
  240. } elseif (is_array($directory)) {
  241. foreach ((array) $directory as $module => $path) {
  242. $this->addControllerDirectory($path, $module);
  243. }
  244. } else {
  245. throw new Zend_Controller_Exception('Controller directory spec must be either a string or an array');
  246. }
  247. return $this;
  248. }
  249. /**
  250. * Retrieve controller directory
  251. *
  252. * Retrieves:
  253. * - Array of all controller directories if no $name passed
  254. * - String path if $name passed and exists as a key in controller directory array
  255. * - null if $name passed but does not exist in controller directory keys
  256. *
  257. * @param string $name Default null
  258. * @return array|string|null
  259. */
  260. public function getControllerDirectory($name = null)
  261. {
  262. if (null === $name) {
  263. return $this->_controllerDir;
  264. }
  265. $name = (string) $name;
  266. if (isset($this->_controllerDir[$name])) {
  267. return $this->_controllerDir[$name];
  268. }
  269. return null;
  270. }
  271. /**
  272. * Specify a directory as containing modules
  273. *
  274. * Iterates through the directory, adding any subdirectories as modules;
  275. * the subdirectory within each module named after {@link $_moduleControllerDirectoryName}
  276. * will be used as the controller directory path.
  277. *
  278. * @param string $path
  279. * @return Zend_Controller_Front
  280. */
  281. public function addModuleDirectory($path)
  282. {
  283. $dir = new DirectoryIterator($path);
  284. foreach ($dir as $file) {
  285. if ($file->isDot() || !$file->isDir()) {
  286. continue;
  287. }
  288. $module = $file->getFilename();
  289. // Don't use SCCS directories as modules
  290. if (preg_match('/^[^a-z]/i', $module) || ('CVS' == $module)) {
  291. continue;
  292. }
  293. $moduleDir = $file->getPathname() . DIRECTORY_SEPARATOR . $this->getModuleControllerDirectoryName();
  294. $this->addControllerDirectory($moduleDir, $module);
  295. }
  296. return $this;
  297. }
  298. /**
  299. * Set the directory name within a module containing controllers
  300. *
  301. * @param string $name
  302. * @return Zend_Controller_Front
  303. */
  304. public function setModuleControllerDirectoryName($name = 'controllers')
  305. {
  306. $this->_moduleControllerDirectoryName = (string) $name;
  307. return $this;
  308. }
  309. /**
  310. * Return the directory name within a module containing controllers
  311. *
  312. * @return string
  313. */
  314. public function getModuleControllerDirectoryName()
  315. {
  316. return $this->_moduleControllerDirectoryName;
  317. }
  318. /**
  319. * Set the default controller (unformatted string)
  320. *
  321. * @param string $controller
  322. * @return Zend_Controller_Front
  323. */
  324. public function setDefaultControllerName($controller)
  325. {
  326. $dispatcher = $this->getDispatcher();
  327. $dispatcher->setDefaultControllerName($controller);
  328. return $this;
  329. }
  330. /**
  331. * Retrieve the default controller (unformatted string)
  332. *
  333. * @return string
  334. */
  335. public function getDefaultControllerName()
  336. {
  337. return $this->getDispatcher()->getDefaultControllerName();
  338. }
  339. /**
  340. * Set the default action (unformatted string)
  341. *
  342. * @param string $action
  343. * @return Zend_Controller_Front
  344. */
  345. public function setDefaultAction($action)
  346. {
  347. $dispatcher = $this->getDispatcher();
  348. $dispatcher->setDefaultAction($action);
  349. return $this;
  350. }
  351. /**
  352. * Retrieve the default action (unformatted string)
  353. *
  354. * @return string
  355. */
  356. public function getDefaultAction()
  357. {
  358. return $this->getDispatcher()->getDefaultAction();
  359. }
  360. /**
  361. * Set the default module name
  362. *
  363. * @param string $module
  364. * @return Zend_Controller_Front
  365. */
  366. public function setDefaultModule($module)
  367. {
  368. $dispatcher = $this->getDispatcher();
  369. $dispatcher->setDefaultModule($module);
  370. return $this;
  371. }
  372. /**
  373. * Retrieve the default module
  374. *
  375. * @return string
  376. */
  377. public function getDefaultModule()
  378. {
  379. return $this->getDispatcher()->getDefaultModule();
  380. }
  381. /**
  382. * Set request class/object
  383. *
  384. * Set the request object. The request holds the request environment.
  385. *
  386. * If a class name is provided, it will instantiate it
  387. *
  388. * @param string|Zend_Controller_Request_Abstract $request
  389. * @throws Zend_Controller_Exception if invalid request class
  390. * @return Zend_Controller_Front
  391. */
  392. public function setRequest($request)
  393. {
  394. if (is_string($request)) {
  395. Zend_Loader::loadClass($request);
  396. $request = new $request();
  397. }
  398. if (!$request instanceof Zend_Controller_Request_Abstract) {
  399. throw new Zend_Controller_Exception('Invalid request class');
  400. }
  401. $this->_request = $request;
  402. return $this;
  403. }
  404. /**
  405. * Return the request object.
  406. *
  407. * @return null|Zend_Controller_Request_Abstract
  408. */
  409. public function getRequest()
  410. {
  411. return $this->_request;
  412. }
  413. /**
  414. * Set router class/object
  415. *
  416. * Set the router object. The router is responsible for mapping
  417. * the request to a controller and action.
  418. *
  419. * If a class name is provided, instantiates router with any parameters
  420. * registered via {@link setParam()} or {@link setParams()}.
  421. *
  422. * @param string|Zend_Controller_Router_Interface $router
  423. * @throws Zend_Controller_Exception if invalid router class
  424. * @return Zend_Controller_Front
  425. */
  426. public function setRouter($router)
  427. {
  428. if (is_string($router)) {
  429. Zend_Loader::loadClass($router);
  430. $router = new $router();
  431. }
  432. if (!$router instanceof Zend_Controller_Router_Interface) {
  433. throw new Zend_Controller_Exception('Invalid router class');
  434. }
  435. $this->_router = $router;
  436. return $this;
  437. }
  438. /**
  439. * Return the router object.
  440. *
  441. * Instantiates a Zend_Controller_Router_Rewrite object if no router currently set.
  442. *
  443. * @return null|Zend_Controller_Router_Interface
  444. */
  445. public function getRouter()
  446. {
  447. if (null == $this->_router) {
  448. require_once 'Zend/Controller/Router/Rewrite.php';
  449. $this->setRouter(new Zend_Controller_Router_Rewrite());
  450. }
  451. return $this->_router;
  452. }
  453. /**
  454. * Set the base URL used for requests
  455. *
  456. * Use to set the base URL segment of the REQUEST_URI to use when
  457. * determining PATH_INFO, etc. Examples:
  458. * - /admin
  459. * - /myapp
  460. * - /subdir/index.php
  461. *
  462. * Note that the URL should not include the full URI. Do not use:
  463. * - http://example.com/admin
  464. * - http://example.com/myapp
  465. * - http://example.com/subdir/index.php
  466. *
  467. * If a null value is passed, this can be used as well for autodiscovery (default).
  468. *
  469. * @param string $base
  470. * @return Zend_Controller_Front
  471. * @throws Zend_Controller_Exception for non-string $base
  472. */
  473. public function setBaseUrl($base = null)
  474. {
  475. if (!is_string($base) && (null !== $base)) {
  476. throw new Zend_Controller_Exception('Rewrite base must be a string');
  477. }
  478. $this->_baseUrl = $base;
  479. if ((null !== ($request = $this->getRequest())) && (method_exists($request, 'setBaseUrl'))) {
  480. $request->setBaseUrl($base);
  481. }
  482. return $this;
  483. }
  484. /**
  485. * Retrieve the currently set base URL
  486. *
  487. * @return string
  488. */
  489. public function getBaseUrl()
  490. {
  491. $request = $this->getRequest();
  492. if ((null !== $request) && method_exists($request, 'getBaseUrl')) {
  493. return $request->getBaseUrl();
  494. }
  495. return $this->_baseUrl;
  496. }
  497. /**
  498. * Set the dispatcher object. The dispatcher is responsible for
  499. * taking a Zend_Controller_Dispatcher_Token object, instantiating the controller, and
  500. * call the action method of the controller.
  501. *
  502. * @param Zend_Controller_Dispatcher_Interface $dispatcher
  503. * @return Zend_Controller_Front
  504. */
  505. public function setDispatcher(Zend_Controller_Dispatcher_Interface $dispatcher)
  506. {
  507. $this->_dispatcher = $dispatcher;
  508. return $this;
  509. }
  510. /**
  511. * Return the dispatcher object.
  512. *
  513. * @return Zend_Controller_Dispatcher_Interface
  514. */
  515. public function getDispatcher()
  516. {
  517. /**
  518. * Instantiate the default dispatcher if one was not set.
  519. */
  520. if (!$this->_dispatcher instanceof Zend_Controller_Dispatcher_Interface) {
  521. require_once 'Zend/Controller/Dispatcher/Standard.php';
  522. $this->_dispatcher = new Zend_Controller_Dispatcher_Standard();
  523. }
  524. return $this->_dispatcher;
  525. }
  526. /**
  527. * Set response class/object
  528. *
  529. * Set the response object. The response is a container for action
  530. * responses and headers. Usage is optional.
  531. *
  532. * If a class name is provided, instantiates a response object.
  533. *
  534. * @param string|Zend_Controller_Response_Abstract $response
  535. * @throws Zend_Controller_Exception if invalid response class
  536. * @return Zend_Controller_Front
  537. */
  538. public function setResponse($response)
  539. {
  540. if (is_string($response)) {
  541. Zend_Loader::loadClass($response);
  542. $response = new $response();
  543. }
  544. if (!$response instanceof Zend_Controller_Response_Abstract) {
  545. throw new Zend_Controller_Exception('Invalid response class');
  546. }
  547. $this->_response = $response;
  548. return $this;
  549. }
  550. /**
  551. * Return the response object.
  552. *
  553. * @return null|Zend_Controller_Response_Abstract
  554. */
  555. public function getResponse()
  556. {
  557. return $this->_response;
  558. }
  559. /**
  560. * Add or modify a parameter to use when instantiating an action controller
  561. *
  562. * @param string $name
  563. * @param mixed $value
  564. * @return Zend_Controller_Front
  565. */
  566. public function setParam($name, $value)
  567. {
  568. $name = (string) $name;
  569. $this->_invokeParams[$name] = $value;
  570. return $this;
  571. }
  572. /**
  573. * Set parameters to pass to action controller constructors
  574. *
  575. * @param array $params
  576. * @return Zend_Controller_Front
  577. */
  578. public function setParams(array $params)
  579. {
  580. $this->_invokeParams = array_merge($this->_invokeParams, $params);
  581. return $this;
  582. }
  583. /**
  584. * Retrieve a single parameter from the controller parameter stack
  585. *
  586. * @param string $name
  587. * @return mixed
  588. */
  589. public function getParam($name)
  590. {
  591. if(isset($this->_invokeParams[$name])) {
  592. return $this->_invokeParams[$name];
  593. }
  594. return null;
  595. }
  596. /**
  597. * Retrieve action controller instantiation parameters
  598. *
  599. * @return array
  600. */
  601. public function getParams()
  602. {
  603. return $this->_invokeParams;
  604. }
  605. /**
  606. * Clear the controller parameter stack
  607. *
  608. * By default, clears all parameters. If a parameter name is given, clears
  609. * only that parameter; if an array of parameter names is provided, clears
  610. * each.
  611. *
  612. * @param null|string|array single key or array of keys for params to clear
  613. * @return Zend_Controller_Front
  614. */
  615. public function clearParams($name = null)
  616. {
  617. if (null === $name) {
  618. $this->_invokeParams = array();
  619. } elseif (is_string($name) && isset($this->_invokeParams[$name])) {
  620. unset($this->_invokeParams[$name]);
  621. } elseif (is_array($name)) {
  622. foreach ($name as $key) {
  623. if (is_string($key) && isset($this->_invokeParams[$key])) {
  624. unset($this->_invokeParams[$key]);
  625. }
  626. }
  627. }
  628. return $this;
  629. }
  630. /**
  631. * Register a plugin.
  632. *
  633. * @param Zend_Controller_Plugin_Abstract $plugin
  634. * @param int $stackIndex Optional; stack index for plugin
  635. * @return Zend_Controller_Front
  636. */
  637. public function registerPlugin(Zend_Controller_Plugin_Abstract $plugin, $stackIndex = null)
  638. {
  639. $this->_plugins->registerPlugin($plugin, $stackIndex);
  640. return $this;
  641. }
  642. /**
  643. * Unregister a plugin.
  644. *
  645. * @param string|Zend_Controller_Plugin_Abstract $plugin Plugin class or object to unregister
  646. * @return Zend_Controller_Front
  647. */
  648. public function unregisterPlugin($plugin)
  649. {
  650. $this->_plugins->unregisterPlugin($plugin);
  651. return $this;
  652. }
  653. /**
  654. * Is a particular plugin registered?
  655. *
  656. * @param string $class
  657. * @return bool
  658. */
  659. public function hasPlugin($class)
  660. {
  661. return $this->_plugins->hasPlugin($class);
  662. }
  663. /**
  664. * Retrieve a plugin or plugins by class
  665. *
  666. * @param string $class
  667. * @return false|Zend_Controller_Plugin_Abstract|array
  668. */
  669. public function getPlugin($class)
  670. {
  671. return $this->_plugins->getPlugin($class);
  672. }
  673. /**
  674. * Retrieve all plugins
  675. *
  676. * @return array
  677. */
  678. public function getPlugins()
  679. {
  680. return $this->_plugins->getPlugins();
  681. }
  682. /**
  683. * Set whether exceptions encounted in the dispatch loop should be thrown
  684. * or caught and trapped in the response object
  685. *
  686. * Default behaviour is to trap them in the response object; call this
  687. * method to have them thrown.
  688. *
  689. * @param boolean $flag Defaults to true
  690. * @return boolean|Zend_Controller_Front Used as a setter, returns object; as a getter, returns boolean
  691. */
  692. public function throwExceptions($flag = null)
  693. {
  694. if (true === $flag) {
  695. $this->_throwExceptions = true;
  696. return $this;
  697. } elseif (false === $flag) {
  698. $this->_throwExceptions = false;
  699. return $this;
  700. }
  701. return $this->_throwExceptions;
  702. }
  703. /**
  704. * Set whether {@link dispatch()} should return the response without first
  705. * rendering output. By default, output is rendered and dispatch() returns
  706. * nothing.
  707. *
  708. * @param boolean $flag
  709. * @return boolean|Zend_Controller_Front Used as a setter, returns object; as a getter, returns boolean
  710. */
  711. public function returnResponse($flag = null)
  712. {
  713. if (true === $flag) {
  714. $this->_returnResponse = true;
  715. return $this;
  716. } elseif (false === $flag) {
  717. $this->_returnResponse = false;
  718. return $this;
  719. }
  720. return $this->_returnResponse;
  721. }
  722. /**
  723. * Dispatch an HTTP request to a controller/action.
  724. *
  725. * @param Zend_Controller_Request_Abstract|null $request
  726. * @param Zend_Controller_Response_Abstract|null $response
  727. * @return void|Zend_Controller_Response_Abstract Returns response object if returnResponse() is true
  728. */
  729. public function dispatch(Zend_Controller_Request_Abstract $request = null, Zend_Controller_Response_Abstract $response = null)
  730. {
  731. if (!$this->getParam('noErrorHandler') && !$this->_plugins->hasPlugin('Zend_Controller_Plugin_ErrorHandler')) {
  732. // Register with stack index of 100
  733. $this->_plugins->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(), 100);
  734. }
  735. if (!$this->getParam('noViewRenderer') && !Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer')) {
  736. Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_ViewRenderer());
  737. }
  738. /**
  739. * Instantiate default request object (HTTP version) if none provided
  740. */
  741. if (null !== $request) {
  742. $this->setRequest($request);
  743. } elseif ((null === $request) && (null === ($request = $this->getRequest()))) {
  744. require_once 'Zend/Controller/Request/Http.php';
  745. $request = new Zend_Controller_Request_Http();
  746. $this->setRequest($request);
  747. }
  748. /**
  749. * Set base URL of request object, if available
  750. */
  751. if (is_callable(array($this->_request, 'setBaseUrl'))) {
  752. if (null !== $this->_baseUrl) {
  753. $this->_request->setBaseUrl($this->_baseUrl);
  754. }
  755. }
  756. /**
  757. * Instantiate default response object (HTTP version) if none provided
  758. */
  759. if (null !== $response) {
  760. $this->setResponse($response);
  761. } elseif ((null === $this->_response) && (null === ($this->_response = $this->getResponse()))) {
  762. require_once 'Zend/Controller/Response/Http.php';
  763. $response = new Zend_Controller_Response_Http();
  764. $this->setResponse($response);
  765. }
  766. /**
  767. * Register request and response objects with plugin broker
  768. */
  769. $this->_plugins
  770. ->setRequest($this->_request)
  771. ->setResponse($this->_response);
  772. /**
  773. * Initialize router
  774. */
  775. $router = $this->getRouter();
  776. $router->setParams($this->getParams());
  777. /**
  778. * Initialize dispatcher
  779. */
  780. $dispatcher = $this->getDispatcher();
  781. $dispatcher->setParams($this->getParams())
  782. ->setResponse($this->_response);
  783. // Begin dispatch
  784. try {
  785. /**
  786. * Route request to controller/action, if a router is provided
  787. */
  788. /**
  789. * Notify plugins of router startup
  790. */
  791. $this->_plugins->routeStartup($this->_request);
  792. $router->route($this->_request);
  793. /**
  794. * Notify plugins of router completion
  795. */
  796. $this->_plugins->routeShutdown($this->_request);
  797. /**
  798. * Notify plugins of dispatch loop startup
  799. */
  800. $this->_plugins->dispatchLoopStartup($this->_request);
  801. /**
  802. * Attempt to dispatch the controller/action. If the $this->_request
  803. * indicates that it needs to be dispatched, move to the next
  804. * action in the request.
  805. */
  806. do {
  807. $this->_request->setDispatched(true);
  808. /**
  809. * Notify plugins of dispatch startup
  810. */
  811. $this->_plugins->preDispatch($this->_request);
  812. /**
  813. * Skip requested action if preDispatch() has reset it
  814. */
  815. if (!$this->_request->isDispatched()) {
  816. continue;
  817. }
  818. /**
  819. * Dispatch request
  820. */
  821. try {
  822. $dispatcher->dispatch($this->_request, $this->_response);
  823. } catch (Exception $e) {
  824. if ($this->throwExceptions()) {
  825. throw $e;
  826. }
  827. $this->_response->setException($e);
  828. }
  829. /**
  830. * Notify plugins of dispatch completion
  831. */
  832. $this->_plugins->postDispatch($this->_request);
  833. } while (!$this->_request->isDispatched());
  834. } catch (Exception $e) {
  835. if ($this->throwExceptions()) {
  836. throw $e;
  837. }
  838. $this->_response->setException($e);
  839. }
  840. /**
  841. * Notify plugins of dispatch loop completion
  842. */
  843. try {
  844. $this->_plugins->dispatchLoopShutdown();
  845. } catch (Exception $e) {
  846. if ($this->throwExceptions()) {
  847. throw $e;
  848. }
  849. $this->_response->setException($e);
  850. }
  851. if ($this->returnResponse()) {
  852. return $this->_response;
  853. }
  854. $this->_response->sendResponse();
  855. }
  856. }