RequestContext.php 6.9 KB

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