RequestContext.php 7.2 KB

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