Skip to content

Commit aec348d

Browse files
committed
[HttpFoundation] Fix BC Break introduces in #61267 and structured suffix formats as constant
1 parent 92962bd commit aec348d

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

UPGRADE-7.4.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ HttpFoundation
6161
--------------
6262

6363
* Deprecate using `Request::sendHeaders()` after headers have already been sent; use a `StreamedResponse` instead
64+
* Add argument `$subtypeFallback` to `Request::getFormat()`
6465

6566
HttpKernel
6667
----------

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,28 @@ class Request
195195
self::HEADER_X_FORWARDED_PREFIX => 'X_FORWARDED_PREFIX',
196196
];
197197

198+
/**
199+
* This mapping is used when no exact MIME match is found in $formats.
200+
*
201+
* It enables mappings like application/soap+xml -> xml.
202+
*
203+
* @see https://datatracker.ietf.org/doc/html/rfc6839
204+
* @see https://datatracker.ietf.org/doc/html/rfc7303
205+
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
206+
*/
207+
private const STRUCTURED_SUFFIX_FORMATS = [
208+
'json' => 'json',
209+
'xml' => 'xml',
210+
'xhtml' => 'html',
211+
'cbor' => 'cbor',
212+
'zip' => 'zip',
213+
'ber' => 'asn1',
214+
'der' => 'asn1',
215+
'tlv' => 'tlv',
216+
'wbxml' => 'xml',
217+
'yaml' => 'yaml',
218+
];
219+
198220
private bool $isIisRewrite = false;
199221

200222
/**
@@ -1239,8 +1261,9 @@ public static function getMimeTypes(string $format): array
12391261
* @param string|null $mimeType The mime type to check
12401262
* @param bool $subtypeFallback Whether to fall back to the subtype if no exact match is found
12411263
*/
1242-
public function getFormat(?string $mimeType, bool $subtypeFallback = false): ?string
1264+
public function getFormat(?string $mimeType/* , bool $subtypeFallback = false */): ?string
12431265
{
1266+
$subtypeFallback = 2 <= \func_num_args() ? func_get_arg(1) : false;
12441267
$canonicalMimeType = null;
12451268
if ($mimeType && false !== $pos = strpos($mimeType, ';')) {
12461269
$canonicalMimeType = trim(substr($mimeType, 0, $pos));
@@ -1265,8 +1288,8 @@ public function getFormat(?string $mimeType, bool $subtypeFallback = false): ?st
12651288

12661289
if (str_starts_with($canonicalMimeType, 'application/') && str_contains($canonicalMimeType, '+')) {
12671290
$suffix = substr(strrchr($canonicalMimeType, '+'), 1);
1268-
if (isset(static::getStructuredSuffixFormats()[$suffix])) {
1269-
return static::getStructuredSuffixFormats()[$suffix];
1291+
if (isset(self::STRUCTURED_SUFFIX_FORMATS[$suffix])) {
1292+
return self::STRUCTURED_SUFFIX_FORMATS[$suffix];
12701293
}
12711294
}
12721295

@@ -1963,32 +1986,6 @@ protected static function initializeFormats(): void
19631986
];
19641987
}
19651988

1966-
/**
1967-
* This mapping is used when no exact MIME match is found in $formats.
1968-
* It enables handling of types like application/soap+xml → 'xml'.
1969-
*
1970-
* @see https://datatracker.ietf.org/doc/html/rfc6839
1971-
* @see https://datatracker.ietf.org/doc/html/rfc7303
1972-
* @see https://www.iana.org/assignments/media-types/media-types.xhtml
1973-
*
1974-
* @return array<string, string>
1975-
*/
1976-
private static function getStructuredSuffixFormats(): array
1977-
{
1978-
return [
1979-
'json' => 'json',
1980-
'xml' => 'xml',
1981-
'xhtml' => 'html',
1982-
'cbor' => 'cbor',
1983-
'zip' => 'zip',
1984-
'ber' => 'asn1',
1985-
'der' => 'asn1',
1986-
'tlv' => 'tlv',
1987-
'wbxml' => 'xml',
1988-
'yaml' => 'yaml',
1989-
];
1990-
}
1991-
19921989
private function setPhpDefaultLocale(string $locale): void
19931990
{
19941991
// if either the class Locale doesn't exist, or an exception is thrown when

0 commit comments

Comments
 (0)