RequestContext.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing;
  11. use Symfony\Component\HttpFoundation\Request;
  12. /**
  13. * Holds information about the current request.
  14. *
  15. * This class implements a fluent interface.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. * @author Tobias Schultze <http://tobion.de>
  19. */
  20. class RequestContext
  21. {
  22. private $baseUrl;
  23. private $pathInfo;
  24. private $method;
  25. private $host;
  26. private $scheme;
  27. private $httpPort;
  28. private $httpsPort;
  29. private $queryString;
  30. private $parameters = array();
  31. /**
  32. * @param string $baseUrl The base URL
  33. * @param string $method The HTTP method
  34. * @param string $host The HTTP host name
  35. * @param string $scheme The HTTP scheme
  36. * @param int $httpPort The HTTP port
  37. * @param int $httpsPort The HTTPS port
  38. * @param string $path The path
  39. * @param string $queryString The query string
  40. */
  41. public function __construct($baseUrl = '', $method = 'GET', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443, $path = '/', $queryString = '')
  42. {
  43. $this->setBaseUrl($baseUrl);
  44. $this->setMethod($method);
  45. $this->setHost($host);
  46. $this->setScheme($scheme);
  47. $this->setHttpPort($httpPort);
  48. $this->setHttpsPort($httpsPort);
  49. $this->setPathInfo($path);
  50. $this->setQueryString($queryString);
  51. }
  52. /**
  53. * Updates the RequestContext information based on a HttpFoundation Request.
  54. *
  55. * @return $this
  56. */
  57. public function fromRequest(Request $request)
  58. {
  59. $this->setBaseUrl($request->getBaseUrl());
  60. $this->setPathInfo($request->getPathInfo());
  61. $this->setMethod($request->getMethod());
  62. $this->setHost($request->getHost());
  63. $this->setScheme($request->getScheme());
  64. $this->setHttpPort($request->isSecure() ? $this->httpPort : $request->getPort());
  65. $this->setHttpsPort($request->isSecure() ? $request->getPort() : $this->httpsPort);
  66. $this->setQueryString($request->server->get('QUERY_STRING', ''));
  67. return $this;
  68. }
  69. /**
  70. * Gets the base URL.
  71. *
  72. * @return string The base URL
  73. */
  74. public function getBaseUrl()
  75. {
  76. return $this->baseUrl;
  77. }
  78. /**
  79. * Sets the base URL.
  80. *
  81. * @param string $baseUrl The base URL
  82. *
  83. * @return $this
  84. */
  85. public function setBaseUrl($baseUrl)
  86. {
  87. $this->baseUrl = $baseUrl;
  88. return $this;
  89. }
  90. /**
  91. * Gets the path info.
  92. *
  93. * @return string The path info
  94. */
  95. public function getPathInfo()
  96. {
  97. return $this->pathInfo;
  98. }
  99. /**
  100. * Sets the path info.
  101. *
  102. * @param string $pathInfo The path info
  103. *
  104. * @return $this
  105. */
  106. public function setPathInfo($pathInfo)
  107. {
  108. $this->pathInfo = $pathInfo;
  109. return $this;
  110. }
  111. /**
  112. * Gets the HTTP method.
  113. *
  114. * The method is always an uppercased string.
  115. *
  116. * @return string The HTTP method
  117. */
  118. public function getMethod()
  119. {
  120. return $this->method;
  121. }
  122. /**
  123. * Sets the HTTP method.
  124. *
  125. * @param string $method The HTTP method
  126. *
  127. * @return $this
  128. */
  129. public function setMethod($method)
  130. {
  131. $this->method = strtoupper($method);
  132. return $this;
  133. }
  134. /**
  135. * Gets the HTTP host.
  136. *
  137. * The host is always lowercased because it must be treated case-insensitive.
  138. *
  139. * @return string The HTTP host
  140. */
  141. public function getHost()
  142. {
  143. return $this->host;
  144. }
  145. /**
  146. * Sets the HTTP host.
  147. *
  148. * @param string $host The HTTP host
  149. *
  150. * @return $this
  151. */
  152. public function setHost($host)
  153. {
  154. $this->host = strtolower($host);
  155. return $this;
  156. }
  157. /**
  158. * Gets the HTTP scheme.
  159. *
  160. * @return string The HTTP scheme
  161. */
  162. public function getScheme()
  163. {
  164. return $this->scheme;
  165. }
  166. /**
  167. * Sets the HTTP scheme.
  168. *
  169. * @param string $scheme The HTTP scheme
  170. *
  171. * @return $this
  172. */
  173. public function setScheme($scheme)
  174. {
  175. $this->scheme = strtolower($scheme);
  176. return $this;
  177. }
  178. /**
  179. * Gets the HTTP port.
  180. *
  181. * @return int The HTTP port
  182. */
  183. public function getHttpPort()
  184. {
  185. return $this->httpPort;
  186. }
  187. /**
  188. * Sets the HTTP port.
  189. *
  190. * @param int $httpPort The HTTP port
  191. *
  192. * @return $this
  193. */
  194. public function setHttpPort($httpPort)
  195. {
  196. $this->httpPort = (int) $httpPort;
  197. return $this;
  198. }
  199. /**
  200. * Gets the HTTPS port.
  201. *
  202. * @return int The HTTPS port
  203. */
  204. public function getHttpsPort()
  205. {
  206. return $this->httpsPort;
  207. }
  208. /**
  209. * Sets the HTTPS port.
  210. *
  211. * @param int $httpsPort The HTTPS port
  212. *
  213. * @return $this
  214. */
  215. public function setHttpsPort($httpsPort)
  216. {
  217. $this->httpsPort = (int) $httpsPort;
  218. return $this;
  219. }
  220. /**
  221. * Gets the query string.
  222. *
  223. * @return string The query string without the "?"
  224. */
  225. public function getQueryString()
  226. {
  227. return $this->queryString;
  228. }
  229. /**
  230. * Sets the query string.
  231. *
  232. * @param string $queryString The query string (after "?")
  233. *
  234. * @return $this
  235. */
  236. public function setQueryString($queryString)
  237. {
  238. // string cast to be fault-tolerant, accepting null
  239. $this->queryString = (string) $queryString;
  240. return $this;
  241. }
  242. /**
  243. * Returns the parameters.
  244. *
  245. * @return array The parameters
  246. */
  247. public function getParameters()
  248. {
  249. return $this->parameters;
  250. }
  251. /**
  252. * Sets the parameters.
  253. *
  254. * @param array $parameters The parameters
  255. *
  256. * @return $this
  257. */
  258. public function setParameters(array $parameters)
  259. {
  260. $this->parameters = $parameters;
  261. return $this;
  262. }
  263. /**
  264. * Gets a parameter value.
  265. *
  266. * @param string $name A parameter name
  267. *
  268. * @return mixed The parameter value or null if nonexistent
  269. */
  270. public function getParameter($name)
  271. {
  272. return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
  273. }
  274. /**
  275. * Checks if a parameter value is set for the given parameter.
  276. *
  277. * @param string $name A parameter name
  278. *
  279. * @return bool True if the parameter value is set, false otherwise
  280. */
  281. public function hasParameter($name)
  282. {
  283. return array_key_exists($name, $this->parameters);
  284. }
  285. /**
  286. * Sets a parameter value.
  287. *
  288. * @param string $name A parameter name
  289. * @param mixed $parameter The parameter value
  290. *
  291. * @return $this
  292. */
  293. public function setParameter($name, $parameter)
  294. {
  295. $this->parameters[$name] = $parameter;
  296. return $this;
  297. }
  298. }