vendor/symfony/routing/RequestContext.php line 57

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