PageRenderTime 57ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/app/sitesearch/lib/Zend/Controller/Action.php

https://github.com/lux/sitellite
PHP | 669 lines | 247 code | 72 blank | 350 comment | 33 complexity | 943358e85f6df455f254d59d90588e03 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, GPL-3.0
  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_Controller_Exception */
  21. require_once 'Zend/Controller/Action/Exception.php';
  22. /** Zend_Controller_Action_HelperBroker */
  23. require_once 'Zend/Controller/Action/HelperBroker.php';
  24. /** Zend_Controller_Front */
  25. require_once 'Zend/Controller/Front.php';
  26. /** Zend_Controller_Request_Abstract */
  27. require_once 'Zend/Controller/Request/Abstract.php';
  28. /** Zend_Controller_Response_Abstract */
  29. require_once 'Zend/Controller/Response/Abstract.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Controller
  33. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. abstract class Zend_Controller_Action
  37. {
  38. /**
  39. * Word delimiters (used for normalizing view script paths)
  40. * @var array
  41. */
  42. protected $_delimiters;
  43. /**
  44. * Array of arguments provided to the constructor, minus the
  45. * {@link $_request Request object}.
  46. * @var array
  47. */
  48. protected $_invokeArgs = array();
  49. /**
  50. * Front controller instance
  51. * @var Zend_Controller_Front
  52. */
  53. protected $_frontController;
  54. /**
  55. * Zend_Controller_Request_Abstract object wrapping the request environment
  56. * @var Zend_Controller_Request_Abstract
  57. */
  58. protected $_request = null;
  59. /**
  60. * Zend_Controller_Response_Abstract object wrapping the response
  61. * @var Zend_Controller_Response_Abstract
  62. */
  63. protected $_response = null;
  64. /**
  65. * View script suffix; defaults to 'phtml'
  66. * @see {render()}
  67. * @var string
  68. */
  69. public $viewSuffix = 'phtml';
  70. /**
  71. * View object
  72. * @var Zend_View_Interface
  73. */
  74. public $view;
  75. /**
  76. * Helper Broker to assist in routing help requests to the proper object
  77. *
  78. * @var Zend_Controller_Action_HelperBroker
  79. */
  80. protected $_helper = null;
  81. /**
  82. * Class constructor
  83. *
  84. * The request and response objects should be registered with the
  85. * controller, as should be any additional optional arguments; these will be
  86. * available via {@link getRequest()}, {@link getResponse()}, and
  87. * {@link getInvokeArgs()}, respectively.
  88. *
  89. * When overriding the constructor, please consider this usage as a best
  90. * practice and ensure that each is registered appropriately; the easiest
  91. * way to do so is to simply call parent::__construct($request, $response,
  92. * $invokeArgs).
  93. *
  94. * After the request, response, and invokeArgs are set, the
  95. * {@link $_helper helper broker} is initialized.
  96. *
  97. * Finally, {@link init()} is called as the final action of
  98. * instantiation, and may be safely overridden to perform initialization
  99. * tasks; as a general rule, override {@link init()} instead of the
  100. * constructor to customize an action controller's instantiation.
  101. *
  102. * @param Zend_Controller_Request_Abstract $request
  103. * @param Zend_Controller_Response_Abstract $response
  104. * @param array $invokeArgs Any additional invocation arguments
  105. * @return void
  106. */
  107. public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array())
  108. {
  109. $this->setRequest($request)
  110. ->setResponse($response)
  111. ->_setInvokeArgs($invokeArgs);
  112. $this->_helper = new Zend_Controller_Action_HelperBroker($this);
  113. $this->init();
  114. }
  115. /**
  116. * Initialize object
  117. *
  118. * Called from {@link __construct()} as final step of object instantiation.
  119. *
  120. * @return void
  121. */
  122. public function init()
  123. {
  124. }
  125. /**
  126. * Initialize View object
  127. *
  128. * Initializes {@link $view} if not otherwise a Zend_View_Interface.
  129. *
  130. * If {@link $view} is not otherwise set, instantiates a new Zend_View
  131. * object, using the 'views' subdirectory at the same level as the
  132. * controller directory for the current module as the base directory.
  133. * It uses this to set the following:
  134. * - script path = views/scripts/
  135. * - helper path = views/helpers/
  136. * - filter path = views/filters/
  137. *
  138. * @return Zend_View_Interface
  139. * @throws Zend_Controller_Exception if base view directory does not exist
  140. */
  141. public function initView()
  142. {
  143. if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) {
  144. return $this->view;
  145. }
  146. require_once 'Zend/View/Interface.php';
  147. if (isset($this->view) && ($this->view instanceof Zend_View_Interface)) {
  148. return $this->view;
  149. }
  150. $request = $this->getRequest();
  151. $module = $request->getModuleName();
  152. $dirs = $this->getFrontController()->getControllerDirectory();
  153. if (empty($module) || !isset($dirs[$module])) {
  154. $module = $this->getFrontController()->getDispatcher()->getDefaultModule();
  155. }
  156. $baseDir = dirname($dirs[$module]) . DIRECTORY_SEPARATOR . 'views';
  157. if (!file_exists($baseDir) || !is_dir($baseDir)) {
  158. throw new Zend_Controller_Exception('Missing base view directory ("' . $baseDir . '")');
  159. }
  160. require_once 'Zend/View.php';
  161. $this->view = new Zend_View(array('basePath' => $baseDir));
  162. return $this->view;
  163. }
  164. /**
  165. * Render a view
  166. *
  167. * Renders a view. By default, views are found in the view script path as
  168. * <controller>/<action>.phtml. You may change the script suffix by
  169. * resetting {@link $viewSuffix}. You may omit the controller directory
  170. * prefix by specifying boolean true for $noController.
  171. *
  172. * By default, the rendered contents are appended to the response. You may
  173. * specify the named body content segment to set by specifying a $name.
  174. *
  175. * @see Zend_Controller_Response_Abstract::appendBody()
  176. * @param string|null $action Defaults to action registered in request object
  177. * @param string|null $name Response object named path segment to use; defaults to null
  178. * @param bool $noController Defaults to false; i.e. use controller name as subdir in which to search for view script
  179. * @return void
  180. */
  181. public function render($action = null, $name = null, $noController = false)
  182. {
  183. if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) {
  184. return $this->_helper->viewRenderer->render($action, $name, $noController);
  185. }
  186. $view = $this->initView();
  187. $script = $this->getViewScript($action, $noController);
  188. $this->getResponse()->appendBody(
  189. $view->render($script),
  190. $name
  191. );
  192. }
  193. /**
  194. * Render a given view script
  195. *
  196. * Similar to {@link render()}, this method renders a view script. Unlike render(),
  197. * however, it does not autodetermine the view script via {@link getViewScript()},
  198. * but instead renders the script passed to it. Use this if you know the
  199. * exact view script name and path you wish to use, or if using paths that do not
  200. * conform to the spec defined with getViewScript().
  201. *
  202. * By default, the rendered contents are appended to the response. You may
  203. * specify the named body content segment to set by specifying a $name.
  204. *
  205. * @param string $script
  206. * @param string $name
  207. * @return void
  208. */
  209. public function renderScript($script, $name = null)
  210. {
  211. if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) {
  212. return $this->_helper->viewRenderer->renderScript($script, $name);
  213. }
  214. $view = $this->initView();
  215. $this->getResponse()->appendBody(
  216. $view->render($script),
  217. $name
  218. );
  219. }
  220. /**
  221. * Construct view script path
  222. *
  223. * Used by render() to determine the path to the view script.
  224. *
  225. * @param string $action Defaults to action registered in request object
  226. * @param bool $noController Defaults to false; i.e. use controller name as subdir in which to search for view script
  227. * @return string
  228. * @throws Zend_Controller_Exception with bad $action
  229. */
  230. public function getViewScript($action = null, $noController = null)
  231. {
  232. if (!$this->getInvokeArg('noViewRenderer') && $this->_helper->hasHelper('viewRenderer')) {
  233. $viewRenderer = $this->_helper->getHelper('viewRenderer');
  234. $viewRenderer->setNoController($noController);
  235. return $viewRenderer->getViewScript($action);
  236. }
  237. $request = $this->getRequest();
  238. if (null === $action) {
  239. $action = $request->getActionName();
  240. } elseif (!is_string($action)) {
  241. throw new Zend_Controller_Exception('Invalid action specifier for view render');
  242. }
  243. if (null === $this->_delimiters) {
  244. $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
  245. $wordDelimiters = $dispatcher->getWordDelimiter();
  246. $pathDelimiters = $dispatcher->getPathDelimiter();
  247. $this->_delimiters = array_unique(array_merge($wordDelimiters, (array) $pathDelimiters));
  248. }
  249. $action = str_replace($this->_delimiters, '-', $action);
  250. $script = $action . '.' . $this->viewSuffix;
  251. if (!$noController) {
  252. $controller = $request->getControllerName();
  253. $controller = str_replace($this->_delimiters, '-', $controller);
  254. $script = $controller . DIRECTORY_SEPARATOR . $script;
  255. }
  256. return $script;
  257. }
  258. /**
  259. * Return the Request object
  260. *
  261. * @return Zend_Controller_Request_Abstract
  262. */
  263. public function getRequest()
  264. {
  265. return $this->_request;
  266. }
  267. /**
  268. * Set the Request object
  269. *
  270. * @param Zend_Controller_Request_Abstract $request
  271. * @return Zend_Controller_Action
  272. */
  273. public function setRequest(Zend_Controller_Request_Abstract $request)
  274. {
  275. $this->_request = $request;
  276. return $this;
  277. }
  278. /**
  279. * Return the Response object
  280. *
  281. * @return Zend_Controller_Response_Abstract
  282. */
  283. public function getResponse()
  284. {
  285. return $this->_response;
  286. }
  287. /**
  288. * Set the Response object
  289. *
  290. * @param Zend_Controller_Response_Abstract $response
  291. * @return Zend_Controller_Action
  292. */
  293. public function setResponse(Zend_Controller_Response_Abstract $response)
  294. {
  295. $this->_response = $response;
  296. return $this;
  297. }
  298. /**
  299. * Set invocation arguments
  300. *
  301. * @param array $args
  302. * @return Zend_Controller_Action
  303. */
  304. protected function _setInvokeArgs(array $args = array())
  305. {
  306. $this->_invokeArgs = $args;
  307. return $this;
  308. }
  309. /**
  310. * Return the array of constructor arguments (minus the Request object)
  311. *
  312. * @return array
  313. */
  314. public function getInvokeArgs()
  315. {
  316. return $this->_invokeArgs;
  317. }
  318. /**
  319. * Return a single invocation argument
  320. *
  321. * @param string $key
  322. * @return mixed
  323. */
  324. public function getInvokeArg($key)
  325. {
  326. if (isset($this->_invokeArgs[$key])) {
  327. return $this->_invokeArgs[$key];
  328. }
  329. return null;
  330. }
  331. /**
  332. * Get a helper by name
  333. *
  334. * @param string $helperName
  335. * @return Zend_Controller_Action_Helper_Abstract
  336. */
  337. public function getHelper($helperName)
  338. {
  339. return $this->_helper->{$helperName};
  340. }
  341. /**
  342. * Get a clone of a helper by name
  343. *
  344. * @param string $helperName
  345. * @return Zend_Controller_Action_Helper_Abstract
  346. */
  347. public function getHelperCopy($helperName)
  348. {
  349. return clone $this->_helper->{$helperName};
  350. }
  351. /**
  352. * Set the front controller instance
  353. *
  354. * @param Zend_Controller_Front $front
  355. * @return Zend_Controller_Action
  356. */
  357. public function setFrontController(Zend_Controller_Front $front)
  358. {
  359. $this->_frontController = $front;
  360. return $this;
  361. }
  362. /**
  363. * Retrieve Front Controller
  364. *
  365. * @return Zend_Controller_Front
  366. */
  367. public function getFrontController()
  368. {
  369. // Used cache version if found
  370. if (null !== $this->_frontController) {
  371. return $this->_frontController;
  372. }
  373. // Grab singleton instance, if class has been loaded
  374. if (class_exists('Zend_Controller_Front')) {
  375. $this->_frontController = Zend_Controller_Front::getInstance();
  376. return $this->_frontController;
  377. }
  378. // Throw exception in all other cases
  379. require_once 'Zend/Controller/Exception.php';
  380. throw new Zend_Controller_Exception('Front controller class has not been loaded');
  381. }
  382. /**
  383. * Pre-dispatch routines
  384. *
  385. * Called before action method. If using class with
  386. * {@link Zend_Controller_Front}, it may modify the
  387. * {@link $_request Request object} and reset its dispatched flag in order
  388. * to skip processing the current action.
  389. *
  390. * @return void
  391. */
  392. public function preDispatch()
  393. {
  394. }
  395. /**
  396. * Post-dispatch routines
  397. *
  398. * Called after action method execution. If using class with
  399. * {@link Zend_Controller_Front}, it may modify the
  400. * {@link $_request Request object} and reset its dispatched flag in order
  401. * to process an additional action.
  402. *
  403. * Common usages for postDispatch() include rendering content in a sitewide
  404. * template, link url correction, setting headers, etc.
  405. *
  406. * @return void
  407. */
  408. public function postDispatch()
  409. {
  410. }
  411. /**
  412. * Proxy for undefined methods. Default behavior is to throw an
  413. * exception on undefined methods, however this function can be
  414. * overridden to implement magic (dynamic) actions, or provide run-time
  415. * dispatching.
  416. *
  417. * @param string $methodName
  418. * @param array $args
  419. */
  420. public function __call($methodName, $args)
  421. {
  422. if (empty($methodName)) {
  423. $msg = 'No action specified and no default action has been defined in __call() for '
  424. . get_class($this);
  425. } else {
  426. $msg = get_class($this) . '::' . $methodName
  427. .'() does not exist and was not trapped in __call()';
  428. }
  429. throw new Zend_Controller_Action_Exception($msg);
  430. }
  431. /**
  432. * Dispatch the requested action
  433. *
  434. * @param string $action Method name of action
  435. * @return void
  436. */
  437. public function dispatch($action)
  438. {
  439. // Notify helpers of action preDispatch state
  440. $this->_helper->notifyPreDispatch();
  441. $this->preDispatch();
  442. if ($this->getRequest()->isDispatched()) {
  443. // preDispatch() didn't change the action, so we can continue
  444. $this->$action();
  445. $this->postDispatch();
  446. }
  447. // whats actually important here is that this action controller is
  448. // shutting down, regardless of dispatching; notify the helpers of this
  449. // state
  450. $this->_helper->notifyPostDispatch();
  451. }
  452. /**
  453. * Call the action specified in the request object, and return a response
  454. *
  455. * Not used in the Action Controller implementation, but left for usage in
  456. * Page Controller implementations. Dispatches a method based on the
  457. * request.
  458. *
  459. * Returns a Zend_Controller_Response_Abstract object, instantiating one
  460. * prior to execution if none exists in the controller.
  461. *
  462. * {@link preDispatch()} is called prior to the action,
  463. * {@link postDispatch()} is called following it.
  464. *
  465. * @param null|Zend_Controller_Request_Abstract $request Optional request
  466. * object to use
  467. * @param null|Zend_Controller_Response_Abstract $response Optional response
  468. * object to use
  469. * @return Zend_Controller_Response_Abstract
  470. */
  471. public function run(Zend_Controller_Request_Abstract $request = null, Zend_Controller_Response_Abstract $response = null)
  472. {
  473. if (null !== $request) {
  474. $this->setRequest($request);
  475. } else {
  476. $request = $this->getRequest();
  477. }
  478. if (null !== $response) {
  479. $this->setResponse($response);
  480. }
  481. $action = $request->getActionName();
  482. if (empty($action)) {
  483. $action = 'index';
  484. }
  485. $action = $action . 'Action';
  486. $request->setDispatched(true);
  487. $this->dispatch($action);
  488. return $this->getResponse();
  489. }
  490. /**
  491. * Gets a parameter from the {@link $_request Request object}. If the
  492. * parameter does not exist, NULL will be returned.
  493. *
  494. * If the parameter does not exist and $default is set, then
  495. * $default will be returned instead of NULL.
  496. *
  497. * @param string $paramName
  498. * @param mixed $default
  499. * @return mixed
  500. */
  501. final protected function _getParam($paramName, $default = null)
  502. {
  503. $value = $this->getRequest()->getParam($paramName);
  504. if ((null == $value) && (null !== $default)) {
  505. $value = $default;
  506. }
  507. return $value;
  508. }
  509. /**
  510. * Set a parameter in the {@link $_request Request object}.
  511. *
  512. * @param string $paramName
  513. * @param mixed $value
  514. * @return Zend_Controller_Action
  515. */
  516. final protected function _setParam($paramName, $value)
  517. {
  518. $this->getRequest()->setParam($paramName, $value);
  519. return $this;
  520. }
  521. /**
  522. * Determine whether a given parameter exists in the
  523. * {@link $_request Request object}.
  524. *
  525. * @param string $paramName
  526. * @return boolean
  527. */
  528. final protected function _hasParam($paramName)
  529. {
  530. return null !== $this->getRequest()->getParam($paramName);
  531. }
  532. /**
  533. * Return all parameters in the {@link $_request Request object}
  534. * as an associative array.
  535. *
  536. * @return array
  537. */
  538. final protected function _getAllParams()
  539. {
  540. return $this->getRequest()->getParams();
  541. }
  542. /**
  543. * Forward to another controller/action.
  544. *
  545. * It is important to supply the unformatted names, i.e. "article"
  546. * rather than "ArticleController". The dispatcher will do the
  547. * appropriate formatting when the request is received.
  548. *
  549. * If only an action name is provided, forwards to that action in this
  550. * controller.
  551. *
  552. * If an action and controller are specified, forwards to that action and
  553. * controller in this module.
  554. *
  555. * Specifying an action, controller, and module is the most specific way to
  556. * forward.
  557. *
  558. * A fourth argument, $params, will be used to set the request parameters.
  559. * If either the controller or module are unnecessary for forwarding,
  560. * simply pass null values for them before specifying the parameters.
  561. *
  562. * @param string $action
  563. * @param string $controller
  564. * @param string $module
  565. * @param array $params
  566. * @return void
  567. */
  568. final protected function _forward($action, $controller = null, $module = null, array $params = null)
  569. {
  570. $request = $this->getRequest();
  571. if (null !== $params) {
  572. $request->setParams($params);
  573. }
  574. if (null !== $controller) {
  575. $request->setControllerName($controller);
  576. // Module should only be reset if controller has been specified
  577. if (null !== $module) {
  578. $request->setModuleName($module);
  579. }
  580. }
  581. $request->setActionName($action)
  582. ->setDispatched(false);
  583. }
  584. /**
  585. * Redirect to another URL
  586. *
  587. * Proxies to {@link Zend_Controller_Action_Helper_Redirector::gotoUrl()}.
  588. *
  589. * @param string $url
  590. * @param array $options Options to be used when redirecting
  591. * @return void
  592. */
  593. protected function _redirect($url, array $options = array())
  594. {
  595. $this->_helper->redirector->gotoUrl($url, $options);
  596. }
  597. }