Skip to content

[2.7][Translation] added message cache. #13986

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

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\Translation;

use Symfony\Bridge\Doctrine\Translation\DoctrineMessageCatalogue;
use Doctrine\Common\Cache\ArrayCache;
use Symfony\Component\Translation\Tests\MessageCatalogueTest;

class DoctrineMessageCatalogueTest extends MessageCatalogueTest
{
protected function setUp()
{
if (!interface_exists('Doctrine\Common\Cache\Cache')) {
$this->markTestSkipped('The "Doctrine Cache" is not available');
}
}

public function testAll()
{
if (!interface_exists('Doctrine\Common\Cache\MultiGetCache')) {
$this->markTestSkipped('The "Doctrine MultiGetCache" is not available');
}

parent::testAll();
}

protected function getCatalogue($locale, $messages = array())
{
$catalogue = new DoctrineMessageCatalogue($locale, new ArrayCache());
foreach ($messages as $domain => $domainMessages) {
$catalogue->add($domainMessages, $domain);
}

return $catalogue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\Translation;

use Symfony\Component\Translation\Translator;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Bridge\Doctrine\Translation\DoctrineMessageCache;
use Doctrine\Common\Cache\ArrayCache;
use Symfony\Component\Translation\Loader\PhpFileLoader;
use Symfony\Component\Translation\Dumper\PhpFileDumper;
use Symfony\Component\Translation\Tests\TranslatorCacheTest;

class TranslatorDoctrineCacheTest extends TranslatorCacheTest
{
private $cache;

protected function setUp()
{
if (!interface_exists('Doctrine\Common\Cache\Cache')) {
$this->markTestSkipped('The "Doctrine Cache" is not available');
}

$this->cache = new ArrayCache();
}

protected function getTranslator($locale, $debug)
{
$cache = new DoctrineMessageCache($this->cache, $debug);

return new Translator($locale, null, $cache);
}
}
141 changes: 141 additions & 0 deletions src/Symfony/Bridge/Doctrine/Translation/DoctrineMessageCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Translation;

use Doctrine\Common\Cache\Cache;
use Symfony\Component\Translation\MessageCacheInterface;
use Symfony\Component\Translation\MessageCatalogueInterface;

/**
* @author Abdellatif Ait Boudad <a.aitboudad@gmail.com>
*/
class DoctrineMessageCache implements MessageCacheInterface
{
const CACHE_CATALOGUE_HASH = 'catalogue_hash';
const CACHE_DUMP_TIME = 'time';
const CACHE_META_DATA = 'meta';
const CATALOGUE_FALLBACK_LOCALE = 'fallback_locale';

/**
* @var bool
*/
private $debug;

/**
* @var Cache
*/
private $cache;

/**
* @param Cache $cache
* @param bool $debug
*/
public function __construct(Cache $cache, $debug = false)
{
$this->cache = $cache;
$this->debug = $debug;
}

/**
* {@inheritdoc}
*/
public function isFresh($locale, array $options = array())
{
$catalogueIdentifier = $this->getCatalogueIdentifier($locale, $options);
if (!$this->cache->contains($this->getCatalogueHashKey($catalogueIdentifier))) {
return false;
}

if ($this->debug) {
$time = $this->cache->fetch($this->getDumpTimeKey($locale));
$meta = unserialize($this->cache->fetch($this->getMetaDataKey($locale)));
foreach ($meta as $resource) {
if (!$resource->isFresh($time)) {
return false;
}
}
}

return true;
}

/**
* {@inheritdoc}
*/
public function load($locale, array $options = array())
{
$messages = new DoctrineMessageCatalogue($locale, $this->cache, $this->getCatalogueIdentifier($locale, $options));
$catalogue = $messages;
while ($fallbackLocale = $this->cache->fetch($this->getFallbackLocaleKey($catalogue->getLocale()))) {
$fallback = new DoctrineMessageCatalogue($fallbackLocale, $this->cache, $this->getCatalogueIdentifier($locale, $options));
$catalogue->addFallbackCatalogue($fallback);
$catalogue = $fallback;
}

return $messages;
}

/**
* {@inheritdoc}
*/
public function dump(MessageCatalogueInterface $messages, array $options = array())
{
$resourcesHash = $this->getCatalogueIdentifier($messages->getLocale(), $options);
while ($messages) {
$catalogue = new DoctrineMessageCatalogue($messages->getLocale(), $this->cache, $resourcesHash);
$catalogue->addCatalogue($messages);

$this->dumpMetaDataCatalogue($messages->getLocale(), $messages->getResources(), $resourcesHash);
if ($fallback = $messages->getFallbackCatalogue()) {
$this->cache->save($this->getFallbackLocaleKey($messages->getLocale()), $fallback->getLocale());
}

$messages = $messages->getFallbackCatalogue();
}
}

private function getCatalogueIdentifier($locale, $options)
{
return sha1(serialize(array(
'resources' => $options['resources'],
'fallback_locales' => $options['fallback_locales'],
)));
}

private function dumpMetaDataCatalogue($locale, $metadata, $resourcesHash)
{
// $catalogueIdentifier = $this->getCatalogueIdentifier($locale, $options);
$this->cache->save($this->getMetaDataKey($locale), serialize($metadata));
$this->cache->save($this->getCatalogueHashKey($resourcesHash), $resourcesHash);
$this->cache->save($this->getDumpTimeKey($locale), time());
}

private function getDumpTimeKey($locale)
{
return self::CACHE_DUMP_TIME.'_'.$locale;
}

private function getMetaDataKey($locale)
{
return self::CACHE_META_DATA.'_'.$locale;
}

private function getCatalogueHashKey($locale)
{
return self::CACHE_CATALOGUE_HASH.'_'.$locale;
}

private function getFallbackLocaleKey($locale)
{
return self::CATALOGUE_FALLBACK_LOCALE.'_'.$locale;
}
}
Loading