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