Skip to content

[Translator] avoid serialize unserializable resources. #14705

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
Jun 5, 2015
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
61 changes: 0 additions & 61 deletions src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,41 +74,6 @@ public function testThatACacheIsUsed($debug)
$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()
{
/*
Expand Down Expand Up @@ -150,32 +115,6 @@ public function testCatalogueIsReloadedWhenResourcesAreNoLongerFresh()
$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
*/
Expand Down
7 changes: 1 addition & 6 deletions src/Symfony/Component/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,12 +419,7 @@ private function getFallbackContent(MessageCatalogue $catalogue)

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)
Expand Down