Front.php 28 KB

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