Skip to content
Closed
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 src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add `Request::resetFormats()` method to reset the list of supported format to MIME type mappings

7.2
---

Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -1731,6 +1731,14 @@ public function preferSafeContent(): bool
return $this->isSafeContentPreferred = AcceptHeader::fromString($this->headers->get('Prefer'))->has('safe');
}

/**
* Resets the mappings of formats to mime types.
*/
public static function resetFormats(): void
{
static::$formats = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static::$formats = null;
static::initializeFormats();

This is a micro-optimization, but this will save some time when handling the next request.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dunglas I considered doing it this way originally but my feeling was because the initial state of $formats is null, that's what we should set it back to as a reset., because that's the change that interferes least with the implementation details of the rest of the class. Happy to change it in accordance with your suggestion if you like.

}

/*
* The following methods are derived from code of the Zend Framework (1.10dev - 2010-01-24)
*
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ protected function tearDown(): void
Request::setTrustedHosts([]);
}

public function testResetFormats()
{
$request = new Request();
$request->setFormat('json', ['application/problem+json']);
$modifiedRequestFormat = $request->getFormat('application/json');
$this->assertNull($modifiedRequestFormat);
Request::resetFormats();
$this->assertEquals('json', $request->getFormat('application/json'));
}

public function testInitialize()
{
$request = new Request();
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add call to `Request::resetFormats()` in `Kernel::boot()` to reset the request formats at the start of a new main request.

7.2
---

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public function boot(): void
{
if (true === $this->booted) {
if (!$this->requestStackSize && $this->resetServices) {
Request::resetFormats();
if ($this->container->has('services_resetter')) {
$this->container->get('services_resetter')->reset();
}
Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,34 @@ public function testInitializeContainerClearsOldContainers()
$this->assertFileDoesNotExist($legacyContainerDir.'.legacy');
}

public function testBootResetsRequestFormatsOnReset()
{
$request = new Request();
$request->setFormat('json', ['application/problem+json']);
$modifiedRequestFormat = $request->getFormat('application/json');

$httpKernelMock = $this->getMockBuilder(HttpKernel::class)
->disableOriginalConstructor()
->getMock();

$httpKernelMock
->expects($this->any())
->method('handle')
->with($request);

$kernel = $this->getKernel(['getHttpKernel']);
$kernel->expects($this->any())
->method('getHttpKernel')
->willReturn($httpKernelMock);

$kernel->handle($request);

$kernel->boot();
$kernel->handle($request);
$this->assertNull($modifiedRequestFormat);
$this->assertSame('json', $request->getFormat('application/json'));
}

public function testBootInitializesBundlesAndContainer()
{
$kernel = $this->getKernel(['initializeBundles']);
Expand Down
Loading