From aec348df2f5c12852867c63d3d9c8621df72ab86 Mon Sep 17 00:00:00 2001 From: Florent Morselli Date: Thu, 28 Aug 2025 11:32:50 +0200 Subject: [PATCH] [HttpFoundation] Fix BC Break introduces in `#61267` and structured suffix formats as constant --- UPGRADE-7.4.md | 1 + .../Component/HttpFoundation/Request.php | 55 +++++++++---------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/UPGRADE-7.4.md b/UPGRADE-7.4.md index 74b9cb0f458db..753592c7163d3 100644 --- a/UPGRADE-7.4.md +++ b/UPGRADE-7.4.md @@ -61,6 +61,7 @@ HttpFoundation -------------- * Deprecate using `Request::sendHeaders()` after headers have already been sent; use a `StreamedResponse` instead + * Add argument `$subtypeFallback` to `Request::getFormat()` HttpKernel ---------- diff --git a/src/Symfony/Component/HttpFoundation/Request.php b/src/Symfony/Component/HttpFoundation/Request.php index 42826b4e3934d..698385223e0b3 100644 --- a/src/Symfony/Component/HttpFoundation/Request.php +++ b/src/Symfony/Component/HttpFoundation/Request.php @@ -195,6 +195,28 @@ class Request self::HEADER_X_FORWARDED_PREFIX => 'X_FORWARDED_PREFIX', ]; + /** + * This mapping is used when no exact MIME match is found in $formats. + * + * It enables mappings like application/soap+xml -> xml. + * + * @see https://datatracker.ietf.org/doc/html/rfc6839 + * @see https://datatracker.ietf.org/doc/html/rfc7303 + * @see https://www.iana.org/assignments/media-types/media-types.xhtml + */ + private const STRUCTURED_SUFFIX_FORMATS = [ + 'json' => 'json', + 'xml' => 'xml', + 'xhtml' => 'html', + 'cbor' => 'cbor', + 'zip' => 'zip', + 'ber' => 'asn1', + 'der' => 'asn1', + 'tlv' => 'tlv', + 'wbxml' => 'xml', + 'yaml' => 'yaml', + ]; + private bool $isIisRewrite = false; /** @@ -1239,8 +1261,9 @@ public static function getMimeTypes(string $format): array * @param string|null $mimeType The mime type to check * @param bool $subtypeFallback Whether to fall back to the subtype if no exact match is found */ - public function getFormat(?string $mimeType, bool $subtypeFallback = false): ?string + public function getFormat(?string $mimeType/* , bool $subtypeFallback = false */): ?string { + $subtypeFallback = 2 <= \func_num_args() ? func_get_arg(1) : false; $canonicalMimeType = null; if ($mimeType && false !== $pos = strpos($mimeType, ';')) { $canonicalMimeType = trim(substr($mimeType, 0, $pos)); @@ -1265,8 +1288,8 @@ public function getFormat(?string $mimeType, bool $subtypeFallback = false): ?st if (str_starts_with($canonicalMimeType, 'application/') && str_contains($canonicalMimeType, '+')) { $suffix = substr(strrchr($canonicalMimeType, '+'), 1); - if (isset(static::getStructuredSuffixFormats()[$suffix])) { - return static::getStructuredSuffixFormats()[$suffix]; + if (isset(self::STRUCTURED_SUFFIX_FORMATS[$suffix])) { + return self::STRUCTURED_SUFFIX_FORMATS[$suffix]; } } @@ -1963,32 +1986,6 @@ protected static function initializeFormats(): void ]; } - /** - * This mapping is used when no exact MIME match is found in $formats. - * It enables handling of types like application/soap+xml → 'xml'. - * - * @see https://datatracker.ietf.org/doc/html/rfc6839 - * @see https://datatracker.ietf.org/doc/html/rfc7303 - * @see https://www.iana.org/assignments/media-types/media-types.xhtml - * - * @return array - */ - private static function getStructuredSuffixFormats(): array - { - return [ - 'json' => 'json', - 'xml' => 'xml', - 'xhtml' => 'html', - 'cbor' => 'cbor', - 'zip' => 'zip', - 'ber' => 'asn1', - 'der' => 'asn1', - 'tlv' => 'tlv', - 'wbxml' => 'xml', - 'yaml' => 'yaml', - ]; - } - private function setPhpDefaultLocale(string $locale): void { // if either the class Locale doesn't exist, or an exception is thrown when