| 148 |
lars |
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\Dumper;
|
|
|
13 |
|
|
|
14 |
use Symfony\Component\Translation\Exception\InvalidArgumentException;
|
|
|
15 |
use Symfony\Component\Translation\Exception\RuntimeException;
|
|
|
16 |
use Symfony\Component\Translation\MessageCatalogue;
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* FileDumper is an implementation of DumperInterface that dump a message catalogue to file(s).
|
|
|
20 |
*
|
|
|
21 |
* Options:
|
|
|
22 |
* - path (mandatory): the directory where the files should be saved
|
|
|
23 |
*
|
|
|
24 |
* @author Michel Salib <michelsalib@hotmail.com>
|
|
|
25 |
*/
|
|
|
26 |
abstract class FileDumper implements DumperInterface
|
|
|
27 |
{
|
|
|
28 |
/**
|
|
|
29 |
* A template for the relative paths to files.
|
|
|
30 |
*
|
|
|
31 |
* @var string
|
|
|
32 |
*/
|
|
|
33 |
protected $relativePathTemplate = '%domain%.%locale%.%extension%';
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Sets the template for the relative paths to files.
|
|
|
37 |
*
|
|
|
38 |
* @param string $relativePathTemplate A template for the relative paths to files
|
|
|
39 |
*/
|
|
|
40 |
public function setRelativePathTemplate(string $relativePathTemplate)
|
|
|
41 |
{
|
|
|
42 |
$this->relativePathTemplate = $relativePathTemplate;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
public function dump(MessageCatalogue $messages, array $options = [])
|
|
|
46 |
{
|
|
|
47 |
if (!\array_key_exists('path', $options)) {
|
|
|
48 |
throw new InvalidArgumentException('The file dumper needs a path option.');
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
// save a file for each domain
|
|
|
52 |
foreach ($messages->getDomains() as $domain) {
|
|
|
53 |
$fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale());
|
|
|
54 |
if (!file_exists($fullpath)) {
|
|
|
55 |
$directory = \dirname($fullpath);
|
|
|
56 |
if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
|
|
|
57 |
throw new RuntimeException(sprintf('Unable to create directory "%s".', $directory));
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
$intlDomain = $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX;
|
|
|
62 |
$intlMessages = $messages->all($intlDomain);
|
|
|
63 |
|
|
|
64 |
if ($intlMessages) {
|
|
|
65 |
$intlPath = $options['path'].'/'.$this->getRelativePath($intlDomain, $messages->getLocale());
|
|
|
66 |
file_put_contents($intlPath, $this->formatCatalogue($messages, $intlDomain, $options));
|
|
|
67 |
|
|
|
68 |
$messages->replace([], $intlDomain);
|
|
|
69 |
|
|
|
70 |
try {
|
|
|
71 |
if ($messages->all($domain)) {
|
|
|
72 |
file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
|
|
|
73 |
}
|
|
|
74 |
continue;
|
|
|
75 |
} finally {
|
|
|
76 |
$messages->replace($intlMessages, $intlDomain);
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Transforms a domain of a message catalogue to its string representation.
|
|
|
86 |
*/
|
|
|
87 |
abstract public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []): string;
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Gets the file extension of the dumper.
|
|
|
91 |
*/
|
|
|
92 |
abstract protected function getExtension(): string;
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Gets the relative file path using the template.
|
|
|
96 |
*/
|
|
|
97 |
private function getRelativePath(string $domain, string $locale): string
|
|
|
98 |
{
|
|
|
99 |
return strtr($this->relativePathTemplate, [
|
|
|
100 |
'%domain%' => $domain,
|
|
|
101 |
'%locale%' => $locale,
|
|
|
102 |
'%extension%' => $this->getExtension(),
|
|
|
103 |
]);
|
|
|
104 |
}
|
|
|
105 |
}
|