RequestContext.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. /**
  12. * Holds information about the current request.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. *
  16. * @api
  17. */
  18. class RequestContext
  19. {
  20. private $baseUrl;
  21. private $method;
  22. private $host;
  23. private $scheme;
  24. private $httpPort;
  25. private $httpsPort;
  26. private $parameters;
  27. /**
  28. * Constructor.
  29. *
  30. * @param string $baseUrl The base URL
  31. * @param string $method The HTTP method
  32. * @param string $host The HTTP host name
  33. * @param string $scheme The HTTP scheme
  34. * @param integer $httpPort The HTTP port
  35. * @param integer $httpsPort The HTTPS port
  36. *
  37. * @api
  38. */
  39. public function __construct($baseUrl = '', $method = 'GET', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443)
  40. {
  41. $this->baseUrl = $baseUrl;
  42. $this->method = strtoupper($method);
  43. $this->host = $host;
  44. $this->scheme = strtolower($scheme);
  45. $this->httpPort = $httpPort;
  46. $this->httpsPort = $httpsPort;
  47. $this->parameters = array();
  48. }
  49. /**
  50. * Gets the base URL.
  51. *
  52. * @return string The base URL
  53. */
  54. public function getBaseUrl()
  55. {
  56. return $this->baseUrl;
  57. }
  58. /**
  59. * Sets the base URL.
  60. *
  61. * @param string $baseUrl The base URL
  62. *
  63. * @api
  64. */
  65. public function setBaseUrl($baseUrl)
  66. {
  67. $this->baseUrl = $baseUrl;
  68. }
  69. /**
  70. * Gets the HTTP method.
  71. *
  72. * The method is always an uppercased string.
  73. *
  74. * @return string The HTTP method
  75. */
  76. public function getMethod()
  77. {
  78. return $this->method;
  79. }
  80. /**
  81. * Sets the HTTP method.
  82. *
  83. * @param string $method The HTTP method
  84. *
  85. * @api
  86. */
  87. public function setMethod($method)
  88. {
  89. $this->method = strtoupper($method);
  90. }
  91. /**
  92. * Gets the HTTP host.
  93. *
  94. * @return string The HTTP host
  95. */
  96. public function getHost()
  97. {
  98. return $this->host;
  99. }
  100. /**
  101. * Sets the HTTP host.
  102. *
  103. * @param string $host The HTTP host
  104. *
  105. * @api
  106. */
  107. public function setHost($host)
  108. {
  109. $this->host = $host;
  110. }
  111. /**
  112. * Gets the HTTP scheme.
  113. *
  114. * @return string The HTTP scheme
  115. */
  116. public function getScheme()
  117. {
  118. return $this->scheme;
  119. }
  120. /**
  121. * Sets the HTTP scheme.
  122. *
  123. * @param string $scheme The HTTP scheme
  124. *
  125. * @api
  126. */
  127. public function setScheme($scheme)
  128. {
  129. $this->scheme = strtolower($scheme);
  130. }
  131. /**
  132. * Gets the HTTP port.
  133. *
  134. * @return string The HTTP port
  135. */
  136. public function getHttpPort()
  137. {
  138. return $this->httpPort;
  139. }
  140. /**
  141. * Sets the HTTP port.
  142. *
  143. * @param string $httpPort The HTTP port
  144. *
  145. * @api
  146. */
  147. public function setHttpPort($httpPort)
  148. {
  149. $this->httpPort = $httpPort;
  150. }
  151. /**
  152. * Gets the HTTPS port.
  153. *
  154. * @return string The HTTPS port
  155. */
  156. public function getHttpsPort()
  157. {
  158. return $this->httpsPort;
  159. }
  160. /**
  161. * Sets the HTTPS port.
  162. *
  163. * @param string $httpsPort The HTTPS port
  164. *
  165. * @api
  166. */
  167. public function setHttpsPort($httpsPort)
  168. {
  169. $this->httpsPort = $httpsPort;
  170. }
  171. /**
  172. * Returns the parameters.
  173. *
  174. * @return array The parameters
  175. */
  176. public function getParameters()
  177. {
  178. return $this->parameters;
  179. }
  180. /**
  181. * Sets the parameters.
  182. *
  183. * This method implements a fluent interface.
  184. *
  185. * @param array $parameters The parameters
  186. *
  187. * @return Route The current Route instance
  188. */
  189. public function setParameters(array $parameters)
  190. {
  191. $this->parameters = $parameters;
  192. return $this;
  193. }
  194. /**
  195. * Gets a parameter value.
  196. *
  197. * @param string $name A parameter name
  198. *
  199. * @return mixed The parameter value
  200. */
  201. public function getParameter($name)
  202. {
  203. return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
  204. }
  205. /**
  206. * Checks if a parameter value is set for the given parameter.
  207. *
  208. * @param string $name A parameter name
  209. *
  210. * @return Boolean true if the parameter value is set, false otherwise
  211. */
  212. public function hasParameter($name)
  213. {
  214. return array_key_exists($name, $this->parameters);
  215. }
  216. /**
  217. * Sets a parameter value.
  218. *
  219. * @param string $name A parameter name
  220. * @param mixed $parameter The parameter value
  221. *
  222. * @api
  223. */
  224. public function setParameter($name, $parameter)
  225. {
  226. $this->parameters[$name] = $parameter;
  227. }
  228. }