vendor/symfony/routing/RequestContext.php line 55

Open in your IDE?
  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. use Symfony\Component\HttpFoundation\Request;
  12. /**
  13.  * Holds information about the current request.
  14.  *
  15.  * This class implements a fluent interface.
  16.  *
  17.  * @author Fabien Potencier <fabien@symfony.com>
  18.  * @author Tobias Schultze <http://tobion.de>
  19.  */
  20. class RequestContext {
  21.     private $baseUrl;
  22.     private $pathInfo;
  23.     private $method;
  24.     private $host;
  25.     private $scheme;
  26.     private $httpPort;
  27.     private $httpsPort;
  28.     private $queryString;
  29.     private $parameters = [];
  30.     public function __construct(string $baseUrl ''string $method 'GET'string $host 'localhost'string $scheme 'http'int $httpPort 80int $httpsPort 443string $path '/'string $queryString '') {
  31.         $this->setBaseUrl($baseUrl);
  32.         $this->setMethod($method);
  33.         $this->setHost($host);
  34.         $this->setScheme($scheme);
  35.         $this->setHttpPort($httpPort);
  36.         $this->setHttpsPort($httpsPort);
  37.         $this->setPathInfo($path);
  38.         $this->setQueryString($queryString);
  39.     }
  40.     /**
  41.      * Updates the RequestContext information based on a HttpFoundation Request.
  42.      *
  43.      * @return $this
  44.      */
  45.     public function fromRequest(Request $request) {
  46.         $this->setBaseUrl($request->getBaseUrl());
  47.         $this->setPathInfo($request->getPathInfo());
  48.         $this->setMethod($request->getMethod());
  49.         $this->setHost($request->getHost());
  50.         $this->setScheme($request->getScheme());
  51.         $this->setHttpPort($request->isSecure() ? $this->httpPort $request->getPort());
  52.         $this->setHttpsPort($request->isSecure() ? $request->getPort() : $this->httpsPort);
  53.         $this->setQueryString($request->server->get('QUERY_STRING'''));
  54.         return $this;
  55.     }
  56.     /**
  57.      * Gets the base URL.
  58.      *
  59.      * @return string The base URL
  60.      */
  61.     public function getBaseUrl() {
  62.         return $this->baseUrl;
  63.     }
  64.     /**
  65.      * Sets the base URL.
  66.      *
  67.      * @param string $baseUrl The base URL
  68.      *
  69.      * @return $this
  70.      */
  71.     public function setBaseUrl($baseUrl) {
  72.         $this->baseUrl $baseUrl;
  73.         return $this;
  74.     }
  75.     /**
  76.      * Gets the path info.
  77.      *
  78.      * @return string The path info
  79.      */
  80.     public function getPathInfo() {
  81.         return $this->pathInfo;
  82.     }
  83.     /**
  84.      * Sets the path info.
  85.      *
  86.      * @param string $pathInfo The path info
  87.      *
  88.      * @return $this
  89.      */
  90.     public function setPathInfo($pathInfo) {
  91.         $this->pathInfo $pathInfo;
  92.         return $this;
  93.     }
  94.     /**
  95.      * Gets the HTTP method.
  96.      *
  97.      * The method is always an uppercased string.
  98.      *
  99.      * @return string The HTTP method
  100.      */
  101.     public function getMethod() {
  102.         return $this->method;
  103.     }
  104.     /**
  105.      * Sets the HTTP method.
  106.      *
  107.      * @param string $method The HTTP method
  108.      *
  109.      * @return $this
  110.      */
  111.     public function setMethod($method) {
  112.         $this->method strtoupper($method);
  113.         return $this;
  114.     }
  115.     /**
  116.      * Gets the HTTP host.
  117.      *
  118.      * The host is always lowercased because it must be treated case-insensitive.
  119.      *
  120.      * @return string The HTTP host
  121.      */
  122.     public function getHost() {
  123.         return $this->host;
  124.     }
  125.     /**
  126.      * Sets the HTTP host.
  127.      *
  128.      * @param string $host The HTTP host
  129.      *
  130.      * @return $this
  131.      */
  132.     public function setHost($host) {
  133.         $this->host strtolower($host);
  134.         return $this;
  135.     }
  136.     /**
  137.      * Gets the HTTP scheme.
  138.      *
  139.      * @return string The HTTP scheme
  140.      */
  141.     public function getScheme() {
  142.         return $this->scheme;
  143.     }
  144.     /**
  145.      * Sets the HTTP scheme.
  146.      *
  147.      * @param string $scheme The HTTP scheme
  148.      *
  149.      * @return $this
  150.      */
  151.     public function setScheme($scheme) {
  152.         $this->scheme strtolower($scheme);
  153.         return $this;
  154.     }
  155.     /**
  156.      * Gets the HTTP port.
  157.      *
  158.      * @return int The HTTP port
  159.      */
  160.     public function getHttpPort() {
  161.         return $this->httpPort;
  162.     }
  163.     /**
  164.      * Sets the HTTP port.
  165.      *
  166.      * @param int $httpPort The HTTP port
  167.      *
  168.      * @return $this
  169.      */
  170.     public function setHttpPort($httpPort) {
  171.         $this->httpPort = (int) $httpPort;
  172.         return $this;
  173.     }
  174.     /**
  175.      * Gets the HTTPS port.
  176.      *
  177.      * @return int The HTTPS port
  178.      */
  179.     public function getHttpsPort() {
  180.         return $this->httpsPort;
  181.     }
  182.     /**
  183.      * Sets the HTTPS port.
  184.      *
  185.      * @param int $httpsPort The HTTPS port
  186.      *
  187.      * @return $this
  188.      */
  189.     public function setHttpsPort($httpsPort) {
  190.         $this->httpsPort = (int) $httpsPort;
  191.         return $this;
  192.     }
  193.     /**
  194.      * Gets the query string.
  195.      *
  196.      * @return string The query string without the "?"
  197.      */
  198.     public function getQueryString() {
  199.         return $this->queryString;
  200.     }
  201.     /**
  202.      * Sets the query string.
  203.      *
  204.      * @param string $queryString The query string (after "?")
  205.      *
  206.      * @return $this
  207.      */
  208.     public function setQueryString($queryString) {
  209.         // string cast to be fault-tolerant, accepting null
  210.         $this->queryString = (string) $queryString;
  211.         return $this;
  212.     }
  213.     /**
  214.      * Returns the parameters.
  215.      *
  216.      * @return array The parameters
  217.      */
  218.     public function getParameters() {
  219.         return $this->parameters;
  220.     }
  221.     /**
  222.      * Sets the parameters.
  223.      *
  224.      * @param array $parameters The parameters
  225.      *
  226.      * @return $this
  227.      */
  228.     public function setParameters(array $parameters) {
  229.         $this->parameters $parameters;
  230.         return $this;
  231.     }
  232.     /**
  233.      * Gets a parameter value.
  234.      *
  235.      * @param string $name A parameter name
  236.      *
  237.      * @return mixed The parameter value or null if nonexistent
  238.      */
  239.     public function getParameter($name) {
  240.         return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
  241.     }
  242.     /**
  243.      * Checks if a parameter value is set for the given parameter.
  244.      *
  245.      * @param string $name A parameter name
  246.      *
  247.      * @return bool True if the parameter value is set, false otherwise
  248.      */
  249.     public function hasParameter($name) {
  250.         return \array_key_exists($name$this->parameters);
  251.     }
  252.     /**
  253.      * Sets a parameter value.
  254.      *
  255.      * @param string $name      A parameter name
  256.      * @param mixed  $parameter The parameter value
  257.      *
  258.      * @return $this
  259.      */
  260.     public function setParameter($name$parameter) {
  261.         $this->parameters[$name] = $parameter;
  262.         return $this;
  263.     }
  264. }