2f5eaa1edf0d62674cecc821911429db59ad8881
[yaffs-website] / vendor / symfony / translation / Writer / TranslationWriter.php
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\Component\Translation\Writer;
13
14 use Symfony\Component\Translation\MessageCatalogue;
15 use Symfony\Component\Translation\Dumper\DumperInterface;
16
17 /**
18  * TranslationWriter writes translation messages.
19  *
20  * @author Michel Salib <michelsalib@hotmail.com>
21  */
22 class TranslationWriter
23 {
24     /**
25      * Dumpers used for export.
26      *
27      * @var array
28      */
29     private $dumpers = array();
30
31     /**
32      * Adds a dumper to the writer.
33      *
34      * @param string          $format The format of the dumper
35      * @param DumperInterface $dumper The dumper
36      */
37     public function addDumper($format, DumperInterface $dumper)
38     {
39         $this->dumpers[$format] = $dumper;
40     }
41
42     /**
43      * Disables dumper backup.
44      */
45     public function disableBackup()
46     {
47         foreach ($this->dumpers as $dumper) {
48             if (method_exists($dumper, 'setBackup')) {
49                 $dumper->setBackup(false);
50             }
51         }
52     }
53
54     /**
55      * Obtains the list of supported formats.
56      *
57      * @return array
58      */
59     public function getFormats()
60     {
61         return array_keys($this->dumpers);
62     }
63
64     /**
65      * Writes translation from the catalogue according to the selected format.
66      *
67      * @param MessageCatalogue $catalogue The message catalogue to dump
68      * @param string           $format    The format to use to dump the messages
69      * @param array            $options   Options that are passed to the dumper
70      *
71      * @throws \InvalidArgumentException
72      */
73     public function writeTranslations(MessageCatalogue $catalogue, $format, $options = array())
74     {
75         if (!isset($this->dumpers[$format])) {
76             throw new \InvalidArgumentException(sprintf('There is no dumper associated with format "%s".', $format));
77         }
78
79         // get the right dumper
80         $dumper = $this->dumpers[$format];
81
82         if (isset($options['path']) && !is_dir($options['path']) && !@mkdir($options['path'], 0777, true) && !is_dir($options['path'])) {
83             throw new \RuntimeException(sprintf('Translation Writer was not able to create directory "%s"', $options['path']));
84         }
85
86         // save
87         $dumper->dump($catalogue, $options);
88     }
89 }