TranslationWriter.php
66 lines
| 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 | namespace ProfilePressVendor\Symfony\Component\Translation\Writer; |
| 12 | |
| 13 | use ProfilePressVendor\Symfony\Component\Translation\Dumper\DumperInterface; |
| 14 | use ProfilePressVendor\Symfony\Component\Translation\Exception\InvalidArgumentException; |
| 15 | use ProfilePressVendor\Symfony\Component\Translation\Exception\RuntimeException; |
| 16 | use ProfilePressVendor\Symfony\Component\Translation\MessageCatalogue; |
| 17 | /** |
| 18 | * TranslationWriter writes translation messages. |
| 19 | * |
| 20 | * @author Michel Salib <michelsalib@hotmail.com> |
| 21 | */ |
| 22 | class TranslationWriter implements TranslationWriterInterface |
| 23 | { |
| 24 | /** |
| 25 | * @var array<string, DumperInterface> |
| 26 | */ |
| 27 | private $dumpers = []; |
| 28 | /** |
| 29 | * Adds a dumper to the writer. |
| 30 | */ |
| 31 | public function addDumper(string $format, DumperInterface $dumper) |
| 32 | { |
| 33 | $this->dumpers[$format] = $dumper; |
| 34 | } |
| 35 | /** |
| 36 | * Obtains the list of supported formats. |
| 37 | * |
| 38 | * @return array |
| 39 | */ |
| 40 | public function getFormats() |
| 41 | { |
| 42 | return array_keys($this->dumpers); |
| 43 | } |
| 44 | /** |
| 45 | * Writes translation from the catalogue according to the selected format. |
| 46 | * |
| 47 | * @param string $format The format to use to dump the messages |
| 48 | * @param array $options Options that are passed to the dumper |
| 49 | * |
| 50 | * @throws InvalidArgumentException |
| 51 | */ |
| 52 | public function write(MessageCatalogue $catalogue, string $format, array $options = []) |
| 53 | { |
| 54 | if (!isset($this->dumpers[$format])) { |
| 55 | throw new InvalidArgumentException(sprintf('There is no dumper associated with format "%s".', $format)); |
| 56 | } |
| 57 | // get the right dumper |
| 58 | $dumper = $this->dumpers[$format]; |
| 59 | if (isset($options['path']) && !is_dir($options['path']) && !@mkdir($options['path'], 0777, \true) && !is_dir($options['path'])) { |
| 60 | throw new RuntimeException(sprintf('Translation Writer was not able to create directory "%s".', $options['path'])); |
| 61 | } |
| 62 | // save |
| 63 | $dumper->dump($catalogue, $options); |
| 64 | } |
| 65 | } |
| 66 |