2
0

Front.php 28 KB

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