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\Dumper;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Translation\Exception\InvalidArgumentException;
|
Chris@0
|
15 use Symfony\Component\Translation\Exception\RuntimeException;
|
Chris@17
|
16 use Symfony\Component\Translation\MessageCatalogue;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * FileDumper is an implementation of DumperInterface that dump a message catalogue to file(s).
|
Chris@0
|
20 * Performs backup of already existing files.
|
Chris@0
|
21 *
|
Chris@0
|
22 * Options:
|
Chris@0
|
23 * - path (mandatory): the directory where the files should be saved
|
Chris@0
|
24 *
|
Chris@0
|
25 * @author Michel Salib <michelsalib@hotmail.com>
|
Chris@0
|
26 */
|
Chris@0
|
27 abstract class FileDumper implements DumperInterface
|
Chris@0
|
28 {
|
Chris@0
|
29 /**
|
Chris@0
|
30 * A template for the relative paths to files.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var string
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $relativePathTemplate = '%domain%.%locale%.%extension%';
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * Make file backup before the dump.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @var bool
|
Chris@0
|
40 */
|
Chris@0
|
41 private $backup = true;
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * Sets the template for the relative paths to files.
|
Chris@0
|
45 *
|
Chris@0
|
46 * @param string $relativePathTemplate A template for the relative paths to files
|
Chris@0
|
47 */
|
Chris@0
|
48 public function setRelativePathTemplate($relativePathTemplate)
|
Chris@0
|
49 {
|
Chris@0
|
50 $this->relativePathTemplate = $relativePathTemplate;
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * Sets backup flag.
|
Chris@0
|
55 *
|
Chris@17
|
56 * @param bool $backup
|
Chris@0
|
57 */
|
Chris@0
|
58 public function setBackup($backup)
|
Chris@0
|
59 {
|
Chris@0
|
60 $this->backup = $backup;
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 /**
|
Chris@0
|
64 * {@inheritdoc}
|
Chris@0
|
65 */
|
Chris@17
|
66 public function dump(MessageCatalogue $messages, $options = [])
|
Chris@0
|
67 {
|
Chris@18
|
68 if (!\array_key_exists('path', $options)) {
|
Chris@0
|
69 throw new InvalidArgumentException('The file dumper needs a path option.');
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 // save a file for each domain
|
Chris@0
|
73 foreach ($messages->getDomains() as $domain) {
|
Chris@0
|
74 // backup
|
Chris@0
|
75 $fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale());
|
Chris@0
|
76 if (file_exists($fullpath)) {
|
Chris@0
|
77 if ($this->backup) {
|
Chris@14
|
78 @trigger_error('Creating a backup while dumping a message catalogue is deprecated since Symfony 3.1 and will be removed in 4.0. Use TranslationWriter::disableBackup() to disable the backup.', E_USER_DEPRECATED);
|
Chris@0
|
79 copy($fullpath, $fullpath.'~');
|
Chris@0
|
80 }
|
Chris@0
|
81 } else {
|
Chris@17
|
82 $directory = \dirname($fullpath);
|
Chris@0
|
83 if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
|
Chris@0
|
84 throw new RuntimeException(sprintf('Unable to create directory "%s".', $directory));
|
Chris@0
|
85 }
|
Chris@0
|
86 }
|
Chris@0
|
87 // save file
|
Chris@0
|
88 file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
|
Chris@0
|
89 }
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /**
|
Chris@0
|
93 * Transforms a domain of a message catalogue to its string representation.
|
Chris@0
|
94 *
|
Chris@0
|
95 * @param MessageCatalogue $messages
|
Chris@0
|
96 * @param string $domain
|
Chris@0
|
97 * @param array $options
|
Chris@0
|
98 *
|
Chris@0
|
99 * @return string representation
|
Chris@0
|
100 */
|
Chris@17
|
101 abstract public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = []);
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * Gets the file extension of the dumper.
|
Chris@0
|
105 *
|
Chris@0
|
106 * @return string file extension
|
Chris@0
|
107 */
|
Chris@0
|
108 abstract protected function getExtension();
|
Chris@0
|
109
|
Chris@0
|
110 /**
|
Chris@0
|
111 * Gets the relative file path using the template.
|
Chris@0
|
112 *
|
Chris@0
|
113 * @param string $domain The domain
|
Chris@0
|
114 * @param string $locale The locale
|
Chris@0
|
115 *
|
Chris@0
|
116 * @return string The relative file path
|
Chris@0
|
117 */
|
Chris@0
|
118 private function getRelativePath($domain, $locale)
|
Chris@0
|
119 {
|
Chris@17
|
120 return strtr($this->relativePathTemplate, [
|
Chris@0
|
121 '%domain%' => $domain,
|
Chris@0
|
122 '%locale%' => $locale,
|
Chris@0
|
123 '%extension%' => $this->getExtension(),
|
Chris@17
|
124 ]);
|
Chris@0
|
125 }
|
Chris@0
|
126 }
|