PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/sluther/portsensor
PHP | 671 lines | 249 code | 72 blank | 350 comment | 34 complexity | 9256133cafc72d626948e1021ad17c65 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_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. if (null !== $noController) {
  235. $viewRenderer->setNoController($noController);
  236. }
  237. return $viewRenderer->getViewScript($action);
  238. }
  239. $request = $this->getRequest();
  240. if (null === $action) {
  241. $action = $request->getActionName();
  242. } elseif (!is_string($action)) {
  243. throw new Zend_Controller_Exception('Invalid action specifier for view render');
  244. }
  245. if (null === $this->_delimiters) {
  246. $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
  247. $wordDelimiters = $dispatcher->getWordDelimiter();
  248. $pathDelimiters = $dispatcher->getPathDelimiter();
  249. $this->_delimiters = array_unique(array_merge($wordDelimiters, (array) $pathDelimiters));
  250. }
  251. $action = str_replace($this->_delimiters, '-', $action);
  252. $script = $action . '.' . $this->viewSuffix;
  253. if (!$noController) {
  254. $controller = $request->getControllerName();
  255. $controller = str_replace($this->_delimiters, '-', $controller);
  256. $script = $controller . DIRECTORY_SEPARATOR . $script;
  257. }
  258. return $script;
  259. }
  260. /**
  261. * Return the Request object
  262. *
  263. * @return Zend_Controller_Request_Abstract
  264. */
  265. public function getRequest()
  266. {
  267. return $this->_request;
  268. }
  269. /**
  270. * Set the Request object
  271. *
  272. * @param Zend_Controller_Request_Abstract $request
  273. * @return Zend_Controller_Action
  274. */
  275. public function setRequest(Zend_Controller_Request_Abstract $request)
  276. {
  277. $this->_request = $request;
  278. return $this;
  279. }
  280. /**
  281. * Return the Response object
  282. *
  283. * @return Zend_Controller_Response_Abstract
  284. */
  285. public function getResponse()
  286. {
  287. return $this->_response;
  288. }
  289. /**
  290. * Set the Response object
  291. *
  292. * @param Zend_Controller_Response_Abstract $response
  293. * @return Zend_Controller_Action
  294. */
  295. public function setResponse(Zend_Controller_Response_Abstract $response)
  296. {
  297. $this->_response = $response;
  298. return $this;
  299. }
  300. /**
  301. * Set invocation arguments
  302. *
  303. * @param array $args
  304. * @return Zend_Controller_Action
  305. */
  306. protected function _setInvokeArgs(array $args = array())
  307. {
  308. $this->_invokeArgs = $args;
  309. return $this;
  310. }
  311. /**
  312. * Return the array of constructor arguments (minus the Request object)
  313. *
  314. * @return array
  315. */
  316. public function getInvokeArgs()
  317. {
  318. return $this->_invokeArgs;
  319. }
  320. /**
  321. * Return a single invocation argument
  322. *
  323. * @param string $key
  324. * @return mixed
  325. */
  326. public function getInvokeArg($key)
  327. {
  328. if (isset($this->_invokeArgs[$key])) {
  329. return $this->_invokeArgs[$key];
  330. }
  331. return null;
  332. }
  333. /**
  334. * Get a helper by name
  335. *
  336. * @param string $helperName
  337. * @return Zend_Controller_Action_Helper_Abstract
  338. */
  339. public function getHelper($helperName)
  340. {
  341. return $this->_helper->{$helperName};
  342. }
  343. /**
  344. * Get a clone of a helper by name
  345. *
  346. * @param string $helperName
  347. * @return Zend_Controller_Action_Helper_Abstract
  348. */
  349. public function getHelperCopy($helperName)
  350. {
  351. return clone $this->_helper->{$helperName};
  352. }
  353. /**
  354. * Set the front controller instance
  355. *
  356. * @param Zend_Controller_Front $front
  357. * @return Zend_Controller_Action
  358. */
  359. public function setFrontController(Zend_Controller_Front $front)
  360. {
  361. $this->_frontController = $front;
  362. return $this;
  363. }
  364. /**
  365. * Retrieve Front Controller
  366. *
  367. * @return Zend_Controller_Front
  368. */
  369. public function getFrontController()
  370. {
  371. // Used cache version if found
  372. if (null !== $this->_frontController) {
  373. return $this->_frontController;
  374. }
  375. // Grab singleton instance, if class has been loaded
  376. if (class_exists('Zend_Controller_Front')) {
  377. $this->_frontController = Zend_Controller_Front::getInstance();
  378. return $this->_frontController;
  379. }
  380. // Throw exception in all other cases
  381. require_once 'Zend/Controller/Exception.php';
  382. throw new Zend_Controller_Exception('Front controller class has not been loaded');
  383. }
  384. /**
  385. * Pre-dispatch routines
  386. *
  387. * Called before action method. If using class with
  388. * {@link Zend_Controller_Front}, it may modify the
  389. * {@link $_request Request object} and reset its dispatched flag in order
  390. * to skip processing the current action.
  391. *
  392. * @return void
  393. */
  394. public function preDispatch()
  395. {
  396. }
  397. /**
  398. * Post-dispatch routines
  399. *
  400. * Called after action method execution. If using class with
  401. * {@link Zend_Controller_Front}, it may modify the
  402. * {@link $_request Request object} and reset its dispatched flag in order
  403. * to process an additional action.
  404. *
  405. * Common usages for postDispatch() include rendering content in a sitewide
  406. * template, link url correction, setting headers, etc.
  407. *
  408. * @return void
  409. */
  410. public function postDispatch()
  411. {
  412. }
  413. /**
  414. * Proxy for undefined methods. Default behavior is to throw an
  415. * exception on undefined methods, however this function can be
  416. * overridden to implement magic (dynamic) actions, or provide run-time
  417. * dispatching.
  418. *
  419. * @param string $methodName
  420. * @param array $args
  421. */
  422. public function __call($methodName, $args)
  423. {
  424. if (empty($methodName)) {
  425. $msg = 'No action specified and no default action has been defined in __call() for '
  426. . get_class($this);
  427. } else {
  428. $msg = get_class($this) . '::' . $methodName
  429. .'() does not exist and was not trapped in __call()';
  430. }
  431. throw new Zend_Controller_Action_Exception($msg);
  432. }
  433. /**
  434. * Dispatch the requested action
  435. *
  436. * @param string $action Method name of action
  437. * @return void
  438. */
  439. public function dispatch($action)
  440. {
  441. // Notify helpers of action preDispatch state
  442. $this->_helper->notifyPreDispatch();
  443. $this->preDispatch();
  444. if ($this->getRequest()->isDispatched()) {
  445. // preDispatch() didn't change the action, so we can continue
  446. $this->$action();
  447. $this->postDispatch();
  448. }
  449. // whats actually important here is that this action controller is
  450. // shutting down, regardless of dispatching; notify the helpers of this
  451. // state
  452. $this->_helper->notifyPostDispatch();
  453. }
  454. /**
  455. * Call the action specified in the request object, and return a response
  456. *
  457. * Not used in the Action Controller implementation, but left for usage in
  458. * Page Controller implementations. Dispatches a method based on the
  459. * request.
  460. *
  461. * Returns a Zend_Controller_Response_Abstract object, instantiating one
  462. * prior to execution if none exists in the controller.
  463. *
  464. * {@link preDispatch()} is called prior to the action,
  465. * {@link postDispatch()} is called following it.
  466. *
  467. * @param null|Zend_Controller_Request_Abstract $request Optional request
  468. * object to use
  469. * @param null|Zend_Controller_Response_Abstract $response Optional response
  470. * object to use
  471. * @return Zend_Controller_Response_Abstract
  472. */
  473. public function run(Zend_Controller_Request_Abstract $request = null, Zend_Controller_Response_Abstract $response = null)
  474. {
  475. if (null !== $request) {
  476. $this->setRequest($request);
  477. } else {
  478. $request = $this->getRequest();
  479. }
  480. if (null !== $response) {
  481. $this->setResponse($response);
  482. }
  483. $action = $request->getActionName();
  484. if (empty($action)) {
  485. $action = 'index';
  486. }
  487. $action = $action . 'Action';
  488. $request->setDispatched(true);
  489. $this->dispatch($action);
  490. return $this->getResponse();
  491. }
  492. /**
  493. * Gets a parameter from the {@link $_request Request object}. If the
  494. * parameter does not exist, NULL will be returned.
  495. *
  496. * If the parameter does not exist and $default is set, then
  497. * $default will be returned instead of NULL.
  498. *
  499. * @param string $paramName
  500. * @param mixed $default
  501. * @return mixed
  502. */
  503. final protected function _getParam($paramName, $default = null)
  504. {
  505. $value = $this->getRequest()->getParam($paramName);
  506. if ((null == $value) && (null !== $default)) {
  507. $value = $default;
  508. }
  509. return $value;
  510. }
  511. /**
  512. * Set a parameter in the {@link $_request Request object}.
  513. *
  514. * @param string $paramName
  515. * @param mixed $value
  516. * @return Zend_Controller_Action
  517. */
  518. final protected function _setParam($paramName, $value)
  519. {
  520. $this->getRequest()->setParam($paramName, $value);
  521. return $this;
  522. }
  523. /**
  524. * Determine whether a given parameter exists in the
  525. * {@link $_request Request object}.
  526. *
  527. * @param string $paramName
  528. * @return boolean
  529. */
  530. final protected function _hasParam($paramName)
  531. {
  532. return null !== $this->getRequest()->getParam($paramName);
  533. }
  534. /**
  535. * Return all parameters in the {@link $_request Request object}
  536. * as an associative array.
  537. *
  538. * @return array
  539. */
  540. final protected function _getAllParams()
  541. {
  542. return $this->getRequest()->getParams();
  543. }
  544. /**
  545. * Forward to another controller/action.
  546. *
  547. * It is important to supply the unformatted names, i.e. "article"
  548. * rather than "ArticleController". The dispatcher will do the
  549. * appropriate formatting when the request is received.
  550. *
  551. * If only an action name is provided, forwards to that action in this
  552. * controller.
  553. *
  554. * If an action and controller are specified, forwards to that action and
  555. * controller in this module.
  556. *
  557. * Specifying an action, controller, and module is the most specific way to
  558. * forward.
  559. *
  560. * A fourth argument, $params, will be used to set the request parameters.
  561. * If either the controller or module are unnecessary for forwarding,
  562. * simply pass null values for them before specifying the parameters.
  563. *
  564. * @param string $action
  565. * @param string $controller
  566. * @param string $module
  567. * @param array $params
  568. * @return void
  569. */
  570. final protected function _forward($action, $controller = null, $module = null, array $params = null)
  571. {
  572. $request = $this->getRequest();
  573. if (null !== $params) {
  574. $request->setParams($params);
  575. }
  576. if (null !== $controller) {
  577. $request->setControllerName($controller);
  578. // Module should only be reset if controller has been specified
  579. if (null !== $module) {
  580. $request->setModuleName($module);
  581. }
  582. }
  583. $request->setActionName($action)
  584. ->setDispatched(false);
  585. }
  586. /**
  587. * Redirect to another URL
  588. *
  589. * Proxies to {@link Zend_Controller_Action_Helper_Redirector::gotoUrl()}.
  590. *
  591. * @param string $url
  592. * @param array $options Options to be used when redirecting
  593. * @return void
  594. */
  595. protected function _redirect($url, array $options = array())
  596. {
  597. $this->_helper->redirector->gotoUrl($url, $options);
  598. }
  599. }