sfRequest.class.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 12660 2008-11-05 11:32:23Z fabien $
  20. */
  21. abstract class sfRequest
  22. {
  23. const GET = 'GET';
  24. const POST = 'POST';
  25. const PUT = 'PUT';
  26. const DELETE = 'DELETE';
  27. const HEAD = 'HEAD';
  28. protected
  29. $dispatcher = null,
  30. $method = null,
  31. $options = array(),
  32. $parameterHolder = null,
  33. $attributeHolder = null;
  34. /**
  35. * Class constructor.
  36. *
  37. * @see initialize()
  38. */
  39. public function __construct(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array(), $options = array())
  40. {
  41. $this->initialize($dispatcher, $parameters, $attributes, $options);
  42. }
  43. /**
  44. * Initializes this sfRequest.
  45. *
  46. * Available options:
  47. *
  48. * * logging: Whether to enable logging or not (false by default)
  49. *
  50. * @param sfEventDispatcher $dispatcher An sfEventDispatcher instance
  51. * @param array $parameters An associative array of initialization parameters
  52. * @param array $attributes An associative array of initialization attributes
  53. * @param array $options An associative array of options
  54. *
  55. * @return bool true, if initialization completes successfully, otherwise false
  56. *
  57. * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfRequest
  58. */
  59. public function initialize(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array(), $options = array())
  60. {
  61. $this->dispatcher = $dispatcher;
  62. $this->options = $options;
  63. if (!isset($this->options['logging']))
  64. {
  65. $this->options['logging'] = false;
  66. }
  67. // initialize parameter and attribute holders
  68. $this->parameterHolder = new sfParameterHolder();
  69. $this->attributeHolder = new sfParameterHolder();
  70. $this->parameterHolder->add($parameters);
  71. $this->attributeHolder->add($attributes);
  72. }
  73. /**
  74. * Extracts parameter values from the request.
  75. *
  76. * @param array $names An indexed array of parameter names to extract
  77. *
  78. * @return array An associative array of parameters and their values. If
  79. * a specified parameter doesn't exist an empty string will
  80. * be returned for its value
  81. */
  82. public function extractParameters($names)
  83. {
  84. $array = array();
  85. $parameters = $this->parameterHolder->getAll();
  86. foreach ($parameters as $key => $value)
  87. {
  88. if (in_array($key, $names))
  89. {
  90. $array[$key] = $value;
  91. }
  92. }
  93. return $array;
  94. }
  95. /**
  96. * Gets the request method.
  97. *
  98. * @return string The request method
  99. */
  100. public function getMethod()
  101. {
  102. return $this->method;
  103. }
  104. /**
  105. * Sets the request method.
  106. *
  107. * @param string $method The request method
  108. *
  109. * @throws <b>sfException</b> - If the specified request method is invalid
  110. */
  111. public function setMethod($method)
  112. {
  113. if (!in_array(strtoupper($method), array(self::GET, self::POST, self::PUT, self::DELETE, self::HEAD)))
  114. {
  115. throw new sfException(sprintf('Invalid request method: %s.', $method));
  116. }
  117. $this->method = strtoupper($method);
  118. }
  119. /**
  120. * Retrieves the parameters for the current request.
  121. *
  122. * @return sfParameterHolder The parameter holder
  123. */
  124. public function getParameterHolder()
  125. {
  126. return $this->parameterHolder;
  127. }
  128. /**
  129. * Retrieves the attributes holder.
  130. *
  131. * @return sfParameterHolder The attribute holder
  132. */
  133. public function getAttributeHolder()
  134. {
  135. return $this->attributeHolder;
  136. }
  137. /**
  138. * Retrieves an attribute from the current request.
  139. *
  140. * @param string $name Attribute name
  141. * @param string $default Default attribute value
  142. *
  143. * @return mixed An attribute value
  144. */
  145. public function getAttribute($name, $default = null)
  146. {
  147. return $this->attributeHolder->get($name, $default);
  148. }
  149. /**
  150. * Indicates whether or not an attribute exist for the current request.
  151. *
  152. * @param string $name Attribute name
  153. *
  154. * @return bool true, if the attribute exists otherwise false
  155. */
  156. public function hasAttribute($name)
  157. {
  158. return $this->attributeHolder->has($name);
  159. }
  160. /**
  161. * Sets an attribute for the request.
  162. *
  163. * @param string $name Attribute name
  164. * @param string $value Value for the attribute
  165. *
  166. */
  167. public function setAttribute($name, $value)
  168. {
  169. $this->attributeHolder->set($name, $value);
  170. }
  171. /**
  172. * Retrieves a paramater for the current request.
  173. *
  174. * @param string $name Parameter name
  175. * @param string $default Parameter default value
  176. *
  177. */
  178. public function getParameter($name, $default = null)
  179. {
  180. return $this->parameterHolder->get($name, $default);
  181. }
  182. /**
  183. * Indicates whether or not a parameter exist for the current request.
  184. *
  185. * @param string $name Parameter name
  186. *
  187. * @return bool true, if the paramater exists otherwise false
  188. */
  189. public function hasParameter($name)
  190. {
  191. return $this->parameterHolder->has($name);
  192. }
  193. /**
  194. * Sets a parameter for the current request.
  195. *
  196. * @param string $name Parameter name
  197. * @param string $value Parameter value
  198. *
  199. */
  200. public function setParameter($name, $value)
  201. {
  202. $this->parameterHolder->set($name, $value);
  203. }
  204. /**
  205. * Calls methods defined via sfEventDispatcher.
  206. *
  207. * @param string $method The method name
  208. * @param array $arguments The method arguments
  209. *
  210. * @return mixed The returned value of the called method
  211. *
  212. * @throws <b>sfException</b> if call fails
  213. */
  214. public function __call($method, $arguments)
  215. {
  216. $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'request.method_not_found', array('method' => $method, 'arguments' => $arguments)));
  217. if (!$event->isProcessed())
  218. {
  219. throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  220. }
  221. return $event->getReturnValue();
  222. }
  223. public function __clone()
  224. {
  225. $this->parameterHolder = clone $this->parameterHolder;
  226. $this->attributeHolder = clone $this->attributeHolder;
  227. }
  228. }