Debian Codegraph @

lib/3rdParty/composer/symfony/translation/Dumper/FileDumper.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[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 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 <[email protected]>
25 */
26 abstract class FileDumper implements DumperInterface
27 {
28 /**
29 * A template for the relative paths to files.
30 */
31 protected string $relativePathTemplate = '%domain%.%locale%.%extension%';
32
33 /**
34 * Sets the template for the relative paths to files.
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 // save a file for each domain
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 * Transforms a domain of a message catalogue to its string representation.
82 */
83 abstract public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []): string;
84
85 /**
86 * Gets the file extension of the dumper.
87 */
88 abstract protected function getExtension(): string;
89
90 /**
91 * Gets the relative file path using the template.
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 }