-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[2.8][Translation] added DoctrineMessageCatalogue. #16036
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/Symfony/Bridge/Doctrine/Tests/Translation/DoctrineMessageCatalogueProviderTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?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\Component\Translation\MessageCatalogueProvider\Tests; | ||
|
||
use Symfony\Component\Translation\Tests\MessageCatalogueProvider\CachedMessageCatalogueProviderTest; | ||
use Symfony\Bridge\Doctrine\Translation\DoctrineMessageCatalogueProvider; | ||
use Symfony\Component\Translation\MessageCatalogueProvider\ResourceMessageCatalogueProvider; | ||
use Doctrine\Common\Cache\ArrayCache; | ||
|
||
class DoctrineMessageCatalogueProviderTest extends CachedMessageCatalogueProviderTest | ||
{ | ||
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 getMessageCatalogueProvider($debug, $loaders = array(), $resources = array(), $fallbackLocales = array()) | ||
{ | ||
$resourceCatalogue = new ResourceMessageCatalogueProvider($loaders, $resources, $fallbackLocales); | ||
|
||
return new DoctrineMessageCatalogueProvider($resourceCatalogue, $this->cache, $debug); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Symfony/Bridge/Doctrine/Tests/Translation/DoctrineMessageCatalogueTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
224 changes: 224 additions & 0 deletions
224
src/Symfony/Bridge/Doctrine/Translation/DoctrineMessageCatalogue.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
<?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 Doctrine\Common\Cache\MultiGetCache; | ||
use Symfony\Component\Translation\MessageCatalogue; | ||
|
||
/** | ||
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com> | ||
*/ | ||
class DoctrineMessageCatalogue extends MessageCatalogue | ||
{ | ||
const PREFIX = 'sf2_translation'; | ||
const CATALOGUE_DOMAINS = 'domains'; | ||
const CATALOGUE_DOMAIN_METADATA = 'domain_meta_'; | ||
|
||
/** | ||
* @var Cache | ||
*/ | ||
private $cache; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $prefix; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $domains = array(); | ||
|
||
/** | ||
* @param string $locale | ||
* @param Cache $cache | ||
* @param string $prefix | ||
*/ | ||
public function __construct($locale, Cache $cache, $prefix = self::PREFIX) | ||
{ | ||
parent::__construct($locale); | ||
if (0 === strlen($prefix)) { | ||
throw new \InvalidArgumentException('$prefix cannot be empty.'); | ||
} | ||
|
||
$this->cache = $cache; | ||
$this->prefix = $prefix.'_'.$locale.'_'; | ||
|
||
if ($cache->contains($domainsId = $this->prefix.self::CATALOGUE_DOMAINS)) { | ||
$this->domains = $cache->fetch($domainsId); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDomains() | ||
{ | ||
return $this->domains; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function all($domain = null) | ||
{ | ||
if (!$this->cache instanceof MultiGetCache) { | ||
return array(); | ||
} | ||
|
||
$domains = $this->domains; | ||
if (null !== $domain) { | ||
$domains = array($domain); | ||
} | ||
|
||
$messages = array(); | ||
foreach ($domains as $domainMeta) { | ||
$domainIdentity = $this->getDomainMetaDataId($domainMeta); | ||
if ($this->cache->contains($domainIdentity)) { | ||
$keys = $this->cache->fetch($domainIdentity); | ||
$values = $this->cache->fetchMultiple(array_keys($keys)); | ||
foreach ($keys as $key => $id) { | ||
if (isset($values[$key])) { | ||
$messages[$domainMeta][$id] = $values[$key]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (null === $domain) { | ||
return $messages; | ||
} | ||
|
||
return isset($messages[$domain]) ? $messages[$domain] : array(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function set($id, $translation, $domain = 'messages') | ||
{ | ||
$this->add(array($id => $translation), $domain); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function has($id, $domain = 'messages') | ||
{ | ||
if ($this->defines($id, $domain)) { | ||
return true; | ||
} | ||
|
||
$fallbackCatalogue = $this->getFallbackCatalogue(); | ||
if (null !== $fallbackCatalogue) { | ||
return $fallbackCatalogue->has($id, $domain); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function defines($id, $domain = 'messages') | ||
{ | ||
$key = $this->getCacheId($domain, $id); | ||
|
||
return $this->cache->contains($key); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get($id, $domain = 'messages') | ||
{ | ||
if ($this->defines($id, $domain)) { | ||
return $this->cache->fetch($this->getCacheId($domain, $id)); | ||
} | ||
|
||
$fallbackCatalogue = $this->getFallbackCatalogue(); | ||
if (null !== $fallbackCatalogue) { | ||
return $fallbackCatalogue->get($id, $domain); | ||
} | ||
|
||
return $id; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function replace($messages, $domain = 'messages') | ||
{ | ||
$domainMetaData = array(); | ||
$domainMetaKey = $this->getDomainMetaDataId($domain); | ||
if ($this->cache->contains($domainMetaKey)) { | ||
$domainMetaData = $this->cache->fetch($domainMetaKey); | ||
} | ||
|
||
foreach ($domainMetaData as $key => $id) { | ||
if (!isset($messages[$id])) { | ||
unset($domainMetaData[$key]); | ||
$this->cache->delete($key); | ||
} | ||
} | ||
|
||
$this->cache->save($domainMetaKey, $domainMetaData); | ||
$this->add($messages, $domain); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function add($messages, $domain = 'messages') | ||
{ | ||
if (!isset($this->domains[$domain])) { | ||
$this->addDomain($domain); | ||
} | ||
|
||
$domainMetaData = array(); | ||
$domainMetaKey = $this->getDomainMetaDataId($domain); | ||
if ($this->cache->contains($domainMetaKey)) { | ||
$domainMetaData = $this->cache->fetch($domainMetaKey); | ||
} | ||
|
||
foreach ($messages as $id => $translation) { | ||
$key = $this->getCacheId($domain, $id); | ||
$domainMetaData[$key] = $id; | ||
$this->cache->save($key, $translation); | ||
} | ||
|
||
$this->addDomainMetaData($domain, $domainMetaData); | ||
} | ||
|
||
private function addDomain($domain) | ||
{ | ||
$this->domains[] = $domain; | ||
$this->cache->save($this->prefix.self::CATALOGUE_DOMAINS, $this->domains); | ||
} | ||
|
||
private function addDomainMetaData($domain, $keys = array()) | ||
{ | ||
$domainIdentity = $this->getDomainMetaDataId($domain); | ||
$this->cache->save($domainIdentity, $keys); | ||
} | ||
|
||
private function getCacheId($id, $domain = 'messages') | ||
{ | ||
return $this->prefix.$domain.'_'.sha1($id); | ||
} | ||
|
||
private function getDomainMetaDataId($domain) | ||
{ | ||
return $this->prefix.self::CATALOGUE_DOMAIN_METADATA.$domain; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for prefixes. doctrine cache has so called namespaces which people can use to avoid collisions.