sfRequest.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004-2006 Sean Kerr <sean@code-box.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * sfRequest provides methods for manipulating client request information such
  12. * as attributes, and parameters. It is also possible to manipulate the
  13. * request method originally sent by the user.
  14. *
  15. * @package symfony
  16. * @subpackage request
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. * @author Sean Kerr <sean@code-box.org>
  19. * @version SVN: $Id: sfRequest.class.php 9098 2008-05-20 07:57:54Z FabianLange $
  20. */
  21. abstract class sfRequest
  22. {
  23. /**
  24. * Process validation and execution for only GET requests.
  25. *
  26. */
  27. const GET = 2;
  28. /**
  29. * Skip validation and execution for any request method.
  30. *
  31. */
  32. const NONE = 1;
  33. /**
  34. * Process validation and execution for only POST requests.
  35. *
  36. */
  37. const POST = 4;
  38. /**
  39. * Process validation and execution for only PUT requests.
  40. *
  41. */
  42. const PUT = 5;
  43. /**
  44. * Process validation and execution for only DELETE requests.
  45. *
  46. */
  47. const DELETE = 6;
  48. /**
  49. * Process validation and execution for only HEAD requests.
  50. *
  51. */
  52. const HEAD = 7;
  53. protected
  54. $errors = array(),
  55. $dispatcher = null,
  56. $method = null,
  57. $parameterHolder = null,
  58. $config = null,
  59. $attributeHolder = null;
  60. /**
  61. * Class constructor.
  62. *
  63. * @see initialize()
  64. */
  65. public function __construct(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array())
  66. {
  67. $this->initialize($dispatcher, $parameters, $attributes);
  68. }
  69. /**
  70. * Initializes this sfRequest.
  71. *
  72. * @param sfEventDispatcher $dispatcher An sfEventDispatcher instance
  73. * @param array $parameters An associative array of initialization parameters
  74. * @param array $attributes An associative array of initialization attributes
  75. *
  76. * @return bool true, if initialization completes successfully, otherwise false
  77. *
  78. * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfRequest
  79. */
  80. public function initialize(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array())
  81. {
  82. $this->dispatcher = $dispatcher;
  83. // initialize parameter and attribute holders
  84. $this->parameterHolder = new sfParameterHolder();
  85. $this->attributeHolder = new sfParameterHolder();
  86. $this->parameterHolder->add($parameters);
  87. $this->attributeHolder->add($attributes);
  88. }
  89. /**
  90. * Extracts parameter values from the request.
  91. *
  92. * @param array $names An indexed array of parameter names to extract
  93. *
  94. * @return array An associative array of parameters and their values. If
  95. * a specified parameter doesn't exist an empty string will
  96. * be returned for its value
  97. */
  98. public function extractParameters($names)
  99. {
  100. $array = array();
  101. $parameters = $this->parameterHolder->getAll();
  102. foreach ($parameters as $key => $value)
  103. {
  104. if (in_array($key, $names))
  105. {
  106. $array[$key] = $value;
  107. }
  108. }
  109. return $array;
  110. }
  111. /**
  112. * Retrieves an error message.
  113. *
  114. * @param string $name An error name
  115. *
  116. * @return string An error message, if the error exists, otherwise null
  117. */
  118. public function getError($name)
  119. {
  120. if (!sfConfig::get('sf_compat_10'))
  121. {
  122. throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
  123. }
  124. return isset($this->errors[$name]) ? $this->errors[$name] : null;
  125. }
  126. /**
  127. * Retrieves an array of error names.
  128. *
  129. * @return array An indexed array of error names
  130. */
  131. public function getErrorNames()
  132. {
  133. if (!sfConfig::get('sf_compat_10'))
  134. {
  135. throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
  136. }
  137. return array_keys($this->errors);
  138. }
  139. /**
  140. * Retrieves an array of errors.
  141. *
  142. * @return array An associative array of errors
  143. */
  144. public function getErrors()
  145. {
  146. if (!sfConfig::get('sf_compat_10'))
  147. {
  148. throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
  149. }
  150. return $this->errors;
  151. }
  152. /**
  153. * Retrieves this request's method.
  154. *
  155. * @return int One of the following constants:
  156. * - sfRequest::GET
  157. * - sfRequest::POST
  158. */
  159. public function getMethod()
  160. {
  161. return $this->method;
  162. }
  163. /**
  164. * Indicates whether or not an error exists.
  165. *
  166. * @param string $name An error name
  167. *
  168. * @return bool true, if the error exists, otherwise false
  169. */
  170. public function hasError($name)
  171. {
  172. if (!sfConfig::get('sf_compat_10'))
  173. {
  174. throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
  175. }
  176. return array_key_exists($name, $this->errors);
  177. }
  178. /**
  179. * Indicates whether or not any errors exist.
  180. *
  181. * @return bool true, if any error exist, otherwise false
  182. */
  183. public function hasErrors()
  184. {
  185. if (!sfConfig::get('sf_compat_10'))
  186. {
  187. throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
  188. }
  189. return count($this->errors) > 0;
  190. }
  191. /**
  192. * Removes an error.
  193. *
  194. * @param string $name An error name
  195. *
  196. * @return string An error message, if the error was removed, otherwise null
  197. */
  198. public function removeError($name)
  199. {
  200. if (!sfConfig::get('sf_compat_10'))
  201. {
  202. throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
  203. }
  204. $retval = null;
  205. if (isset($this->errors[$name]))
  206. {
  207. $retval = $this->errors[$name];
  208. unset($this->errors[$name]);
  209. }
  210. return $retval;
  211. }
  212. /**
  213. * Sets an error.
  214. *
  215. * @param string $name An error name
  216. * @param string $message An error message
  217. *
  218. */
  219. public function setError($name, $message)
  220. {
  221. if (!sfConfig::get('sf_compat_10'))
  222. {
  223. throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
  224. }
  225. if (sfConfig::get('sf_logging_enabled'))
  226. {
  227. $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Error in form for parameter "%s" (with message "%s")', $name, $message))));
  228. }
  229. $this->errors[$name] = $message;
  230. }
  231. /**
  232. * Sets an array of errors
  233. *
  234. * If an existing error name matches any of the keys in the supplied
  235. * array, the associated message will be overridden.
  236. *
  237. * @param array $erros An associative array of errors and their associated messages
  238. *
  239. */
  240. public function setErrors($errors)
  241. {
  242. if (!sfConfig::get('sf_compat_10'))
  243. {
  244. throw new sfConfigurationException('You must set "compat_10" to true if you want to use this method which is deprecated.');
  245. }
  246. $this->errors = array_merge($this->errors, $errors);
  247. }
  248. /**
  249. * Sets the request method.
  250. *
  251. * @param int $methodCode One of the following constants:
  252. *
  253. * - sfRequest::GET
  254. * - sfRequest::POST
  255. * - sfRequest::PUT
  256. * - sfRequest::DELETE
  257. * - sfRequest::HEAD
  258. *
  259. * @throws <b>sfException</b> - If the specified request method is invalid
  260. */
  261. public function setMethod($methodCode)
  262. {
  263. $available_methods = array(self::GET, self::POST, self::PUT, self::DELETE, self::HEAD, self::NONE);
  264. if (in_array($methodCode, $available_methods))
  265. {
  266. $this->method = $methodCode;
  267. return;
  268. }
  269. // invalid method type
  270. throw new sfException(sprintf('Invalid request method: %s.', $methodCode));
  271. }
  272. /**
  273. * Retrieves the parameters for the current request.
  274. *
  275. * @return sfParameterHolder The parameter holder
  276. */
  277. public function getParameterHolder()
  278. {
  279. return $this->parameterHolder;
  280. }
  281. /**
  282. * Retrieves the attributes holder.
  283. *
  284. * @return sfParameterHolder The attribute holder
  285. */
  286. public function getAttributeHolder()
  287. {
  288. return $this->attributeHolder;
  289. }
  290. /**
  291. * Retrieves an attribute from the current request.
  292. *
  293. * @param string $name Attribute name
  294. * @param string $default Default attribute value
  295. *
  296. * @return mixed An attribute value
  297. */
  298. public function getAttribute($name, $default = null)
  299. {
  300. return $this->attributeHolder->get($name, $default);
  301. }
  302. /**
  303. * Indicates whether or not an attribute exist for the current request.
  304. *
  305. * @param string $name Attribute name
  306. *
  307. * @return bool true, if the attribute exists otherwise false
  308. */
  309. public function hasAttribute($name)
  310. {
  311. return $this->attributeHolder->has($name);
  312. }
  313. /**
  314. * Sets an attribute for the request.
  315. *
  316. * @param string $name Attribute name
  317. * @param string $value Value for the attribute
  318. *
  319. */
  320. public function setAttribute($name, $value)
  321. {
  322. $this->attributeHolder->set($name, $value);
  323. }
  324. /**
  325. * Retrieves a paramater for the current request.
  326. *
  327. * @param string $name Parameter name
  328. * @param string $default Parameter default value
  329. *
  330. */
  331. public function getParameter($name, $default = null)
  332. {
  333. return $this->parameterHolder->get($name, $default);
  334. }
  335. /**
  336. * Indicates whether or not a parameter exist for the current request.
  337. *
  338. * @param string $name Parameter name
  339. *
  340. * @return bool true, if the paramater exists otherwise false
  341. */
  342. public function hasParameter($name)
  343. {
  344. return $this->parameterHolder->has($name);
  345. }
  346. /**
  347. * Sets a parameter for the current request.
  348. *
  349. * @param string $name Parameter name
  350. * @param string $value Parameter value
  351. *
  352. */
  353. public function setParameter($name, $value)
  354. {
  355. $this->parameterHolder->set($name, $value);
  356. }
  357. /**
  358. * Calls methods defined via sfEventDispatcher.
  359. *
  360. * @param string $method The method name
  361. * @param array $arguments The method arguments
  362. *
  363. * @return mixed The returned value of the called method
  364. *
  365. * @throws <b>sfException</b> if call fails
  366. */
  367. public function __call($method, $arguments)
  368. {
  369. $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'request.method_not_found', array('method' => $method, 'arguments' => $arguments)));
  370. if (!$event->isProcessed())
  371. {
  372. throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  373. }
  374. return $event->getReturnValue();
  375. }
  376. }