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