Skip to content

Commit 9608bf5

Browse files
committed
bug symfony#15110 Add a way to reset the singleton (dawehner)
This PR was submitted for the 2.8 branch but it was merged into the 2.3 branch instead (closes symfony#15110). Discussion ---------- Add a way to reset the singleton ## Problem The mime type guesser is a singleton, which as we know, causes all kind of problems. One problem Drupal ran into is the problem that for tests we continuesly registered more and more mimetype guessers, once everytime we rebuild the container as part of your testing. Once that we had that, with enough test functions + made one of the mimetype guessers as proxy, we ran into a memory leak of storing the container as part of the mime type singleton. See https://www.drupal.org/node/2408371 ## Solution Allow to reset the singleton. | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT Commits ------- 0096266 Add a way to reset the singleton
2 parents 8a8a929 + 0096266 commit 9608bf5

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

src/Symfony/Component/HttpFoundation/File/MimeType/MimeTypeGuesser.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ public static function getInstance()
6767
return self::$instance;
6868
}
6969

70+
/**
71+
* Resets the singleton instance.
72+
*/
73+
public static function reset() {
74+
self::$instance = null;
75+
}
76+
7077
/**
7178
* Registers all natively provided mime type guessers.
7279
*/

src/Symfony/Component/HttpFoundation/Tests/File/FileTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ public function testGuessExtensionIsBasedOnMimeType()
4545
$this->assertEquals('gif', $file->guessExtension());
4646
}
4747

48+
public function testGuessExtensionWithReset() {
49+
$file = new File(__DIR__.'/Fixtures/other-file.example');
50+
$guesser = $this->createMockGuesser($file->getPathname(), 'image/gif');
51+
MimeTypeGuesser::getInstance()->register($guesser);
52+
53+
$this->assertEquals('gif', $file->guessExtension());
54+
55+
MimeTypeGuesser::reset();
56+
57+
$this->assertNull($file->guessExtension());
58+
}
59+
4860
public function testConstructWhenFileNotExists()
4961
{
5062
$this->setExpectedException('Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException');

src/Symfony/Component/HttpFoundation/Tests/File/Fixtures/other-file.example

Whitespace-only changes.

0 commit comments

Comments
 (0)