You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
6.9 KiB

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