RequestContext.php 6.5 KB

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