RequestContext.php 6.8 KB

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