Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 57b24fe

Browse filesBrowse files
committed
feature #45034 [HttpFoundation] Rename Request::getContentType to getContentTypeFormat (MarkPedron)
This PR was merged into the 6.2 branch. Discussion ---------- [HttpFoundation] Rename Request::getContentType to getContentTypeFormat | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | yes | Tickets | Fix #39750 | License | MIT | Doc PR | TODO Commits ------- f545ed4 Rename getContentType to getContentTypeFormat
2 parents 6920f1d + f545ed4 commit 57b24fe
Copy full SHA for 57b24fe

File tree

6 files changed

+48
-3
lines changed
Filter options

6 files changed

+48
-3
lines changed

‎UPGRADE-6.2.md

Copy file name to clipboardExpand all lines: UPGRADE-6.2.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ FrameworkBundle
99
`Symfony\Component\Serializer\Normalizer\NormalizerInterface` or implement `NormalizerAwareInterface` instead
1010
* Deprecate `AbstractController::renderForm()`, use `render()` instead
1111

12+
HttpFoundation
13+
--------------
14+
15+
* Deprecate `Request::getContentType()`, use `Request::getContentTypeFormat()` instead
16+
1217
Mailer
1318
--------
1419

‎src/Symfony/Component/HttpFoundation/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add stale while revalidate and stale if error cache header
88
* Allow dynamic session "ttl" when using a remote storage
9+
* Deprecate `Request::getContentType()`, use `Request::getContentTypeFormat()` instead
910

1011
6.0
1112
---

‎src/Symfony/Component/HttpFoundation/Request.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Request.php
+15-1Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1331,9 +1331,23 @@ public function setRequestFormat(?string $format)
13311331
}
13321332

13331333
/**
1334-
* Gets the format associated with the request.
1334+
* Gets the usual name of the format associated with the request's media type (provided in the Content-Type header).
1335+
*
1336+
* @deprecated since Symfony 6.2, use getContentTypeFormat instead
13351337
*/
13361338
public function getContentType(): ?string
1339+
{
1340+
trigger_deprecation('symfony/http-foundation', '6.2', 'The method "%s" is deprecated, use "getContentTypeFormat" instead.', __METHOD__);
1341+
1342+
return $this->getContentTypeFormat();
1343+
}
1344+
1345+
/**
1346+
* Gets the usual name of the format associated with the request's media type (provided in the Content-Type header).
1347+
*
1348+
* @see Request::$formats
1349+
*/
1350+
public function getContentTypeFormat(): ?string
13371351
{
13381352
return $this->getFormat($this->headers->get('CONTENT_TYPE', ''));
13391353
}

‎src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpFoundation\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
1617
use Symfony\Component\HttpFoundation\Exception\JsonException;
1718
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
@@ -23,6 +24,8 @@
2324

2425
class RequestTest extends TestCase
2526
{
27+
use ExpectDeprecationTrait;
28+
2629
protected function tearDown(): void
2730
{
2831
Request::setTrustedProxies([], -1);
@@ -78,14 +81,33 @@ public function testIsNoCache()
7881
$this->assertFalse($isNoCache);
7982
}
8083

84+
/**
85+
* @group legacy
86+
*/
8187
public function testGetContentType()
8288
{
89+
$this->expectDeprecation('Since symfony/http-foundation 6.2: The method "Symfony\Component\HttpFoundation\Request::getContentType" is deprecated, use "getContentTypeFormat" instead.');
8390
$request = new Request();
91+
8492
$contentType = $request->getContentType();
8593

8694
$this->assertNull($contentType);
8795
}
8896

97+
public function testGetContentTypeFormat()
98+
{
99+
$request = new Request();
100+
$this->assertNull($request->getContentTypeFormat());
101+
102+
$server = ['HTTP_CONTENT_TYPE' => 'application/json'];
103+
$request = new Request([], [], [], [], [], $server);
104+
$this->assertEquals('json', $request->getContentTypeFormat());
105+
106+
$server = ['HTTP_CONTENT_TYPE' => 'text/html'];
107+
$request = new Request([], [], [], [], [], $server);
108+
$this->assertEquals('html', $request->getContentTypeFormat());
109+
}
110+
89111
public function testSetDefaultLocale()
90112
{
91113
$request = new Request();

‎src/Symfony/Component/Security/Http/Authenticator/FormLoginAuthenticator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Http/Authenticator/FormLoginAuthenticator.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function supports(Request $request): bool
7474
{
7575
return ($this->options['post_only'] ? $request->isMethod('POST') : true)
7676
&& $this->httpUtils->checkRequestPath($request, $this->options['check_path'])
77-
&& ($this->options['form_only'] ? 'form' === $request->getContentType() : true);
77+
&& ($this->options['form_only'] ? 'form' === (method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType()) : true);
7878
}
7979

8080
public function authenticate(Request $request): Passport

‎src/Symfony/Component/Security/Http/Authenticator/JsonLoginAuthenticator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Http/Authenticator/JsonLoginAuthenticator.php
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ public function __construct(HttpUtils $httpUtils, UserProviderInterface $userPro
6363

6464
public function supports(Request $request): ?bool
6565
{
66-
if (!str_contains($request->getRequestFormat() ?? '', 'json') && !str_contains($request->getContentType() ?? '', 'json')) {
66+
if (
67+
!str_contains($request->getRequestFormat() ?? '', 'json')
68+
&& !str_contains((method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType()) ?? '', 'json')
69+
) {
6770
return false;
6871
}
6972

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.