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