| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation\Writer; |
| 13 |
|
| 14 |
use Symfony\Component\Translation\Dumper\DumperInterface; |
| 15 |
use Symfony\Component\Translation\Exception\InvalidArgumentException; |
| 16 |
use Symfony\Component\Translation\Exception\RuntimeException; |
| 17 |
use Symfony\Component\Translation\MessageCatalogue; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
class TranslationWriter implements TranslationWriterInterface |
| 25 |
{ |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
private array $dumpers = []; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function addDumper(string $format, DumperInterface $dumper): void |
| 35 |
{ |
| 36 |
$this->dumpers[$format] = $dumper; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public function getFormats(): array |
| 43 |
{ |
| 44 |
return array_keys($this->dumpers); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function write(MessageCatalogue $catalogue, string $format, array $options = []): void |
| 56 |
{ |
| 57 |
if (!isset($this->dumpers[$format])) { |
| 58 |
throw new InvalidArgumentException(\sprintf('There is no dumper associated with format "%s".', $format)); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
$dumper = $this->dumpers[$format]; |
| 63 |
|
| 64 |
if (isset($options['path']) && !is_dir($options['path']) && !@mkdir($options['path'], 0o777, true) && !is_dir($options['path'])) { |
| 65 |
throw new RuntimeException(\sprintf('Translation Writer was not able to create directory "%s".', $options['path'])); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
$dumper->dump($catalogue, $options); |
| 70 |
} |
| 71 |
} |