| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
abstract class FileDumper implements DumperInterface |
| 27 |
{ |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
protected string $relativePathTemplate = '%domain%.%locale%.%extension%'; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public function setRelativePathTemplate(string $relativePathTemplate): void |
| 37 |
{ |
| 38 |
$this->relativePathTemplate = $relativePathTemplate; |
| 39 |
} |
| 40 |
|
| 41 |
public function dump(MessageCatalogue $messages, array $options = []): void |
| 42 |
{ |
| 43 |
if (!\array_key_exists('path', $options)) { |
| 44 |
throw new InvalidArgumentException('The file dumper needs a path option.'); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
foreach ($messages->getDomains() as $domain) { |
| 49 |
$fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale()); |
| 50 |
if (!file_exists($fullpath)) { |
| 51 |
$directory = \dirname($fullpath); |
| 52 |
if (!file_exists($directory) && !@mkdir($directory, 0o777, true)) { |
| 53 |
throw new RuntimeException(\sprintf('Unable to create directory "%s".', $directory)); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
$intlDomain = $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX; |
| 58 |
$intlMessages = $messages->all($intlDomain); |
| 59 |
|
| 60 |
if ($intlMessages) { |
| 61 |
$intlPath = $options['path'].'/'.$this->getRelativePath($intlDomain, $messages->getLocale()); |
| 62 |
file_put_contents($intlPath, $this->formatCatalogue($messages, $intlDomain, $options)); |
| 63 |
|
| 64 |
$messages->replace([], $intlDomain); |
| 65 |
|
| 66 |
try { |
| 67 |
if ($messages->all($domain)) { |
| 68 |
file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options)); |
| 69 |
} |
| 70 |
continue; |
| 71 |
} finally { |
| 72 |
$messages->replace($intlMessages, $intlDomain); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options)); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
abstract public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []): string; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
abstract protected function getExtension(): string; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
private function getRelativePath(string $domain, string $locale): string |
| 94 |
{ |
| 95 |
return strtr($this->relativePathTemplate, [ |
| 96 |
'%domain%' => $domain, |
| 97 |
'%locale%' => $locale, |
| 98 |
'%extension%' => $this->getExtension(), |
| 99 |
]); |
| 100 |
} |
| 101 |
} |