1 | 1 |
deleted file mode 100644 |
... | ... |
@@ -1,132 +0,0 @@ |
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($relativePathTemplate) |
|
41 |
- { |
|
42 |
- $this->relativePathTemplate = $relativePathTemplate; |
|
43 |
- } |
|
44 |
- |
|
45 |
- /** |
|
46 |
- * Sets backup flag. |
|
47 |
- * |
|
48 |
- * @param bool $backup |
|
49 |
- * |
|
50 |
- * @deprecated since Symfony 4.1 |
|
51 |
- */ |
|
52 |
- public function setBackup($backup) |
|
53 |
- { |
|
54 |
- @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.1.', __METHOD__), E_USER_DEPRECATED); |
|
55 |
- |
|
56 |
- if (false !== $backup) { |
|
57 |
- throw new \LogicException('The backup feature is no longer supported.'); |
|
58 |
- } |
|
59 |
- } |
|
60 |
- |
|
61 |
- /** |
|
62 |
- * {@inheritdoc} |
|
63 |
- */ |
|
64 |
- public function dump(MessageCatalogue $messages, $options = []) |
|
65 |
- { |
|
66 |
- if (!\array_key_exists('path', $options)) { |
|
67 |
- throw new InvalidArgumentException('The file dumper needs a path option.'); |
|
68 |
- } |
|
69 |
- |
|
70 |
- // save a file for each domain |
|
71 |
- foreach ($messages->getDomains() as $domain) { |
|
72 |
- $fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale()); |
|
73 |
- if (!file_exists($fullpath)) { |
|
74 |
- $directory = \dirname($fullpath); |
|
75 |
- if (!file_exists($directory) && !@mkdir($directory, 0777, true)) { |
|
76 |
- throw new RuntimeException(sprintf('Unable to create directory "%s".', $directory)); |
|
77 |
- } |
|
78 |
- } |
|
79 |
- |
|
80 |
- $intlDomain = $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX; |
|
81 |
- $intlMessages = $messages->all($intlDomain); |
|
82 |
- |
|
83 |
- if ($intlMessages) { |
|
84 |
- $intlPath = $options['path'].'/'.$this->getRelativePath($intlDomain, $messages->getLocale()); |
|
85 |
- file_put_contents($intlPath, $this->formatCatalogue($messages, $intlDomain, $options)); |
|
86 |
- |
|
87 |
- $messages->replace([], $intlDomain); |
|
88 |
- |
|
89 |
- try { |
|
90 |
- if ($messages->all($domain)) { |
|
91 |
- file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options)); |
|
92 |
- } |
|
93 |
- continue; |
|
94 |
- } finally { |
|
95 |
- $messages->replace($intlMessages, $intlDomain); |
|
96 |
- } |
|
97 |
- } |
|
98 |
- |
|
99 |
- file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options)); |
|
100 |
- } |
|
101 |
- } |
|
102 |
- |
|
103 |
- /** |
|
104 |
- * Transforms a domain of a message catalogue to its string representation. |
|
105 |
- * |
|
106 |
- * @param MessageCatalogue $messages |
|
107 |
- * @param string $domain |
|
108 |
- * @param array $options |
|
109 |
- * |
|
110 |
- * @return string representation |
|
111 |
- */ |
|
112 |
- abstract public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = []); |
|
113 |
- |
|
114 |
- /** |
|
115 |
- * Gets the file extension of the dumper. |
|
116 |
- * |
|
117 |
- * @return string file extension |
|
118 |
- */ |
|
119 |
- abstract protected function getExtension(); |
|
120 |
- |
|
121 |
- /** |
|
122 |
- * Gets the relative file path using the template. |
|
123 |
- */ |
|
124 |
- private function getRelativePath(string $domain, string $locale): string |
|
125 |
- { |
|
126 |
- return strtr($this->relativePathTemplate, [ |
|
127 |
- '%domain%' => $domain, |
|
128 |
- '%locale%' => $locale, |
|
129 |
- '%extension%' => $this->getExtension(), |
|
130 |
- ]); |
|
131 |
- } |
|
132 |
-} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,132 @@ |
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($relativePathTemplate) |
|
41 |
+ { |
|
42 |
+ $this->relativePathTemplate = $relativePathTemplate; |
|
43 |
+ } |
|
44 |
+ |
|
45 |
+ /** |
|
46 |
+ * Sets backup flag. |
|
47 |
+ * |
|
48 |
+ * @param bool $backup |
|
49 |
+ * |
|
50 |
+ * @deprecated since Symfony 4.1 |
|
51 |
+ */ |
|
52 |
+ public function setBackup($backup) |
|
53 |
+ { |
|
54 |
+ @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.1.', __METHOD__), E_USER_DEPRECATED); |
|
55 |
+ |
|
56 |
+ if (false !== $backup) { |
|
57 |
+ throw new \LogicException('The backup feature is no longer supported.'); |
|
58 |
+ } |
|
59 |
+ } |
|
60 |
+ |
|
61 |
+ /** |
|
62 |
+ * {@inheritdoc} |
|
63 |
+ */ |
|
64 |
+ public function dump(MessageCatalogue $messages, $options = []) |
|
65 |
+ { |
|
66 |
+ if (!\array_key_exists('path', $options)) { |
|
67 |
+ throw new InvalidArgumentException('The file dumper needs a path option.'); |
|
68 |
+ } |
|
69 |
+ |
|
70 |
+ // save a file for each domain |
|
71 |
+ foreach ($messages->getDomains() as $domain) { |
|
72 |
+ $fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale()); |
|
73 |
+ if (!file_exists($fullpath)) { |
|
74 |
+ $directory = \dirname($fullpath); |
|
75 |
+ if (!file_exists($directory) && !@mkdir($directory, 0777, true)) { |
|
76 |
+ throw new RuntimeException(sprintf('Unable to create directory "%s".', $directory)); |
|
77 |
+ } |
|
78 |
+ } |
|
79 |
+ |
|
80 |
+ $intlDomain = $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX; |
|
81 |
+ $intlMessages = $messages->all($intlDomain); |
|
82 |
+ |
|
83 |
+ if ($intlMessages) { |
|
84 |
+ $intlPath = $options['path'].'/'.$this->getRelativePath($intlDomain, $messages->getLocale()); |
|
85 |
+ file_put_contents($intlPath, $this->formatCatalogue($messages, $intlDomain, $options)); |
|
86 |
+ |
|
87 |
+ $messages->replace([], $intlDomain); |
|
88 |
+ |
|
89 |
+ try { |
|
90 |
+ if ($messages->all($domain)) { |
|
91 |
+ file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options)); |
|
92 |
+ } |
|
93 |
+ continue; |
|
94 |
+ } finally { |
|
95 |
+ $messages->replace($intlMessages, $intlDomain); |
|
96 |
+ } |
|
97 |
+ } |
|
98 |
+ |
|
99 |
+ file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options)); |
|
100 |
+ } |
|
101 |
+ } |
|
102 |
+ |
|
103 |
+ /** |
|
104 |
+ * Transforms a domain of a message catalogue to its string representation. |
|
105 |
+ * |
|
106 |
+ * @param MessageCatalogue $messages |
|
107 |
+ * @param string $domain |
|
108 |
+ * @param array $options |
|
109 |
+ * |
|
110 |
+ * @return string representation |
|
111 |
+ */ |
|
112 |
+ abstract public function formatCatalogue(MessageCatalogue $messages, $domain, array $options = []); |
|
113 |
+ |
|
114 |
+ /** |
|
115 |
+ * Gets the file extension of the dumper. |
|
116 |
+ * |
|
117 |
+ * @return string file extension |
|
118 |
+ */ |
|
119 |
+ abstract protected function getExtension(); |
|
120 |
+ |
|
121 |
+ /** |
|
122 |
+ * Gets the relative file path using the template. |
|
123 |
+ */ |
|
124 |
+ private function getRelativePath(string $domain, string $locale): string |
|
125 |
+ { |
|
126 |
+ return strtr($this->relativePathTemplate, [ |
|
127 |
+ '%domain%' => $domain, |
|
128 |
+ '%locale%' => $locale, |
|
129 |
+ '%extension%' => $this->getExtension(), |
|
130 |
+ ]); |
|
131 |
+ } |
|
132 |
+} |