upgrade to laravel 7 and set branch to v2

This commit is contained in:
2020-12-25 11:22:15 +00:00
parent 516105c492
commit 0ddd298350
4638 changed files with 132501 additions and 190226 deletions

View File

@@ -12,6 +12,7 @@
namespace Symfony\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
use Symfony\Component\HttpFoundation\Exception\JsonException;
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
@@ -20,6 +21,7 @@ class_exists(AcceptHeader::class);
class_exists(FileBag::class);
class_exists(HeaderBag::class);
class_exists(HeaderUtils::class);
class_exists(InputBag::class);
class_exists(ParameterBag::class);
class_exists(ServerBag::class);
@@ -38,13 +40,17 @@ class_exists(ServerBag::class);
*/
class Request
{
public const HEADER_FORWARDED = 0b00001; // When using RFC 7239
public const HEADER_X_FORWARDED_FOR = 0b00010;
public const HEADER_X_FORWARDED_HOST = 0b00100;
public const HEADER_X_FORWARDED_PROTO = 0b01000;
public const HEADER_X_FORWARDED_PORT = 0b10000;
public const HEADER_X_FORWARDED_ALL = 0b11110; // All "X-Forwarded-*" headers
public const HEADER_X_FORWARDED_AWS_ELB = 0b11010; // AWS ELB doesn't send X-Forwarded-Host
public const HEADER_FORWARDED = 0b000001; // When using RFC 7239
public const HEADER_X_FORWARDED_FOR = 0b000010;
public const HEADER_X_FORWARDED_HOST = 0b000100;
public const HEADER_X_FORWARDED_PROTO = 0b001000;
public const HEADER_X_FORWARDED_PORT = 0b010000;
public const HEADER_X_FORWARDED_PREFIX = 0b100000;
/** @deprecated since Symfony 5.2, use either "HEADER_X_FORWARDED_FOR | HEADER_X_FORWARDED_HOST | HEADER_X_FORWARDED_PORT | HEADER_X_FORWARDED_PROTO" or "HEADER_X_FORWARDED_AWS_ELB" or "HEADER_X_FORWARDED_TRAEFIK" constants instead. */
public const HEADER_X_FORWARDED_ALL = 0b1011110; // All "X-Forwarded-*" headers sent by "usual" reverse proxy
public const HEADER_X_FORWARDED_AWS_ELB = 0b0011010; // AWS ELB doesn't send X-Forwarded-Host
public const HEADER_X_FORWARDED_TRAEFIK = 0b0111110; // All "X-Forwarded-*" headers sent by Traefik reverse proxy
public const METHOD_HEAD = 'HEAD';
public const METHOD_GET = 'GET';
@@ -84,14 +90,14 @@ class Request
/**
* Request body parameters ($_POST).
*
* @var ParameterBag
* @var InputBag|ParameterBag
*/
public $request;
/**
* Query string parameters ($_GET).
*
* @var ParameterBag
* @var InputBag
*/
public $query;
@@ -112,7 +118,7 @@ class Request
/**
* Cookies ($_COOKIE).
*
* @var ParameterBag
* @var InputBag
*/
public $cookies;
@@ -207,6 +213,11 @@ class Request
private $isHostValid = true;
private $isForwardedValid = true;
/**
* @var bool|null
*/
private $isSafeContentPreferred;
private static $trustedHeaderSet = -1;
private static $forwardedParams = [
@@ -231,6 +242,7 @@ class Request
self::HEADER_X_FORWARDED_HOST => 'X_FORWARDED_HOST',
self::HEADER_X_FORWARDED_PROTO => 'X_FORWARDED_PROTO',
self::HEADER_X_FORWARDED_PORT => 'X_FORWARDED_PORT',
self::HEADER_X_FORWARDED_PREFIX => 'X_FORWARDED_PREFIX',
];
/**
@@ -263,9 +275,9 @@ class Request
public function initialize(array $query = [], array $request = [], array $attributes = [], array $cookies = [], array $files = [], array $server = [], $content = null)
{
$this->request = new ParameterBag($request);
$this->query = new ParameterBag($query);
$this->query = new InputBag($query);
$this->attributes = new ParameterBag($attributes);
$this->cookies = new ParameterBag($cookies);
$this->cookies = new InputBag($cookies);
$this->files = new FileBag($files);
$this->server = new ServerBag($server);
$this->headers = new HeaderBag($this->server->getHeaders());
@@ -292,11 +304,13 @@ class Request
{
$request = self::createRequestFromFactory($_GET, $_POST, [], $_COOKIE, $_FILES, $_SERVER);
if (0 === strpos($request->headers->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded')
if ($_POST) {
$request->request = new InputBag($_POST);
} elseif (0 === strpos($request->headers->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded')
&& \in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), ['PUT', 'DELETE', 'PATCH'])
) {
parse_str($request->getContent(), $data);
$request->request = new ParameterBag($data);
$request->request = new InputBag($data);
}
return $request;
@@ -318,7 +332,7 @@ class Request
*
* @return static
*/
public static function create($uri, $method = 'GET', $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
public static function create(string $uri, string $method = 'GET', array $parameters = [], array $cookies = [], array $files = [], array $server = [], $content = null)
{
$server = array_replace([
'SERVER_NAME' => 'localhost',
@@ -416,10 +430,8 @@ class Request
* This is mainly useful when you need to override the Request class
* to keep BC with an existing system. It should not be used for any
* other purpose.
*
* @param callable|null $callable A PHP callable
*/
public static function setFactory($callable)
public static function setFactory(?callable $callable)
{
self::$requestFactory = $callable;
}
@@ -440,7 +452,7 @@ class Request
{
$dup = clone $this;
if (null !== $query) {
$dup->query = new ParameterBag($query);
$dup->query = new InputBag($query);
}
if (null !== $request) {
$dup->request = new ParameterBag($request);
@@ -449,7 +461,7 @@ class Request
$dup->attributes = new ParameterBag($attributes);
}
if (null !== $cookies) {
$dup->cookies = new ParameterBag($cookies);
$dup->cookies = new InputBag($cookies);
}
if (null !== $files) {
$dup->files = new FileBag($files);
@@ -572,6 +584,9 @@ class Request
*/
public static function setTrustedProxies(array $proxies, int $trustedHeaderSet)
{
if (self::HEADER_X_FORWARDED_ALL === $trustedHeaderSet) {
trigger_deprecation('symfony/http-foundation', '5.2', 'The "HEADER_X_FORWARDED_ALL" constant is deprecated, use either "HEADER_X_FORWARDED_FOR | HEADER_X_FORWARDED_HOST | HEADER_X_FORWARDED_PORT | HEADER_X_FORWARDED_PROTO" or "HEADER_X_FORWARDED_AWS_ELB" or "HEADER_X_FORWARDED_TRAEFIK" constants instead.');
}
self::$trustedProxies = array_reduce($proxies, function ($proxies, $proxy) {
if ('REMOTE_ADDR' !== $proxy) {
$proxies[] = $proxy;
@@ -636,17 +651,15 @@ class Request
* It builds a normalized query string, where keys/value pairs are alphabetized,
* have consistent escaping and unneeded delimiters are removed.
*
* @param string $qs Query string
*
* @return string A normalized query string for the Request
*/
public static function normalizeQueryString($qs)
public static function normalizeQueryString(?string $qs)
{
if ('' === ($qs ?? '')) {
return '';
}
parse_str($qs, $qs);
$qs = HeaderUtils::parseQuery($qs);
ksort($qs);
return http_build_query($qs, '', '&', \PHP_QUERY_RFC3986);
@@ -687,23 +700,22 @@ class Request
*
* Order of precedence: PATH (routing placeholders or custom attributes), GET, BODY
*
* @param string $key The key
* @param mixed $default The default value if the parameter key does not exist
* @param mixed $default The default value if the parameter key does not exist
*
* @return mixed
*/
public function get($key, $default = null)
public function get(string $key, $default = null)
{
if ($this !== $result = $this->attributes->get($key, $this)) {
return $result;
}
if ($this !== $result = $this->query->get($key, $this)) {
return $result;
if ($this->query->has($key)) {
return $this->query->all()[$key];
}
if ($this !== $result = $this->request->get($key, $this)) {
return $result;
if ($this->request->has($key)) {
return $this->request->all()[$key];
}
return $default;
@@ -722,8 +734,7 @@ class Request
}
if (null === $session) {
@trigger_error(sprintf('Calling "%s()" when no session has been set is deprecated since Symfony 4.1 and will throw an exception in 5.0. Use "hasSession()" instead.', __METHOD__), \E_USER_DEPRECATED);
// throw new \BadMethodCallException('Session has not been set.');
throw new \BadMethodCallException('Session has not been set.');
}
return $session;
@@ -882,6 +893,24 @@ class Request
* @return string The raw URL (i.e. not urldecoded)
*/
public function getBaseUrl()
{
$trustedPrefix = '';
// the proxy prefix must be prepended to any prefix being needed at the webserver level
if ($this->isFromTrustedProxy() && $trustedPrefixValues = $this->getTrustedValues(self::HEADER_X_FORWARDED_PREFIX)) {
$trustedPrefix = rtrim($trustedPrefixValues[0], '/');
}
return $trustedPrefix.$this->getBaseUrlReal();
}
/**
* Returns the real base URL received by the webserver from which this request is executed.
* The URL does not include trusted reverse proxy prefix.
*
* @return string The raw URL (i.e. not urldecoded)
*/
private function getBaseUrlReal()
{
if (null === $this->baseUrl) {
$this->baseUrl = $this->prepareBaseUrl();
@@ -1039,7 +1068,7 @@ class Request
*
* @return string The normalized URI for the path
*/
public function getUriForPath($path)
public function getUriForPath(string $path)
{
return $this->getSchemeAndHttpHost().$this->getBaseUrl().$path;
}
@@ -1059,11 +1088,9 @@ class Request
* - "/a/b/c/other" -> "other"
* - "/a/x/y" -> "../../x/y"
*
* @param string $path The target path
*
* @return string The relative target path
*/
public function getRelativeUriForPath($path)
public function getRelativeUriForPath(string $path)
{
// be sure that we are dealing with an absolute path
if (!isset($path[0]) || '/' !== $path[0]) {
@@ -1201,10 +1228,8 @@ class Request
/**
* Sets the request method.
*
* @param string $method
*/
public function setMethod($method)
public function setMethod(string $method)
{
$this->method = null;
$this->server->set('REQUEST_METHOD', $method);
@@ -1275,11 +1300,9 @@ class Request
/**
* Gets the mime type associated with the format.
*
* @param string $format The format
*
* @return string|null The associated mime type (null if not found)
*/
public function getMimeType($format)
public function getMimeType(string $format)
{
if (null === static::$formats) {
static::initializeFormats();
@@ -1291,11 +1314,9 @@ class Request
/**
* Gets the mime types associated with the format.
*
* @param string $format The format
*
* @return array The associated mime types
*/
public static function getMimeTypes($format)
public static function getMimeTypes(string $format)
{
if (null === static::$formats) {
static::initializeFormats();
@@ -1307,11 +1328,9 @@ class Request
/**
* Gets the format associated with the mime type.
*
* @param string $mimeType The associated mime type
*
* @return string|null The format (null if not found)
*/
public function getFormat($mimeType)
public function getFormat(?string $mimeType)
{
$canonicalMimeType = null;
if (false !== $pos = strpos($mimeType, ';')) {
@@ -1337,10 +1356,9 @@ class Request
/**
* Associates a format with mime types.
*
* @param string $format The format
* @param string|array $mimeTypes The associated mime types (the preferred one must be the first as it will be used as the content type)
*/
public function setFormat($format, $mimeTypes)
public function setFormat(?string $format, $mimeTypes)
{
if (null === static::$formats) {
static::initializeFormats();
@@ -1360,11 +1378,9 @@ class Request
*
* @see getPreferredFormat
*
* @param string|null $default The default format
*
* @return string|null The request format
*/
public function getRequestFormat($default = 'html')
public function getRequestFormat(?string $default = 'html')
{
if (null === $this->format) {
$this->format = $this->attributes->get('_format');
@@ -1375,10 +1391,8 @@ class Request
/**
* Sets the request format.
*
* @param string $format The request format
*/
public function setRequestFormat($format)
public function setRequestFormat(?string $format)
{
$this->format = $format;
}
@@ -1395,10 +1409,8 @@ class Request
/**
* Sets the default locale.
*
* @param string $locale
*/
public function setDefaultLocale($locale)
public function setDefaultLocale(string $locale)
{
$this->defaultLocale = $locale;
@@ -1419,10 +1431,8 @@ class Request
/**
* Sets the locale.
*
* @param string $locale
*/
public function setLocale($locale)
public function setLocale(string $locale)
{
$this->setPhpDefaultLocale($this->locale = $locale);
}
@@ -1444,7 +1454,7 @@ class Request
*
* @return bool
*/
public function isMethod($method)
public function isMethod(string $method)
{
return $this->getMethod() === strtoupper($method);
}
@@ -1458,10 +1468,6 @@ class Request
*/
public function isMethodSafe()
{
if (\func_num_args() > 0) {
@trigger_error(sprintf('Passing arguments to "%s()" has been deprecated since Symfony 4.4; use "%s::isMethodCacheable()" to check if the method is cacheable instead.', __METHOD__, __CLASS__), \E_USER_DEPRECATED);
}
return \in_array($this->getMethod(), ['GET', 'HEAD', 'OPTIONS', 'TRACE']);
}
@@ -1518,7 +1524,7 @@ class Request
*
* @return string|resource The request body content or a resource to read the body stream
*/
public function getContent($asResource = false)
public function getContent(bool $asResource = false)
{
$currentContentIsResource = \is_resource($this->content);
@@ -1556,6 +1562,36 @@ class Request
return $this->content;
}
/**
* Gets the request body decoded as array, typically from a JSON payload.
*
* @throws JsonException When the body cannot be decoded to an array
*
* @return array
*/
public function toArray()
{
if ('' === $content = $this->getContent()) {
throw new JsonException('Response body is empty.');
}
try {
$content = json_decode($content, true, 512, \JSON_BIGINT_AS_STRING | (\PHP_VERSION_ID >= 70300 ? \JSON_THROW_ON_ERROR : 0));
} catch (\JsonException $e) {
throw new JsonException('Could not decode request body.', $e->getCode(), $e);
}
if (\PHP_VERSION_ID < 70300 && \JSON_ERROR_NONE !== json_last_error()) {
throw new JsonException('Could not decode request body: '.json_last_error_msg(), json_last_error());
}
if (!\is_array($content)) {
throw new JsonException(sprintf('JSON content was expected to decode to an array, "%s" returned.', get_debug_type($content)));
}
return $content;
}
/**
* Gets the Etags.
*
@@ -1576,7 +1612,7 @@ class Request
/**
* Gets the preferred format for the response by inspecting, in the following order:
* * the request format set using setRequestFormat
* * the request format set using setRequestFormat;
* * the values of the Accept HTTP header.
*
* Note that if you use this method, you should send the "Vary: Accept" header
@@ -1729,6 +1765,29 @@ class Request
return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
}
/**
* Checks whether the client browser prefers safe content or not according to RFC8674.
*
* @see https://tools.ietf.org/html/rfc8674
*/
public function preferSafeContent(): bool
{
if (null !== $this->isSafeContentPreferred) {
return $this->isSafeContentPreferred;
}
if (!$this->isSecure()) {
// see https://tools.ietf.org/html/rfc8674#section-3
$this->isSafeContentPreferred = false;
return $this->isSafeContentPreferred;
}
$this->isSafeContentPreferred = AcceptHeader::fromString($this->headers->get('Prefer'))->has('safe');
return $this->isSafeContentPreferred;
}
/*
* The following methods are derived from code of the Zend Framework (1.10dev - 2010-01-24)
*
@@ -1902,7 +1961,7 @@ class Request
$requestUri = '/'.$requestUri;
}
if (null === ($baseUrl = $this->getBaseUrl())) {
if (null === ($baseUrl = $this->getBaseUrlReal())) {
return $requestUri;
}
@@ -2006,7 +2065,7 @@ class Request
}
}
if ((self::$trustedHeaderSet & self::HEADER_FORWARDED) && $this->headers->has(self::$trustedHeaders[self::HEADER_FORWARDED])) {
if ((self::$trustedHeaderSet & self::HEADER_FORWARDED) && (isset(self::$forwardedParams[$type])) && $this->headers->has(self::$trustedHeaders[self::HEADER_FORWARDED])) {
$forwarded = $this->headers->get(self::$trustedHeaders[self::HEADER_FORWARDED]);
$parts = HeaderUtils::split($forwarded, ',;=');
$forwardedValues = [];