8000 [Translation] added message cache + doctrine cache. · symfony/symfony@4794d8b · GitHub
[go: up one dir, main page]

Skip to content

Commit 4794d8b

Browse files
committed
[Translation] added message cache + doctrine cache.
1 parent 17ee6a6 commit 4794d8b

17 files changed

+758
-154
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Translation;
13+
14+
use Symfony\Bridge\Doctrine\Translation\DoctrineMessageCatalogue;
15+
use Doctrine\Common\Cache\ArrayCache;
16+
use Symfony\Component\Translation\Tests\MessageCatalogueTest;
17+
18+
class DoctrineMessageCatalogueTest extends MessageCatalogueTest
19+
{
20+
protected function setUp()
21+
{
22+
if (!interface_exists('Doctrine\Common\Cache\Cache')) {
23+
$this->markTestSkipped('The "Doctrine Cache" is not available');
24+
}
25+
}
26+
27+
public function testAll()
28+
{
29+
if (!interface_exists('Doctrine\Common\Cache\MultiGetCache')) {
30+
$this->markTestSkipped('The "Doctrine MultiGetCache" is not available');
31+
}
32+
33+
parent::testAll();
34+
}
35+
36+
protected function getCatalogue($locale, $messages = array())
37+
{
38+
$catalogue = new DoctrineMessageCatalogue($locale, new ArrayCache());
39+
foreach ($messages as $domain => $domainMessages) {
40+
$catalogue->add($domainMessages, $domain);
41+
}
42+
43+
return $catalogue;
44+
}
45+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\Translation;
13+
14+
use Symfony\Component\Translation\Translator;
15+
use Symfony\Component\Translation\MessageCatalogue;
16+
use Symfony\Component\Translation\MessageSelector;
17+
use Symfony\Bridge\Doctrine\Translation\DoctrineMessageCache;
18+
use Doctrine\Common\Cache\ArrayCache;
19+
use Symfony\Component\Translation\Loader\PhpFileLoader;
20+
use Symfony\Component\Translation\Dumper\PhpFileDumper;
21+
use Symfony\Component\Translation\Tests\TranslatorCacheTest;
22+
23+
class TranslatorDoctrineCacheTest extends TranslatorCacheTest
24+
{
25+
private $cache;
26+
27+
protected function setUp()
28+
{
29+
if (!interface_exists('Doctrine\Common\Cache\Cache')) {
30+
$this->markTestSkipped('The "Doctrine Cache" is not available');
31+
}
32+
33+
$this->cache = new ArrayCache();
34+
}
35+
36+
protected function getTranslator($locale, $debug)
37+
{
38+
$cache = new DoctrineMessageCache($this->cache, $debug);
39+
40+
return new Translator($locale, null, $cache);
41+
}
42+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Translation;
13+
14+
use Doctrine\Common\Cache\Cache;
15+
use Symfony\Component\Translation\MessageCacheInterface;
16+
use Symfony\Component\Translation\MessageCatalogueInterface;
17+
18+
/**
19+
* @author Abdellatif Ait Boudad <a.aitboudad@gmail.com>
20+
*/
21+
class DoctrineMessageCache implements MessageCacheInterface
22+
{
23+
const CACHE_CATALOGUE_HASH = 'catalogue_hash';
24+
const CACHE_DUMP_TIME = 'time';
25+
const CACHE_META_DATA = 'meta';
26+
const CATALOGUE_FALLBACK_LOCALE = 'fallback_locale';
27+
28+
/**
29+
* @var bool
30+
*/
31+
private $debug;
32+
33+
/**
34+
* @var Cache
35+
*/
36+
private $cache;
37+
38+
/**
39+
* @param Cache $cache
40+
* @param bool $debug
41+
*/
42+
public function __construct(Cache $cache, $debug = false)
43+
{
44+
$this->cache = $cache;
45+
$this->debug = $debug;
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function isFresh($locale, array $options = array())
52+
{
53+
$catalogueIdentifier = $this->getCatalogueIdentifier($locale, $options);
54+
if (!$this->cache->contains($this->getCatalogueHashKey($catalogueIdentifier))) {
55+
return false;
56+
}
57+
58+
if ($this->debug) {
59+
$time = $this->cache->fetch($this->getDumpTimeKey($locale));
60+
$meta = unserialize($this->cache->fetch($this->getMetaDataKey($locale)));
61+
foreach ($meta as $resource) {
62+
if (!$resource->isFresh($time)) {
63+
return false;
64+
}
65+
}
66+
}
67+
68+
return true;
69+
}
70+
71+
/**
72+
* {@inheritdoc}
73+
*/
74+
public function load($locale, array $options = array())
75+
{
76+
$messages = new DoctrineMessageCatalogue($locale, $this->cache, $this->getCatalogueIdentifier($locale, $options));
77+
$catalogue = $messages;
78+
while ($fallbackLocale = $this->cache->fetch($this->getFallbackLocaleKey($catalogue->getLocale()))) {
79+
$fallback = new DoctrineMessageCatalogue($fallbackLocale, $this->cache, $this->getCatalogueIdentifier($locale, $options));
80+
$catalogue->addFallbackCatalogue($fallback);
81+
$catalogue = $fallback;
82+
}
83+
84+
return $messages;
85+
}
86+
87+
/**
88+
* {@inheritdoc}
89+
*/
90+
public function dump(MessageCatalogueInterface $messages, array $options = array())
91+
{
92+
$resourcesHash = $this->getCatalogueIdentifier($messages->getLocale(), $options);
93+
while ($messages) {
94+
$catalogue = new DoctrineMessageCatalogue($messages->getLocale(), $this->cache, $resourcesHash);
95+
$catalogue->addCatalogue($messages);
96+
97+
$this->dumpMetaDataCatalogue($messages->getLocale(), $messages->getResources(), $resourcesHash);
98+
if ($fallback = $messages->getFallbackCatalogue()) {
99+
$this->cache->save($this->getFallbackLocaleKey($messages->getLocale()), $fallback->getLocale());
100+
}
101+
102+
$messages = $messages->getFallbackCatalogue();
103+
}
104+
}
105+
106+
private function getCatalogueIdentifier($locale, $options)
107+
{
108+
return sha1(serialize(array(
109+
'resources' => $options['resources'],
110+
'fallback_locales' => $options['fallback_locales'],
111+
)));
112+
}
113+
114+
private function dumpMetaDataCatalogue($locale, $metadata, $resourcesHash)
115+
{
116+
// $catalogueIdentifier = $this->getCatalogueIdentifier($locale, $options);
117+
$this->cache->save($this->getMetaDataKey($locale), serialize($metadata));
118+
$this->cache->save($this->getCatalogueHashKey($resourcesHash), $resourcesHash);
119+
$this->cache->save($this->getDumpTimeKey($locale), time());
120+
}
121+
122+
private function getDumpTimeKey($locale)
123+
{
124+
return self::CACHE_DUMP_TIME.'_'.$locale;
125+
}
126+
127+
private function getMetaDataKey($locale)
128+
{
129+
return self::CACHE_META_DATA.'_'.$locale;
130+
}
131+
132+
private function getCatalogueHashKey($locale)
133+
{
134+
return self::CACHE_CATALOGUE_HASH.'_'.$locale;
135+
}
136+
137+
private function getFallbackLocaleKey($locale)
138+
{
139+
return self::CATALOGUE_FALLBACK_LOCALE.'_'.$locale;
140+
}
141+
}

0 commit comments

Comments
 (0)
0