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