|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the PHP Translation package. |
| 5 | + * |
| 6 | + * (c) PHP Translation team <tobias.nyholm@gmail.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 Translation\SymfonyStorage; |
| 13 | + |
| 14 | +use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader as SymfonyTranslationLoader; |
| 15 | +use Symfony\Component\Translation\MessageCatalogue; |
| 16 | +use Symfony\Component\Translation\Writer\TranslationWriter; |
| 17 | +use Translation\Common\Model\Message; |
| 18 | +use Translation\Common\Storage; |
| 19 | + |
| 20 | +/** |
| 21 | + * This storage uses Symfony's writer and loader. |
| 22 | + * |
| 23 | + * @author Tobias Nyholm <tobias.nyholm@gmail.com> |
| 24 | + */ |
| 25 | +final class FileStorage implements Storage |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var TranslationWriter |
| 29 | + */ |
| 30 | + private $writer; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var TranslationLoader |
| 34 | + */ |
| 35 | + private $loader; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var string directory path |
| 39 | + */ |
| 40 | + private $dir; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var MessageCatalogue[] Fetched catalogies |
| 44 | + */ |
| 45 | + private $catalogues; |
| 46 | + |
| 47 | + /** |
| 48 | + * @param TranslationWriter $writer |
| 49 | + * @param mixed $loader |
| 50 | + * @param array $dir |
| 51 | + */ |
| 52 | + public function __construct(TranslationWriter $writer, $loader, array $dir) |
| 53 | + { |
| 54 | + if (!$loader instanceof SymfonyTranslationLoader && !$loader instanceof TranslationLoader) { |
| 55 | + throw new \LogicException('Second parameter of FileStorage must be a Symfony translation loader or implement Translation\SymfonyStorage\TranslationLoader'); |
| 56 | + } |
| 57 | + |
| 58 | + $this->writer = $writer; |
| 59 | + $this->loader = $loader; |
| 60 | + $this->dir = $dir; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * {@inheritdoc} |
| 65 | + */ |
| 66 | + public function get($locale, $domain, $key) |
| 67 | + { |
| 68 | + $catalogue = $this->getCatalogue($locale); |
| 69 | + |
| 70 | + $translation = $catalogue->get($key, $domain); |
| 71 | + |
| 72 | + return new Message($key, $domain, $locale, $translation); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * {@inheritdoc} |
| 77 | + */ |
| 78 | + public function create(Message $m) |
| 79 | + { |
| 80 | + $catalogue = $this->getCatalogue($m->getLocale()); |
| 81 | + if (!$catalogue->defines($m->getKey(), $m->getDomain())) { |
| 82 | + $catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain()); |
| 83 | + $this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain()); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * {@inheritdoc} |
| 89 | + */ |
| 90 | + public function update(Message $m) |
| 91 | + { |
| 92 | + $catalogue = $this->getCatalogue($m->getLocale()); |
| 93 | + $catalogue->set($m->getKey(), $m->getTranslation(), $m->getDomain()); |
| 94 | + $this->writeCatalogue($catalogue, $m->getLocale(), $m->getDomain()); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * {@inheritdoc} |
| 99 | + */ |
| 100 | + public function delete($locale, $domain, $key) |
| 101 | + { |
| 102 | + $catalogue = $this->getCatalogue($locale); |
| 103 | + $messages = $catalogue->all($domain); |
| 104 | + unset($messages[$key]); |
| 105 | + |
| 106 | + $catalogue->replace($messages, $domain); |
| 107 | + $this->writeCatalogue($catalogue, $locale, $domain); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Save catalogue back to file. |
| 112 | + * |
| 113 | + * @param MessageCatalogue $catalogue |
| 114 | + * @param string $domain |
| 115 | + */ |
| 116 | + private function writeCatalogue(MessageCatalogue $catalogue, $locale, $domain) |
| 117 | + { |
| 118 | + $resources = $catalogue->getResources(); |
| 119 | + foreach ($resources as $resource) { |
| 120 | + $path = $resource->getResource(); |
| 121 | + if (preg_match('|/'.$domain.'\.'.$locale.'\.([a-z]+)$|', $path, $matches)) { |
| 122 | + $this->writer->writeTranslations($catalogue, $matches[1], ['path' => str_replace($matches[0], '', $path)]); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @param string $locale |
| 129 | + * |
| 130 | + * @return MessageCatalogue |
| 131 | + */ |
| 132 | + private function getCatalogue($locale) |
| 133 | + { |
| 134 | + if (empty($this->catalogues[$locale])) { |
| 135 | + $this->loadCatalogue($locale, $this->dir); |
| 136 | + } |
| 137 | + |
| 138 | + return $this->catalogues[$locale]; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Load catalogue from files. |
| 143 | + * |
| 144 | + * @param string $locale |
| 145 | + * @param array $dirs |
| 146 | + */ |
| 147 | + private function loadCatalogue($locale, array $dirs) |
| 148 | + { |
| 149 | + $currentCatalogue = new MessageCatalogue($locale); |
| 150 | + foreach ($dirs as $path) { |
| 151 | + if (is_dir($path)) { |
| 152 | + $this->loader->loadMessages($path, $currentCatalogue); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + $this->catalogues[$locale] = $currentCatalogue; |
| 157 | + } |
| 158 | +} |
0 commit comments