1. sfRequest.class.php
  2. /** * sfRequest provides methods for manipulating client request information such * as attributes, and parameters. It is also possible to manipulate the * request method originally sent by the user. * * @package symfony * @subpackage request * @author Fabien Potencier * @author Sean Kerr * @version SVN: $Id: sfRequest.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $ */
  3. abstract class sfRequest implements ArrayAccess
  4. {
  5. const GET = 'GET';
  6. const POST = 'POST';
  7. const PUT = 'PUT';
  8. const DELETE = 'DELETE';
  9. const HEAD = 'HEAD';
  10. protected
  11. $dispatcher = null,
  12. $content = null,
  13. $method = null,
  14. $options = array(),
  15. $parameterHolder = null,
  16. $attributeHolder = null;
  17. /**
  18. * Class constructor.
  19. *
  20. * @see initialize()
  21. */
  22. public function __construct(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array(), $options = array())
  23. {
  24. $this->initialize($dispatcher, $parameters, $attributes, $options);
  25. }
  26. /**
  27. * Initializes this sfRequest.
  28. *
  29. * Available options:
  30. *
  31. * * logging: Whether to enable logging or not (false by default)
  32. *
  33. * @param sfEventDispatcher $dispatcher An sfEventDispatcher instance
  34. * @param array $parameters An associative array of initialization parameters
  35. * @param array $attributes An associative array of initialization attributes
  36. * @param array $options An associative array of options
  37. *
  38. * @return bool true, if initialization completes successfully, otherwise false
  39. *
  40. * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfRequest
  41. */
  42. public function initialize(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array(), $options = array())
  43. {
  44. $this->dispatcher = $dispatcher;
  45. $this->options = $options;
  46. if (!isset($this->options['logging']))
  47. {
  48. $this->options['logging'] = false;
  49. }
  50. // initialize parameter and attribute holders
  51. $this->parameterHolder = new sfParameterHolder();
  52. $this->attributeHolder = new sfParameterHolder();
  53. $this->parameterHolder->add($parameters);
  54. $this->attributeHolder->add($attributes);
  55. }
  56. /**
  57. * Returns the options.
  58. *
  59. * @return array The options.
  60. */
  61. public function getOptions()
  62. {
  63. return $this->options;
  64. }
  65. /**
  66. * Extracts parameter values from the request.
  67. *
  68. * @param array $names An indexed array of parameter names to extract
  69. *
  70. * @return array An associative array of parameters and their values. If
  71. * a specified parameter doesn't exist an empty string will
  72. * be returned for its value
  73. */
  74. public function extractParameters($names)
  75. {
  76. $array = array();
  77. $parameters = $this->parameterHolder->getAll();
  78. foreach ($parameters as $key => $value)
  79. {
  80. if (in_array($key, $names))
  81. {
  82. $array[$key] = $value;
  83. }
  84. }
  85. return $array;
  86. }
  87. /**
  88. * Gets the request method.
  89. *
  90. * @return string The request method
  91. */
  92. public function getMethod()
  93. {
  94. return $this->method;
  95. }
  96. /**
  97. * Sets the request method.
  98. *
  99. * @param string $method The request method
  100. *
  101. * @throws <b>sfException</b> - If the specified request method is invalid
  102. */
  103. public function setMethod($method)
  104. {
  105. if (!in_array(strtoupper($method), array(self::GET, self::POST, self::PUT, self::DELETE, self::HEAD)))
  106. {
  107. throw new sfException(sprintf('Invalid request method: %s.', $method));
  108. }
  109. $this->method = strtoupper($method);
  110. }
  111. /**
  112. * Returns true if the request parameter exists (implements the ArrayAccess interface).
  113. *
  114. * @param string $name The name of the request parameter
  115. *
  116. * @return Boolean true if the request parameter exists, false otherwise
  117. */
  118. public function offsetExists($name)
  119. {
  120. return $this->hasParameter($name);
  121. }
  122. /**
  123. * Returns the request parameter associated with the name (implements the ArrayAccess interface).
  124. *
  125. * @param string $name The offset of the value to get
  126. *
  127. * @return mixed The request parameter if exists, null otherwise
  128. */
  129. public function offsetGet($name)
  130. {
  131. return $this->getParameter($name, false);
  132. }
  133. /**
  134. * Sets the request parameter associated with the offset (implements the ArrayAccess interface).
  135. *
  136. * @param string $offset The parameter name
  137. * @param string $value The parameter value
  138. */
  139. public function offsetSet($offset, $value)
  140. {
  141. $this->setParameter($offset, $value);
  142. }
  143. /**
  144. * Removes a request parameter.
  145. *
  146. * @param string $offset The parameter name
  147. */
  148. public function offsetUnset($offset)
  149. {
  150. $this->getParameterHolder()->remove($offset);
  151. }
  152. /**
  153. * Retrieves the parameters for the current request.
  154. *
  155. * @return sfParameterHolder The parameter holder
  156. */
  157. public function getParameterHolder()
  158. {
  159. return $this->parameterHolder;
  160. }
  161. /**
  162. * Retrieves the attributes holder.
  163. *
  164. * @return sfParameterHolder The attribute holder
  165. */
  166. public function getAttributeHolder()
  167. {
  168. return $this->attributeHolder;
  169. }
  170. /**
  171. * Retrieves an attribute from the current request.
  172. *
  173. * @param string $name Attribute name
  174. * @param string $default Default attribute value
  175. *
  176. * @return mixed An attribute value
  177. */
  178. public function getAttribute($name, $default = null)
  179. {
  180. return $this->attributeHolder->get($name, $default);
  181. }
  182. /**
  183. * Indicates whether or not an attribute exist for the current request.
  184. *
  185. * @param string $name Attribute name
  186. *
  187. * @return bool true, if the attribute exists otherwise false
  188. */
  189. public function hasAttribute($name)
  190. {
  191. return $this->attributeHolder->has($name);
  192. }
  193. /**
  194. * Sets an attribute for the request.
  195. *
  196. * @param string $name Attribute name
  197. * @param string $value Value for the attribute
  198. *
  199. */
  200. public function setAttribute($name, $value)
  201. {
  202. $this->attributeHolder->set($name, $value);
  203. }
  204. /**
  205. * Retrieves a paramater for the current request.
  206. *
  207. * @param string $name Parameter name
  208. * @param string $default Parameter default value
  209. *
  210. */
  211. public function getParameter($name, $default = null)
  212. {
  213. return $this->parameterHolder->get($name, $default);
  214. }
  215. /**
  216. * Indicates whether or not a parameter exist for the current request.
  217. *
  218. * @param string $name Parameter name
  219. *
  220. * @return bool true, if the paramater exists otherwise false
  221. */
  222. public function hasParameter($name)
  223. {
  224. return $this->parameterHolder->has($name);
  225. }
  226. /**
  227. * Sets a parameter for the current request.
  228. *
  229. * @param string $name Parameter name
  230. * @param string $value Parameter value
  231. *
  232. */
  233. public function setParameter($name, $value)
  234. {
  235. $this->parameterHolder->set($name, $value);
  236. }
  237. /**
  238. * Returns the content of the current request.
  239. *
  240. * @return string|Boolean The content or false if none is available
  241. */
  242. public function getContent()
  243. {
  244. if (null === $this->content)
  245. {
  246. if (0 === strlen(trim($this->content = file_get_contents('php://input'))))
  247. {
  248. $this->content = false;
  249. }
  250. }
  251. return $this->content;
  252. }
  253. /**
  254. * Calls methods defined via sfEventDispatcher.
  255. *
  256. * @param string $method The method name
  257. * @param array $arguments The method arguments
  258. *
  259. * @return mixed The returned value of the called method
  260. *
  261. * @throws <b>sfException</b> if call fails
  262. */
  263. public function __call($method, $arguments)
  264. {
  265. $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'request.method_not_found', array('method' => $method, 'arguments' => $arguments)));
  266. if (!$event->isProcessed())
  267. {
  268. throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
  269. }
  270. return $event->getReturnValue();
  271. }
  272. public function __clone()
  273. {
  274. $this->parameterHolder = clone $this->parameterHolder;
  275. $this->attributeHolder = clone $this->attributeHolder;
  276. }
  277. }

Debug toolbar