From 7220f2c88007131c51c79d37880ef9bb2d1736b0 Mon Sep 17 00:00:00 2001 From: Abdellatif Ait boudad Date: Wed, 20 May 2015 17:31:24 +0000 Subject: [PATCH] [Translator] avoid serialize unserializable resources. --- .../Translation/Tests/TranslatorCacheTest.php | 61 ------------------- .../Component/Translation/Translator.php | 7 +-- 2 files changed, 1 insertion(+), 67 deletions(-) diff --git a/src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php b/src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php index d5d4639984..abe364c736 100644 --- a/src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php +++ b/src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php @@ -74,41 +74,6 @@ class TranslatorCacheTest extends \PHPUnit_Framework_TestCase $this->assertEquals('OK', $translator->trans($msgid), '-> caching does not work in '.($debug ? 'debug' : 'production')); } - public function testRefreshCacheWhenResourcesChange() - { - // prime the cache - $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); - $loader - ->method('load') - ->will($this->returnValue($this->getCatalogue('fr', array( - 'foo' => 'foo A', - )))) - ; - - $translator = new Translator('fr', null, $this->tmpDir, true); - $translator->setLocale('fr'); - $translator->addLoader('loader', $loader); - $translator->addResource('loader', 'foo', 'fr'); - - $this->assertEquals('foo A', $translator->trans('foo')); - - // add a new resource to refresh the cache - $loader = $this->getMock('Symfony\Component\Translation\Loader\LoaderInterface'); - $loader - ->method('load') - ->will($this->returnValue($this->getCatalogue('fr', array( - 'foo' => 'foo B', - )))) - ; - - $translator = new Translator('fr', null, $this->tmpDir, true); - $translator->setLocale('fr'); - $translator->addLoader('loader', $loader); - $translator->addResource('loader', 'bar', 'fr'); - - $this->assertEquals('foo B', $translator->trans('foo')); - } - public function testCatalogueIsReloadedWhenResourcesAreNoLongerFresh() { /* @@ -150,32 +115,6 @@ class TranslatorCacheTest extends \PHPUnit_Framework_TestCase $translator->trans($msgid); } - /** - * @dataProvider runForDebugAndProduction - */ - public function testDifferentTranslatorsForSameLocaleDoNotInterfere($debug) - { - $locale = 'any_locale'; - $format = 'some_format'; - $msgid = 'test'; - - // Create a Translator and prime its cache - $translator = new Translator($locale, null, $this->tmpDir, $debug); - $translator->addLoader($format, new ArrayLoader()); - $translator->addResource($format, array($msgid => 'FAIL'), $locale); - $translator->trans($msgid); - - /* - * Create another Translator with the same locale but a different resource. - * It should not use the first translator's cache but return the value from its own resource. - */ - $translator = new Translator($locale, null, $this->tmpDir, $debug); - $translator->addLoader($format, new ArrayLoader()); - $translator->addResource($format, array($msgid => 'OK'), $locale); - - $this->assertEquals('OK', $translator->trans($msgid), '-> different translators for the same domain interfere in '.($debug ? 'debug' : 'production')); - } - /** * @dataProvider runForDebugAndProduction */ diff --git a/src/Symfony/Component/Translation/Translator.php b/src/Symfony/Component/Translation/Translator.php index ec315066f7..c9cf480adc 100644 --- a/src/Symfony/Component/Translation/Translator.php +++ b/src/Symfony/Component/Translation/Translator.php @@ -419,12 +419,7 @@ EOF private function getCatalogueCachePath($locale) { - $catalogueHash = sha1(serialize(array( - 'resources' => isset($this->resources[$locale]) ? $this->resources[$locale] : array(), - 'fallback_locales' => $this->fallbackLocales, - ))); - - return $this->cacheDir.'/catalogue.'.$locale.'.'.$catalogueHash.'.php'; + return $this->cacheDir.'/catalogue.'.$locale.'.'.sha1(serialize($this->fallbackLocales)).'.php'; } private function doLoadCatalogue($locale)