Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpFoundation] Rename Request::getContentType to getContentTypeFormat #45034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions UPGRADE-6.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ FrameworkBundle
`Symfony\Component\Serializer\Normalizer\NormalizerInterface` or implement `NormalizerAwareInterface` instead
* Deprecate `AbstractController::renderForm()`, use `render()` instead

HttpFoundation
--------------

* Deprecate `Request::getContentType()`, use `Request::getContentTypeFormat()` instead

Mailer
--------

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

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

6.0
---
Expand Down
16 changes: 15 additions & 1 deletion src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1331,9 +1331,23 @@ public function setRequestFormat(?string $format)
}

/**
* Gets the format associated with the request.
* Gets the usual name of the format associated with the request's media type (provided in the Content-Type header).
*
* @deprecated since Symfony 6.2, use getContentTypeFormat instead
*/
public function getContentType(): ?string
{
trigger_deprecation('symfony/http-foundation', '6.2', 'The method "%s" is deprecated, use "getContentTypeFormat" instead.', __METHOD__);

return $this->getContentTypeFormat();
}

/**
* Gets the usual name of the format associated with the request's media type (provided in the Content-Type header).
*
* @see Request::$formats
*/
public function getContentTypeFormat(): ?string
{
return $this->getFormat($this->headers->get('CONTENT_TYPE', ''));
}
Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpFoundation\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
use Symfony\Component\HttpFoundation\Exception\JsonException;
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
Expand All @@ -23,6 +24,8 @@

class RequestTest extends TestCase
{
use ExpectDeprecationTrait;

protected function tearDown(): void
{
Request::setTrustedProxies([], -1);
Expand Down Expand Up @@ -78,14 +81,33 @@ public function testIsNoCache()
$this->assertFalse($isNoCache);
}

/**
* @group legacy
*/
public function testGetContentType()
{
$this->expectDeprecation('Since symfony/http-foundation 6.2: The method "Symfony\Component\HttpFoundation\Request::getContentType" is deprecated, use "getContentTypeFormat" instead.');
$request = new Request();

$contentType = $request->getContentType();

$this->assertNull($contentType);
}

public function testGetContentTypeFormat()
{
$request = new Request();
$this->assertNull($request->getContentTypeFormat());

$server = ['HTTP_CONTENT_TYPE' => 'application/json'];
$request = new Request([], [], [], [], [], $server);
$this->assertEquals('json', $request->getContentTypeFormat());

$server = ['HTTP_CONTENT_TYPE' => 'text/html'];
$request = new Request([], [], [], [], [], $server);
$this->assertEquals('html', $request->getContentTypeFormat());
}

public function testSetDefaultLocale()
{
$request = new Request();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function supports(Request $request): bool
{
return ($this->options['post_only'] ? $request->isMethod('POST') : true)
&& $this->httpUtils->checkRequestPath($request, $this->options['check_path'])
&& ($this->options['form_only'] ? 'form' === $request->getContentType() : true);
&& ($this->options['form_only'] ? 'form' === (method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType()) : true);
}

public function authenticate(Request $request): Passport
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ public function __construct(HttpUtils $httpUtils, UserProviderInterface $userPro

public function supports(Request $request): ?bool
{
if (!str_contains($request->getRequestFormat() ?? '', 'json') && !str_contains($request->getContentType() ?? '', 'json')) {
if (
!str_contains($request->getRequestFormat() ?? '', 'json')
&& !str_contains((method_exists(Request::class, 'getContentTypeFormat') ? $request->getContentTypeFormat() : $request->getContentType()) ?? '', 'json')
) {
return false;
}

Expand Down