Skip to content

Commit

Permalink
bug #14705 [Translator] avoid serialize unserializable resources. (ai…
Browse files Browse the repository at this point in the history
…tboudad)

This PR was merged into the 2.7 branch.

Discussion
----------

[Translator] avoid serialize unserializable resources.

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Fixed tickets  | #14689
| Tests pass?   | yes
| License       | MIT

Commits
-------

7220f2c [Translator] avoid serialize unserializable resources.
  • Loading branch information
fabpot committed Jun 5, 2015
2 parents d65adc0 + 7220f2c commit 34ad82e
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 67 deletions.
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

0 comments on commit 34ad82e

Please sign in to comment.