5ff5cd855dfa4ac42a51b99e4f0f7ef9a7b808fd
[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 use Symfony\Component\Translation\Exception\InvalidArgumentException;
17 use Symfony\Component\Translation\Exception\RuntimeException;
18
19 /**
20  * TranslationWriter writes translation messages.
21  *
22  * @author Michel Salib <michelsalib@hotmail.com>
23  */
24 class TranslationWriter implements TranslationWriterInterface
25 {
26     private $dumpers = array();
27
28     /**
29      * Adds a dumper to the writer.
30      *
31      * @param string          $format The format of the dumper
32      * @param DumperInterface $dumper The dumper
33      */
34     public function addDumper($format, DumperInterface $dumper)
35     {
36         $this->dumpers[$format] = $dumper;
37     }
38
39     /**
40      * Disables dumper backup.
41      */
42     public function disableBackup()
43     {
44         foreach ($this->dumpers as $dumper) {
45             if (method_exists($dumper, 'setBackup')) {
46                 $dumper->setBackup(false);
47             }
48         }
49     }
50
51     /**
52      * Obtains the list of supported formats.
53      *
54      * @return array
55      */
56     public function getFormats()
57     {
58         return array_keys($this->dumpers);
59     }
60
61     /**
62      * Writes translation from the catalogue according to the selected format.
63      *
64      * @param MessageCatalogue $catalogue The message catalogue to write
65      * @param string           $format    The format to use to dump the messages
66      * @param array            $options   Options that are passed to the dumper
67      *
68      * @throws InvalidArgumentException
69      */
70     public function write(MessageCatalogue $catalogue, $format, $options = array())
71     {
72         if (!isset($this->dumpers[$format])) {
73             throw new InvalidArgumentException(sprintf('There is no dumper associated with format "%s".', $format));
74         }
75
76         // get the right dumper
77         $dumper = $this->dumpers[$format];
78
79         if (isset($options['path']) && !is_dir($options['path']) && !@mkdir($options['path'], 0777, true) && !is_dir($options['path'])) {
80             throw new RuntimeException(sprintf('Translation Writer was not able to create directory "%s"', $options['path']));
81         }
82
83         // save
84         $dumper->dump($catalogue, $options);
85     }
86
87     /**
88      * Writes translation from the catalogue according to the selected format.
89      *
90      * @param MessageCatalogue $catalogue The message catalogue to write
91      * @param string           $format    The format to use to dump the messages
92      * @param array            $options   Options that are passed to the dumper
93      *
94      * @throws InvalidArgumentException
95      *
96      * @deprecated since 3.4 will be removed in 4.0. Use write instead.
97      */
98     public function writeTranslations(MessageCatalogue $catalogue, $format, $options = array())
99     {
100         @trigger_error(sprintf('Method %s() is deprecated since Symfony 3.4 and will be removed in 4.0. Use write() instead.', __METHOD__), E_USER_DEPRECATED);
101         $this->write($catalogue, $format, $options);
102     }
103 }