Update dependencies

This commit is contained in:
Chris Hunt
2024-02-16 21:36:54 +00:00
parent 22d7a59e59
commit d52ae0d3c3
9569 changed files with 460443 additions and 282416 deletions

View File

@@ -98,6 +98,8 @@ class Response
'proxy_revalidate' => false,
'max_age' => true,
's_maxage' => true,
'stale_if_error' => true, // RFC5861
'stale_while_revalidate' => true, // RFC5861
'immutable' => false,
'last_modified' => true,
'etag' => true,
@@ -210,6 +212,13 @@ class Response
];
/**
* Tracks headers already sent in informational responses.
*/
private array $sentHeaders;
/**
* @param int $status The HTTP status code (200 "OK" by default)
*
* @throws \InvalidArgumentException When the HTTP status code is not valid
*/
public function __construct(?string $content = '', int $status = 200, array $headers = [])
@@ -220,25 +229,6 @@ class Response
$this->setProtocolVersion('1.0');
}
/**
* Factory method for chainability.
*
* Example:
*
* return Response::create($body, 200)
* ->setSharedMaxAge(300);
*
* @return static
*
* @deprecated since Symfony 5.1, use __construct() instead.
*/
public static function create(?string $content = '', int $status = 200, array $headers = [])
{
trigger_deprecation('symfony/http-foundation', '5.1', 'The "%s()" method is deprecated, use "new %s()" instead.', __METHOD__, static::class);
return new static($content, $status, $headers);
}
/**
* Returns the Response as an HTTP string.
*
@@ -246,11 +236,9 @@ class Response
* one that will be sent to the client only if the prepare() method
* has been called before.
*
* @return string
*
* @see prepare()
*/
public function __toString()
public function __toString(): string
{
return
sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\r\n".
@@ -275,7 +263,7 @@ class Response
*
* @return $this
*/
public function prepare(Request $request)
public function prepare(Request $request): static
{
$headers = $this->headers;
@@ -298,7 +286,7 @@ class Response
$charset = $this->charset ?: 'UTF-8';
if (!$headers->has('Content-Type')) {
$headers->set('Content-Type', 'text/html; charset='.$charset);
} elseif (0 === stripos($headers->get('Content-Type'), 'text/') && false === stripos($headers->get('Content-Type'), 'charset')) {
} elseif (0 === stripos($headers->get('Content-Type') ?? '', 'text/') && false === stripos($headers->get('Content-Type') ?? '', 'charset')) {
// add the charset
$headers->set('Content-Type', $headers->get('Content-Type').'; charset='.$charset);
}
@@ -343,21 +331,54 @@ class Response
/**
* Sends HTTP headers.
*
* @param positive-int|null $statusCode The status code to use, override the statusCode property if set and not null
*
* @return $this
*/
public function sendHeaders()
public function sendHeaders(/* int $statusCode = null */): static
{
// headers have already been sent by the developer
if (headers_sent()) {
return $this;
}
$statusCode = \func_num_args() > 0 ? func_get_arg(0) : null;
$informationalResponse = $statusCode >= 100 && $statusCode < 200;
if ($informationalResponse && !\function_exists('headers_send')) {
// skip informational responses if not supported by the SAPI
return $this;
}
// headers
foreach ($this->headers->allPreserveCaseWithoutCookies() as $name => $values) {
$replace = 0 === strcasecmp($name, 'Content-Type');
foreach ($values as $value) {
$newValues = $values;
$replace = false;
// As recommended by RFC 8297, PHP automatically copies headers from previous 103 responses, we need to deal with that if headers changed
if (103 === $statusCode) {
$previousValues = $this->sentHeaders[$name] ?? null;
if ($previousValues === $values) {
// Header already sent in a previous response, it will be automatically copied in this response by PHP
continue;
}
$replace = 0 === strcasecmp($name, 'Content-Type');
if (null !== $previousValues && array_diff($previousValues, $values)) {
header_remove($name);
$previousValues = null;
}
$newValues = null === $previousValues ? $values : array_diff($values, $previousValues);
}
foreach ($newValues as $value) {
header($name.': '.$value, $replace, $this->statusCode);
}
if ($informationalResponse) {
$this->sentHeaders[$name] = $values;
}
}
// cookies
@@ -365,8 +386,16 @@ class Response
header('Set-Cookie: '.$cookie, false, $this->statusCode);
}
if ($informationalResponse) {
headers_send($statusCode);
return $this;
}
$statusCode ??= $this->statusCode;
// status
header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText), true, $this->statusCode);
header(sprintf('HTTP/%s %s %s', $this->version, $statusCode, $this->statusText), true, $statusCode);
return $this;
}
@@ -376,7 +405,7 @@ class Response
*
* @return $this
*/
public function sendContent()
public function sendContent(): static
{
echo $this->content;
@@ -386,18 +415,25 @@ class Response
/**
* Sends HTTP headers and content.
*
* @param bool $flush Whether output buffers should be flushed
*
* @return $this
*/
public function send()
public function send(/* bool $flush = true */): static
{
$this->sendHeaders();
$this->sendContent();
$flush = 1 <= \func_num_args() ? func_get_arg(0) : true;
if (!$flush) {
return $this;
}
if (\function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
} elseif (\function_exists('litespeed_finish_request')) {
litespeed_finish_request();
} elseif (!\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
} elseif (!\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
static::closeOutputBuffers(0, true);
flush();
}
@@ -410,7 +446,7 @@ class Response
*
* @return $this
*/
public function setContent(?string $content)
public function setContent(?string $content): static
{
$this->content = $content ?? '';
@@ -419,10 +455,8 @@ class Response
/**
* Gets the current response content.
*
* @return string|false
*/
public function getContent()
public function getContent(): string|false
{
return $this->content;
}
@@ -434,7 +468,7 @@ class Response
*
* @final
*/
public function setProtocolVersion(string $version): object
public function setProtocolVersion(string $version): static
{
$this->version = $version;
@@ -463,7 +497,7 @@ class Response
*
* @final
*/
public function setStatusCode(int $code, string $text = null): object
public function setStatusCode(int $code, ?string $text = null): static
{
$this->statusCode = $code;
if ($this->isInvalid()) {
@@ -476,12 +510,6 @@ class Response
return $this;
}
if (false === $text) {
$this->statusText = '';
return $this;
}
$this->statusText = $text;
return $this;
@@ -504,7 +532,7 @@ class Response
*
* @final
*/
public function setCharset(string $charset): object
public function setCharset(string $charset): static
{
$this->charset = $charset;
@@ -585,7 +613,7 @@ class Response
*
* @final
*/
public function setPrivate(): object
public function setPrivate(): static
{
$this->headers->removeCacheControlDirective('public');
$this->headers->addCacheControlDirective('private');
@@ -602,7 +630,7 @@ class Response
*
* @final
*/
public function setPublic(): object
public function setPublic(): static
{
$this->headers->addCacheControlDirective('public');
$this->headers->removeCacheControlDirective('private');
@@ -617,7 +645,7 @@ class Response
*
* @final
*/
public function setImmutable(bool $immutable = true): object
public function setImmutable(bool $immutable = true): static
{
if ($immutable) {
$this->headers->addCacheControlDirective('immutable');
@@ -660,7 +688,7 @@ class Response
*
* @final
*/
public function getDate(): ?\DateTimeInterface
public function getDate(): ?\DateTimeImmutable
{
return $this->headers->getDate('Date');
}
@@ -672,12 +700,9 @@ class Response
*
* @final
*/
public function setDate(\DateTimeInterface $date): object
public function setDate(\DateTimeInterface $date): static
{
if ($date instanceof \DateTime) {
$date = \DateTimeImmutable::createFromMutable($date);
}
$date = \DateTimeImmutable::createFromInterface($date);
$date = $date->setTimezone(new \DateTimeZone('UTC'));
$this->headers->set('Date', $date->format('D, d M Y H:i:s').' GMT');
@@ -703,7 +728,7 @@ class Response
*
* @return $this
*/
public function expire()
public function expire(): static
{
if ($this->isFresh()) {
$this->headers->set('Age', $this->getMaxAge());
@@ -718,13 +743,13 @@ class Response
*
* @final
*/
public function getExpires(): ?\DateTimeInterface
public function getExpires(): ?\DateTimeImmutable
{
try {
return $this->headers->getDate('Expires');
} catch (\RuntimeException $e) {
} catch (\RuntimeException) {
// according to RFC 2616 invalid date formats (e.g. "0" and "-1") must be treated as in the past
return \DateTime::createFromFormat('U', time() - 172800);
return \DateTimeImmutable::createFromFormat('U', time() - 172800);
}
}
@@ -737,18 +762,18 @@ class Response
*
* @final
*/
public function setExpires(\DateTimeInterface $date = null): object
public function setExpires(?\DateTimeInterface $date = null): static
{
if (1 > \func_num_args()) {
trigger_deprecation('symfony/http-foundation', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
}
if (null === $date) {
$this->headers->remove('Expires');
return $this;
}
if ($date instanceof \DateTime) {
$date = \DateTimeImmutable::createFromMutable($date);
}
$date = \DateTimeImmutable::createFromInterface($date);
$date = $date->setTimezone(new \DateTimeZone('UTC'));
$this->headers->set('Expires', $date->format('D, d M Y H:i:s').' GMT');
@@ -774,8 +799,10 @@ class Response
return (int) $this->headers->getCacheControlDirective('max-age');
}
if (null !== $this->getExpires()) {
return (int) $this->getExpires()->format('U') - (int) $this->getDate()->format('U');
if (null !== $expires = $this->getExpires()) {
$maxAge = (int) $expires->format('U') - (int) $this->getDate()->format('U');
return max($maxAge, 0);
}
return null;
@@ -790,13 +817,45 @@ class Response
*
* @final
*/
public function setMaxAge(int $value): object
public function setMaxAge(int $value): static
{
$this->headers->addCacheControlDirective('max-age', $value);
return $this;
}
/**
* Sets the number of seconds after which the response should no longer be returned by shared caches when backend is down.
*
* This method sets the Cache-Control stale-if-error directive.
*
* @return $this
*
* @final
*/
public function setStaleIfError(int $value): static
{
$this->headers->addCacheControlDirective('stale-if-error', $value);
return $this;
}
/**
* Sets the number of seconds after which the response should no longer return stale content by shared caches.
*
* This method sets the Cache-Control stale-while-revalidate directive.
*
* @return $this
*
* @final
*/
public function setStaleWhileRevalidate(int $value): static
{
$this->headers->addCacheControlDirective('stale-while-revalidate', $value);
return $this;
}
/**
* Sets the number of seconds after which the response should no longer be considered fresh by shared caches.
*
@@ -806,7 +865,7 @@ class Response
*
* @final
*/
public function setSharedMaxAge(int $value): object
public function setSharedMaxAge(int $value): static
{
$this->setPublic();
$this->headers->addCacheControlDirective('s-maxage', $value);
@@ -819,7 +878,7 @@ class Response
*
* It returns null when no freshness information is present in the response.
*
* When the responses TTL is <= 0, the response may not be served from cache without first
* When the response's TTL is 0, the response may not be served from cache without first
* revalidating with the origin.
*
* @final
@@ -828,7 +887,7 @@ class Response
{
$maxAge = $this->getMaxAge();
return null !== $maxAge ? $maxAge - $this->getAge() : null;
return null !== $maxAge ? max($maxAge - $this->getAge(), 0) : null;
}
/**
@@ -840,7 +899,7 @@ class Response
*
* @final
*/
public function setTtl(int $seconds): object
public function setTtl(int $seconds): static
{
$this->setSharedMaxAge($this->getAge() + $seconds);
@@ -856,7 +915,7 @@ class Response
*
* @final
*/
public function setClientTtl(int $seconds): object
public function setClientTtl(int $seconds): static
{
$this->setMaxAge($this->getAge() + $seconds);
@@ -870,7 +929,7 @@ class Response
*
* @final
*/
public function getLastModified(): ?\DateTimeInterface
public function getLastModified(): ?\DateTimeImmutable
{
return $this->headers->getDate('Last-Modified');
}
@@ -884,18 +943,18 @@ class Response
*
* @final
*/
public function setLastModified(\DateTimeInterface $date = null): object
public function setLastModified(?\DateTimeInterface $date = null): static
{
if (1 > \func_num_args()) {
trigger_deprecation('symfony/http-foundation', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
}
if (null === $date) {
$this->headers->remove('Last-Modified');
return $this;
}
if ($date instanceof \DateTime) {
$date = \DateTimeImmutable::createFromMutable($date);
}
$date = \DateTimeImmutable::createFromInterface($date);
$date = $date->setTimezone(new \DateTimeZone('UTC'));
$this->headers->set('Last-Modified', $date->format('D, d M Y H:i:s').' GMT');
@@ -922,8 +981,11 @@ class Response
*
* @final
*/
public function setEtag(string $etag = null, bool $weak = false): object
public function setEtag(?string $etag = null, bool $weak = false): static
{
if (1 > \func_num_args()) {
trigger_deprecation('symfony/http-foundation', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
}
if (null === $etag) {
$this->headers->remove('Etag');
} else {
@@ -948,7 +1010,7 @@ class Response
*
* @final
*/
public function setCache(array $options): object
public function setCache(array $options): static
{
if ($diff = array_diff(array_keys($options), array_keys(self::HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES))) {
throw new \InvalidArgumentException(sprintf('Response does not support the following options: "%s".', implode('", "', $diff)));
@@ -970,6 +1032,14 @@ class Response
$this->setSharedMaxAge($options['s_maxage']);
}
if (isset($options['stale_while_revalidate'])) {
$this->setStaleWhileRevalidate($options['stale_while_revalidate']);
}
if (isset($options['stale_if_error'])) {
$this->setStaleIfError($options['stale_if_error']);
}
foreach (self::HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES as $directive => $hasValue) {
if (!$hasValue && isset($options[$directive])) {
if ($options[$directive]) {
@@ -1011,7 +1081,7 @@ class Response
*
* @final
*/
public function setNotModified(): object
public function setNotModified(): static
{
$this->setStatusCode(304);
$this->setContent(null);
@@ -1056,14 +1126,13 @@ class Response
/**
* Sets the Vary header.
*
* @param string|array $headers
* @param bool $replace Whether to replace the actual value or not (true by default)
* @param bool $replace Whether to replace the actual value or not (true by default)
*
* @return $this
*
* @final
*/
public function setVary($headers, bool $replace = true): object
public function setVary(string|array $headers, bool $replace = true): static
{
$this->headers->set('Vary', $headers, $replace);
@@ -1215,7 +1284,7 @@ class Response
*
* @final
*/
public function isRedirect(string $location = null): bool
public function isRedirect(?string $location = null): bool
{
return \in_array($this->statusCode, [201, 301, 302, 303, 307, 308]) && (null === $location ?: $location == $this->headers->get('Location'));
}