RequestContext.php 4.7 KB

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