RequestContext.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 = [];
  31. public function __construct(string $baseUrl = '', string $method = 'GET', string $host = 'localhost', string $scheme = 'http', int $httpPort = 80, int $httpsPort = 443, string $path = '/', string $queryString = '')
  32. {
  33. $this->setBaseUrl($baseUrl);
  34. $this->setMethod($method);
  35. $this->setHost($host);
  36. $this->setScheme($scheme);
  37. $this->setHttpPort($httpPort);
  38. $this->setHttpsPort($httpsPort);
  39. $this->setPathInfo($path);
  40. $this->setQueryString($queryString);
  41. }
  42. /**
  43. * Updates the RequestContext information based on a HttpFoundation Request.
  44. *
  45. * @return $this
  46. */
  47. public function fromRequest(Request $request)
  48. {
  49. $this->setBaseUrl($request->getBaseUrl());
  50. $this->setPathInfo($request->getPathInfo());
  51. $this->setMethod($request->getMethod());
  52. $this->setHost($request->getHost());
  53. $this->setScheme($request->getScheme());
  54. $this->setHttpPort($request->isSecure() || null === $request->getPort() ? $this->httpPort : $request->getPort());
  55. $this->setHttpsPort($request->isSecure() && null !== $request->getPort() ? $request->getPort() : $this->httpsPort);
  56. $this->setQueryString($request->server->get('QUERY_STRING', ''));
  57. return $this;
  58. }
  59. /**
  60. * Gets the base URL.
  61. *
  62. * @return string The base URL
  63. */
  64. public function getBaseUrl()
  65. {
  66. return $this->baseUrl;
  67. }
  68. /**
  69. * Sets the base URL.
  70. *
  71. * @return $this
  72. */
  73. public function setBaseUrl(string $baseUrl)
  74. {
  75. $this->baseUrl = $baseUrl;
  76. return $this;
  77. }
  78. /**
  79. * Gets the path info.
  80. *
  81. * @return string The path info
  82. */
  83. public function getPathInfo()
  84. {
  85. return $this->pathInfo;
  86. }
  87. /**
  88. * Sets the path info.
  89. *
  90. * @return $this
  91. */
  92. public function setPathInfo(string $pathInfo)
  93. {
  94. $this->pathInfo = $pathInfo;
  95. return $this;
  96. }
  97. /**
  98. * Gets the HTTP method.
  99. *
  100. * The method is always an uppercased string.
  101. *
  102. * @return string The HTTP method
  103. */
  104. public function getMethod()
  105. {
  106. return $this->method;
  107. }
  108. /**
  109. * Sets the HTTP method.
  110. *
  111. * @return $this
  112. */
  113. public function setMethod(string $method)
  114. {
  115. $this->method = strtoupper($method);
  116. return $this;
  117. }
  118. /**
  119. * Gets the HTTP host.
  120. *
  121. * The host is always lowercased because it must be treated case-insensitive.
  122. *
  123. * @return string The HTTP host
  124. */
  125. public function getHost()
  126. {
  127. return $this->host;
  128. }
  129. /**
  130. * Sets the HTTP host.
  131. *
  132. * @return $this
  133. */
  134. public function setHost(string $host)
  135. {
  136. $this->host = strtolower($host);
  137. return $this;
  138. }
  139. /**
  140. * Gets the HTTP scheme.
  141. *
  142. * @return string The HTTP scheme
  143. */
  144. public function getScheme()
  145. {
  146. return $this->scheme;
  147. }
  148. /**
  149. * Sets the HTTP scheme.
  150. *
  151. * @return $this
  152. */
  153. public function setScheme(string $scheme)
  154. {
  155. $this->scheme = strtolower($scheme);
  156. return $this;
  157. }
  158. /**
  159. * Gets the HTTP port.
  160. *
  161. * @return int The HTTP port
  162. */
  163. public function getHttpPort()
  164. {
  165. return $this->httpPort;
  166. }
  167. /**
  168. * Sets the HTTP port.
  169. *
  170. * @return $this
  171. */
  172. public function setHttpPort(int $httpPort)
  173. {
  174. $this->httpPort = $httpPort;
  175. return $this;
  176. }
  177. /**
  178. * Gets the HTTPS port.
  179. *
  180. * @return int The HTTPS port
  181. */
  182. public function getHttpsPort()
  183. {
  184. return $this->httpsPort;
  185. }
  186. /**
  187. * Sets the HTTPS port.
  188. *
  189. * @return $this
  190. */
  191. public function setHttpsPort(int $httpsPort)
  192. {
  193. $this->httpsPort = $httpsPort;
  194. return $this;
  195. }
  196. /**
  197. * Gets the query string.
  198. *
  199. * @return string The query string without the "?"
  200. */
  201. public function getQueryString()
  202. {
  203. return $this->queryString;
  204. }
  205. /**
  206. * Sets the query string.
  207. *
  208. * @return $this
  209. */
  210. public function setQueryString(?string $queryString)
  211. {
  212. // string cast to be fault-tolerant, accepting null
  213. $this->queryString = (string) $queryString;
  214. return $this;
  215. }
  216. /**
  217. * Returns the parameters.
  218. *
  219. * @return array The parameters
  220. */
  221. public function getParameters()
  222. {
  223. return $this->parameters;
  224. }
  225. /**
  226. * Sets the parameters.
  227. *
  228. * @param array $parameters The parameters
  229. *
  230. * @return $this
  231. */
  232. public function setParameters(array $parameters)
  233. {
  234. $this->parameters = $parameters;
  235. return $this;
  236. }
  237. /**
  238. * Gets a parameter value.
  239. *
  240. * @return mixed The parameter value or null if nonexistent
  241. */
  242. public function getParameter(string $name)
  243. {
  244. return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
  245. }
  246. /**
  247. * Checks if a parameter value is set for the given parameter.
  248. *
  249. * @return bool True if the parameter value is set, false otherwise
  250. */
  251. public function hasParameter(string $name)
  252. {
  253. return \array_key_exists($name, $this->parameters);
  254. }
  255. /**
  256. * Sets a parameter value.
  257. *
  258. * @param mixed $parameter The parameter value
  259. *
  260. * @return $this
  261. */
  262. public function setParameter(string $name, $parameter)
  263. {
  264. $this->parameters[$name] = $parameter;
  265. return $this;
  266. }
  267. }