/vendor/symfony/lib/request/sfRequest.class.php

https://github.com/mikesname/ehri-ica-atom · PHP · 359 lines · 157 code · 42 blank · 160 comment · 6 complexity · 78497a5ffd7af30fbf72845eb1c2c433 MD5 · raw file

  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 28641 2010-03-21 10:20:44Z fabien $
  20. */
  21. abstract class sfRequest implements ArrayAccess
  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. $content = null,
  31. $method = null,
  32. $options = array(),
  33. $parameterHolder = null,
  34. $attributeHolder = null;
  35. /**
  36. * Class constructor.
  37. *
  38. * @see initialize()
  39. */
  40. public function __construct(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array(), $options = array())
  41. {
  42. $this->initialize($dispatcher, $parameters, $attributes, $options);
  43. }
  44. /**
  45. * Initializes this sfRequest.
  46. *
  47. * Available options:
  48. *
  49. * * logging: Whether to enable logging or not (false by default)
  50. *
  51. * @param sfEventDispatcher $dispatcher An sfEventDispatcher instance
  52. * @param array $parameters An associative array of initialization parameters
  53. * @param array $attributes An associative array of initialization attributes
  54. * @param array $options An associative array of options
  55. *
  56. * @return bool true, if initialization completes successfully, otherwise false
  57. *
  58. * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfRequest
  59. */
  60. public function initialize(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array(), $options = array())
  61. {
  62. $this->dispatcher = $dispatcher;
  63. $this->options = $options;
  64. if (!isset($this->options['logging']))
  65. {
  66. $this->options['logging'] = false;
  67. }
  68. // initialize parameter and attribute holders
  69. $this->parameterHolder = new sfParameterHolder();
  70. $this->attributeHolder = new sfParameterHolder();
  71. $this->parameterHolder->add($parameters);
  72. $this->attributeHolder->add($attributes);
  73. }
  74. /**
  75. * Returns the options.
  76. *
  77. * @return array The options.
  78. */
  79. public function getOptions()
  80. {
  81. return $this->options;
  82. }
  83. /**
  84. * Extracts parameter values from the request.
  85. *
  86. * @param array $names An indexed array of parameter names to extract
  87. *
  88. * @return array An associative array of parameters and their values. If
  89. * a specified parameter doesn't exist an empty string will
  90. * be returned for its value
  91. */
  92. public function extractParameters($names)
  93. {
  94. $array = array();
  95. $parameters = $this->parameterHolder->getAll();
  96. foreach ($parameters as $key => $value)
  97. {
  98. if (in_array($key, $names))
  99. {
  100. $array[$key] = $value;
  101. }
  102. }
  103. return $array;
  104. }
  105. /**
  106. * Gets the request method.
  107. *
  108. * @return string The request method
  109. */
  110. public function getMethod()
  111. {
  112. return $this->method;
  113. }
  114. /**
  115. * Sets the request method.
  116. *
  117. * @param string $method The request method
  118. *
  119. * @throws <b>sfException</b> - If the specified request method is invalid
  120. */
  121. public function setMethod($method)
  122. {
  123. if (!in_array(strtoupper($method), array(self::GET, self::POST, self::PUT, self::DELETE, self::HEAD)))
  124. {
  125. throw new sfException(sprintf('Invalid request method: %s.', $method));
  126. }
  127. $this->method = strtoupper($method);
  128. }
  129. /**
  130. * Returns true if the request parameter exists (implements the ArrayAccess interface).
  131. *
  132. * @param string $name The name of the request parameter
  133. *
  134. * @return Boolean true if the request parameter exists, false otherwise
  135. */
  136. public function __isset($name)
  137. {
  138. return $this->parameterHolder->has($name);
  139. }
  140. public function offsetExists($offset)
  141. {
  142. $args = func_get_args();
  143. return call_user_func_array(array($this, '__isset'), $args);
  144. }
  145. /**
  146. * Returns the request parameter associated with the name (implements the ArrayAccess interface).
  147. *
  148. * @param string $name The offset of the value to get
  149. *
  150. * @return mixed The request parameter if exists, null otherwise
  151. */
  152. public function __get($name)
  153. {
  154. return $this->parameterHolder->get($name);
  155. }
  156. public function offsetGet($offset)
  157. {
  158. $args = func_get_args();
  159. return call_user_func_array(array($this, '__get'), $args);
  160. }
  161. /**
  162. * Sets the request parameter associated with the offset (implements the ArrayAccess interface).
  163. *
  164. * @param string $offset The parameter name
  165. * @param string $value The parameter value
  166. */
  167. public function __set($name, $value)
  168. {
  169. return $this->parameterHolder->set($name, $value);
  170. }
  171. public function offsetSet($offset, $value)
  172. {
  173. $args = func_get_args();
  174. return call_user_func_array(array($this, '__set'), $args);
  175. }
  176. /**
  177. * Removes a request parameter.
  178. *
  179. * @param string $offset The parameter name
  180. */
  181. public function __unset($name)
  182. {
  183. return $this->parameterHolder->remove($name);
  184. }
  185. public function offsetUnset($offset)
  186. {
  187. $args = func_get_args();
  188. return call_user_func_array(array($this, '__unset'), $args);
  189. }
  190. /**
  191. * Retrieves the parameters for the current request.
  192. *
  193. * @return sfParameterHolder The parameter holder
  194. */
  195. public function getParameterHolder()
  196. {
  197. return $this->parameterHolder;
  198. }
  199. /**
  200. * Retrieves the attributes holder.
  201. *
  202. * @return sfParameterHolder The attribute holder
  203. */
  204. public function getAttributeHolder()
  205. {
  206. return $this->attributeHolder;
  207. }
  208. /**
  209. * Retrieves an attribute from the current request.
  210. *
  211. * @param string $name Attribute name
  212. * @param string $default Default attribute value
  213. *
  214. * @return mixed An attribute value
  215. */
  216. public function getAttribute($name, $default = null)
  217. {
  218. return $this->attributeHolder->get($name, $default);
  219. }
  220. /**
  221. * Indicates whether or not an attribute exist for the current request.
  222. *
  223. * @param string $name Attribute name
  224. *
  225. * @return bool true, if the attribute exists otherwise false
  226. */
  227. public function hasAttribute($name)
  228. {
  229. return $this->attributeHolder->has($name);
  230. }
  231. /**
  232. * Sets an attribute for the request.
  233. *
  234. * @param string $name Attribute name
  235. * @param string $value Value for the attribute
  236. *
  237. */
  238. public function setAttribute($name, $value)
  239. {
  240. $this->attributeHolder->set($name, $value);
  241. }
  242. /**
  243. * Retrieves a parameter for the current request.
  244. *
  245. * @param string $name Parameter name
  246. * @param string $default Parameter default value
  247. *
  248. */
  249. public function getParameter($name, $default = null)
  250. {
  251. return $this->parameterHolder->get($name, $default);
  252. }
  253. /**
  254. * Indicates whether or not a parameter exist for the current request.
  255. *
  256. * @param string $name Parameter name
  257. *
  258. * @return bool true, if the parameter exists otherwise false
  259. */
  260. public function hasParameter($name)
  261. {
  262. return $this->parameterHolder->has($name);
  263. }
  264. /**
  265. * Sets a parameter for the current request.
  266. *
  267. * @param string $name Parameter name
  268. * @param string $value Parameter value
  269. *
  270. */
  271. public function setParameter($name, $value)
  272. {
  273. $this->parameterHolder->set($name, $value);
  274. }
  275. /**
  276. * Returns the content of the current request.
  277. *
  278. * @return string|Boolean The content or false if none is available
  279. */
  280. public function getContent()
  281. {
  282. if (null === $this->content)
  283. {
  284. if (0 === strlen(trim($this->content = file_get_contents('php://input'))))
  285. {
  286. $this->content = false;
  287. }
  288. }
  289. return $this->content;
  290. }
  291. /**
  292. * Calls methods defined via sfEventDispatcher.
  293. *
  294. * @param string $method The method name
  295. * @param array $arguments The method arguments
  296. *
  297. * @return mixed The returned value of the called method
  298. *
  299. * @throws <b>sfException</b> if call fails
  300. */
  301. public function __call($method, $arguments)
  302. {
  303. $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'request.method_not_found', array('method' => $method, 'arguments' => $arguments)));
  304. if (!$event->isProcessed())
  305. {
  306. $clone = clone $this;
  307. $clone->options[$method] = $arguments[0];
  308. return $clone;
  309. }
  310. return $event->getReturnValue();
  311. }
  312. public function __clone()
  313. {
  314. $this->parameterHolder = clone $this->parameterHolder;
  315. $this->attributeHolder = clone $this->attributeHolder;
  316. }
  317. }