Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Routing; Chris@0: Chris@0: use Symfony\Component\HttpFoundation\Request; Chris@0: Chris@0: /** Chris@0: * Holds information about the current request. Chris@0: * Chris@0: * This class implements a fluent interface. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: * @author Tobias Schultze Chris@0: */ Chris@0: class RequestContext Chris@0: { Chris@0: private $baseUrl; Chris@0: private $pathInfo; Chris@0: private $method; Chris@0: private $host; Chris@0: private $scheme; Chris@0: private $httpPort; Chris@0: private $httpsPort; Chris@0: private $queryString; Chris@17: private $parameters = []; Chris@0: Chris@0: /** Chris@0: * @param string $baseUrl The base URL Chris@0: * @param string $method The HTTP method Chris@0: * @param string $host The HTTP host name Chris@0: * @param string $scheme The HTTP scheme Chris@0: * @param int $httpPort The HTTP port Chris@0: * @param int $httpsPort The HTTPS port Chris@0: * @param string $path The path Chris@0: * @param string $queryString The query string Chris@0: */ Chris@0: public function __construct($baseUrl = '', $method = 'GET', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443, $path = '/', $queryString = '') Chris@0: { Chris@0: $this->setBaseUrl($baseUrl); Chris@0: $this->setMethod($method); Chris@0: $this->setHost($host); Chris@0: $this->setScheme($scheme); Chris@0: $this->setHttpPort($httpPort); Chris@0: $this->setHttpsPort($httpsPort); Chris@0: $this->setPathInfo($path); Chris@0: $this->setQueryString($queryString); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Updates the RequestContext information based on a HttpFoundation Request. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function fromRequest(Request $request) Chris@0: { Chris@0: $this->setBaseUrl($request->getBaseUrl()); Chris@0: $this->setPathInfo($request->getPathInfo()); Chris@0: $this->setMethod($request->getMethod()); Chris@0: $this->setHost($request->getHost()); Chris@0: $this->setScheme($request->getScheme()); Chris@0: $this->setHttpPort($request->isSecure() ? $this->httpPort : $request->getPort()); Chris@0: $this->setHttpsPort($request->isSecure() ? $request->getPort() : $this->httpsPort); Chris@0: $this->setQueryString($request->server->get('QUERY_STRING', '')); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the base URL. Chris@0: * Chris@0: * @return string The base URL Chris@0: */ Chris@0: public function getBaseUrl() Chris@0: { Chris@0: return $this->baseUrl; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the base URL. Chris@0: * Chris@0: * @param string $baseUrl The base URL Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setBaseUrl($baseUrl) Chris@0: { Chris@0: $this->baseUrl = $baseUrl; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the path info. Chris@0: * Chris@0: * @return string The path info Chris@0: */ Chris@0: public function getPathInfo() Chris@0: { Chris@0: return $this->pathInfo; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the path info. Chris@0: * Chris@0: * @param string $pathInfo The path info Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setPathInfo($pathInfo) Chris@0: { Chris@0: $this->pathInfo = $pathInfo; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the HTTP method. Chris@0: * Chris@0: * The method is always an uppercased string. Chris@0: * Chris@0: * @return string The HTTP method Chris@0: */ Chris@0: public function getMethod() Chris@0: { Chris@0: return $this->method; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the HTTP method. Chris@0: * Chris@0: * @param string $method The HTTP method Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setMethod($method) Chris@0: { Chris@0: $this->method = strtoupper($method); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the HTTP host. Chris@0: * Chris@0: * The host is always lowercased because it must be treated case-insensitive. Chris@0: * Chris@0: * @return string The HTTP host Chris@0: */ Chris@0: public function getHost() Chris@0: { Chris@0: return $this->host; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the HTTP host. Chris@0: * Chris@0: * @param string $host The HTTP host Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setHost($host) Chris@0: { Chris@0: $this->host = strtolower($host); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the HTTP scheme. Chris@0: * Chris@0: * @return string The HTTP scheme Chris@0: */ Chris@0: public function getScheme() Chris@0: { Chris@0: return $this->scheme; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the HTTP scheme. Chris@0: * Chris@0: * @param string $scheme The HTTP scheme Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setScheme($scheme) Chris@0: { Chris@0: $this->scheme = strtolower($scheme); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the HTTP port. Chris@0: * Chris@0: * @return int The HTTP port Chris@0: */ Chris@0: public function getHttpPort() Chris@0: { Chris@0: return $this->httpPort; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the HTTP port. Chris@0: * Chris@0: * @param int $httpPort The HTTP port Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setHttpPort($httpPort) Chris@0: { Chris@0: $this->httpPort = (int) $httpPort; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the HTTPS port. Chris@0: * Chris@0: * @return int The HTTPS port Chris@0: */ Chris@0: public function getHttpsPort() Chris@0: { Chris@0: return $this->httpsPort; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the HTTPS port. Chris@0: * Chris@0: * @param int $httpsPort The HTTPS port Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setHttpsPort($httpsPort) Chris@0: { Chris@0: $this->httpsPort = (int) $httpsPort; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the query string. Chris@0: * Chris@0: * @return string The query string without the "?" Chris@0: */ Chris@0: public function getQueryString() Chris@0: { Chris@0: return $this->queryString; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the query string. Chris@0: * Chris@0: * @param string $queryString The query string (after "?") Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setQueryString($queryString) Chris@0: { Chris@0: // string cast to be fault-tolerant, accepting null Chris@0: $this->queryString = (string) $queryString; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the parameters. Chris@0: * Chris@0: * @return array The parameters Chris@0: */ Chris@0: public function getParameters() Chris@0: { Chris@0: return $this->parameters; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the parameters. Chris@0: * Chris@0: * @param array $parameters The parameters Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setParameters(array $parameters) Chris@0: { Chris@0: $this->parameters = $parameters; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets a parameter value. Chris@0: * Chris@0: * @param string $name A parameter name Chris@0: * Chris@0: * @return mixed The parameter value or null if nonexistent Chris@0: */ Chris@0: public function getParameter($name) Chris@0: { Chris@0: return isset($this->parameters[$name]) ? $this->parameters[$name] : null; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks if a parameter value is set for the given parameter. Chris@0: * Chris@0: * @param string $name A parameter name Chris@0: * Chris@0: * @return bool True if the parameter value is set, false otherwise Chris@0: */ Chris@0: public function hasParameter($name) Chris@0: { Chris@18: return \array_key_exists($name, $this->parameters); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets a parameter value. Chris@0: * Chris@0: * @param string $name A parameter name Chris@0: * @param mixed $parameter The parameter value Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setParameter($name, $parameter) Chris@0: { Chris@0: $this->parameters[$name] = $parameter; Chris@0: Chris@0: return $this; Chris@0: } Chris@0: }