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