Abstract.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. /**
  22. * @category Zend
  23. * @package Zend_Controller
  24. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. abstract class Zend_Controller_Request_Abstract
  28. {
  29. /**
  30. * Has the action been dispatched?
  31. * @var boolean
  32. */
  33. protected $_dispatched = false;
  34. /**
  35. * Module
  36. * @var string
  37. */
  38. protected $_module;
  39. /**
  40. * Module key for retrieving module from params
  41. * @var string
  42. */
  43. protected $_moduleKey = 'module';
  44. /**
  45. * Controller
  46. * @var string
  47. */
  48. protected $_controller;
  49. /**
  50. * Controller key for retrieving controller from params
  51. * @var string
  52. */
  53. protected $_controllerKey = 'controller';
  54. /**
  55. * Action
  56. * @var string
  57. */
  58. protected $_action;
  59. /**
  60. * Action key for retrieving action from params
  61. * @var string
  62. */
  63. protected $_actionKey = 'action';
  64. /**
  65. * Request parameters
  66. * @var array
  67. */
  68. protected $_params = array();
  69. /**
  70. * Retrieve the module name
  71. *
  72. * @return string
  73. */
  74. public function getModuleName()
  75. {
  76. if (null === $this->_module) {
  77. $this->_module = $this->getParam($this->getModuleKey());
  78. }
  79. return $this->_module;
  80. }
  81. /**
  82. * Set the module name to use
  83. *
  84. * @param string $value
  85. * @return Zend_Controller_Request_Abstract
  86. */
  87. public function setModuleName($value)
  88. {
  89. $this->_module = $value;
  90. return $this;
  91. }
  92. /**
  93. * Retrieve the controller name
  94. *
  95. * @return string
  96. */
  97. public function getControllerName()
  98. {
  99. if (null === $this->_controller) {
  100. $this->_controller = $this->getParam($this->getControllerKey());
  101. }
  102. return $this->_controller;
  103. }
  104. /**
  105. * Set the controller name to use
  106. *
  107. * @param string $value
  108. * @return Zend_Controller_Request_Abstract
  109. */
  110. public function setControllerName($value)
  111. {
  112. $this->_controller = $value;
  113. return $this;
  114. }
  115. /**
  116. * Retrieve the action name
  117. *
  118. * @return string
  119. */
  120. public function getActionName()
  121. {
  122. if (null === $this->_action) {
  123. $this->_action = $this->getParam($this->getActionKey());
  124. }
  125. return $this->_action;
  126. }
  127. /**
  128. * Set the action name
  129. *
  130. * @param string $value
  131. * @return Zend_Controller_Request_Abstract
  132. */
  133. public function setActionName($value)
  134. {
  135. $this->_action = $value;
  136. /**
  137. * @see ZF-3465
  138. */
  139. if (null === $value) {
  140. $this->setParam($this->getActionKey(), $value);
  141. }
  142. return $this;
  143. }
  144. /**
  145. * Retrieve the module key
  146. *
  147. * @return string
  148. */
  149. public function getModuleKey()
  150. {
  151. return $this->_moduleKey;
  152. }
  153. /**
  154. * Set the module key
  155. *
  156. * @param string $key
  157. * @return Zend_Controller_Request_Abstract
  158. */
  159. public function setModuleKey($key)
  160. {
  161. $this->_moduleKey = (string) $key;
  162. return $this;
  163. }
  164. /**
  165. * Retrieve the controller key
  166. *
  167. * @return string
  168. */
  169. public function getControllerKey()
  170. {
  171. return $this->_controllerKey;
  172. }
  173. /**
  174. * Set the controller key
  175. *
  176. * @param string $key
  177. * @return Zend_Controller_Request_Abstract
  178. */
  179. public function setControllerKey($key)
  180. {
  181. $this->_controllerKey = (string) $key;
  182. return $this;
  183. }
  184. /**
  185. * Retrieve the action key
  186. *
  187. * @return string
  188. */
  189. public function getActionKey()
  190. {
  191. return $this->_actionKey;
  192. }
  193. /**
  194. * Set the action key
  195. *
  196. * @param string $key
  197. * @return Zend_Controller_Request_Abstract
  198. */
  199. public function setActionKey($key)
  200. {
  201. $this->_actionKey = (string) $key;
  202. return $this;
  203. }
  204. /**
  205. * Get an action parameter
  206. *
  207. * @param string $key
  208. * @param mixed $default Default value to use if key not found
  209. * @return mixed
  210. */
  211. public function getParam($key, $default = null)
  212. {
  213. $key = (string) $key;
  214. if (isset($this->_params[$key])) {
  215. return $this->_params[$key];
  216. }
  217. return $default;
  218. }
  219. /**
  220. * Retrieve only user params (i.e, any param specific to the object and not the environment)
  221. *
  222. * @return array
  223. */
  224. public function getUserParams()
  225. {
  226. return $this->_params;
  227. }
  228. /**
  229. * Retrieve a single user param (i.e, a param specific to the object and not the environment)
  230. *
  231. * @param string $key
  232. * @param string $default Default value to use if key not found
  233. * @return mixed
  234. */
  235. public function getUserParam($key, $default = null)
  236. {
  237. if (isset($this->_params[$key])) {
  238. return $this->_params[$key];
  239. }
  240. return $default;
  241. }
  242. /**
  243. * Set an action parameter
  244. *
  245. * A $value of null will unset the $key if it exists
  246. *
  247. * @param string $key
  248. * @param mixed $value
  249. * @return Zend_Controller_Request_Abstract
  250. */
  251. public function setParam($key, $value)
  252. {
  253. $key = (string) $key;
  254. if ((null === $value) && isset($this->_params[$key])) {
  255. unset($this->_params[$key]);
  256. } elseif (null !== $value) {
  257. $this->_params[$key] = $value;
  258. }
  259. return $this;
  260. }
  261. /**
  262. * Get all action parameters
  263. *
  264. * @return array
  265. */
  266. public function getParams()
  267. {
  268. return $this->_params;
  269. }
  270. /**
  271. * Set action parameters en masse; does not overwrite
  272. *
  273. * Null values will unset the associated key.
  274. *
  275. * @param array $array
  276. * @return Zend_Controller_Request_Abstract
  277. */
  278. public function setParams(array $array)
  279. {
  280. $this->_params = $this->_params + (array) $array;
  281. foreach ($array as $key => $value) {
  282. if (null === $value) {
  283. unset($this->_params[$key]);
  284. }
  285. }
  286. return $this;
  287. }
  288. /**
  289. * Unset all user parameters
  290. *
  291. * @return Zend_Controller_Request_Abstract
  292. */
  293. public function clearParams()
  294. {
  295. $this->_params = array();
  296. return $this;
  297. }
  298. /**
  299. * Set flag indicating whether or not request has been dispatched
  300. *
  301. * @param boolean $flag
  302. * @return Zend_Controller_Request_Abstract
  303. */
  304. public function setDispatched($flag = true)
  305. {
  306. $this->_dispatched = $flag ? true : false;
  307. return $this;
  308. }
  309. /**
  310. * Determine if the request has been dispatched
  311. *
  312. * @return boolean
  313. */
  314. public function isDispatched()
  315. {
  316. return $this->_dispatched;
  317. }
  318. }