No Description

RequestContext.php 8.0KB

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