|
| 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