Abstract.php 7.3 KB

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