Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5854add

Browse files
committed
remove AbstractFileDumper, restore FileDumper.
add array type hints
1 parent d8defa7 commit 5854add

File tree

6 files changed

+116
-146
lines changed

6 files changed

+116
-146
lines changed

src/Symfony/Component/Translation/Dumper/AbstractFileDumper.php

Lines changed: 0 additions & 122 deletions
This file was deleted.

src/Symfony/Component/Translation/Dumper/AbstractTreeFileDumper.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,35 @@
1818
* e.g. this
1919
* 'foo.bar1' => 'test1',
2020
* 'foo.bar2' => 'test2'
21-
*
2221
* dumps as follows:
2322
* foo:
2423
* bar1: test1
2524
* bar2: test2
2625
*
2726
* @author Gennady Telegin <[email protected]>
2827
*/
29-
abstract class AbstractTreeFileDumper extends AbstractFileDumper
28+
abstract class AbstractTreeFileDumper extends FileDumper
3029
{
3130
/**
3231
* {@inheritdoc}
3332
*/
34-
protected function formatCatalogue(MessageCatalogue $messages, $domain, $options = array())
33+
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
3534
{
3635
$data = $messages->all($domain);
3736

38-
if (array_key_exists('as_tree', $options) && $options['as_tree']) {
37+
if (isset($options['as_tree']) && $options['as_tree']) {
3938
$data = $this->expandToTree($data);
4039
}
4140

4241
return $this->formatMessages($data, $options);
4342
}
4443

4544
/**
46-
* This function provided for backward compatibility for those
47-
* who extend YamlFileDumper, PhpFileDumper or JsonFileDumper classes.
45+
* This function provided for backward compatibility for those.
4846
*
4947
* @param MessageCatalogue $messages
50-
* @param string $domain
48+
* @param string $domain
49+
*
5150
* @return string representation
5251
*/
5352
protected function format(MessageCatalogue $messages, $domain)
@@ -60,9 +59,10 @@ protected function format(MessageCatalogue $messages, $domain)
6059
*
6160
* @param array $messages
6261
* @param array $options
62+
*
6363
* @return string representation
6464
*/
65-
abstract protected function formatMessages(array $messages, $options = array());
65+
abstract protected function formatMessages(array $messages, array $options = array());
6666

6767
private function expandToTree(array $messages)
6868
{
@@ -75,18 +75,18 @@ private function expandToTree(array $messages)
7575
return $tree;
7676
}
7777

78-
private function expandElement(array & $tree, array $parts, $value)
78+
private function expandElement(array &$tree, array $parts, $value)
7979
{
80-
$referenceToElement = & $this->getElementByPath($tree, $parts);
80+
$referenceToElement = &$this->getElementByPath($tree, $parts);
8181

8282
$referenceToElement = $value;
8383

8484
unset($referenceToElement);
8585
}
8686

87-
private function & getElementByPath(array & $tree, array $parts)
87+
private function &getElementByPath(array &$tree, array $parts)
8888
{
89-
$elem = & $tree;
89+
$elem = &$tree;
9090
$parentOfElem = null;
9191

9292
foreach ($parts as $i => $part) {
@@ -98,12 +98,12 @@ private function & getElementByPath(array & $tree, array $parts)
9898
* 'foo" was string before we found array {bar: test2}.
9999
* Treat new element as string too.
100100
*/
101-
$elem = & $elem[ implode('.', array_slice($parts, $i)) ];
101+
$elem = &$elem[ implode('.', array_slice($parts, $i)) ];
102102
break;
103103
}
104104

105-
$parentOfElem = & $elem;
106-
$elem = & $elem[$part];
105+
$parentOfElem = &$elem;
106+
$elem = &$elem[$part];
107107
}
108108

109109
if (is_array($elem) && count($elem) > 0 && $parentOfElem) {
@@ -120,7 +120,7 @@ private function & getElementByPath(array & $tree, array $parts)
120120
return $elem;
121121
}
122122

123-
private function cancelExpand(array & $tree, $prefix, array $node)
123+
private function cancelExpand(array &$tree, $prefix, array $node)
124124
{
125125
$prefix .= '.';
126126

src/Symfony/Component/Translation/Dumper/FileDumper.php

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,115 @@
2222
*
2323
* @author Michel Salib <[email protected]>
2424
*/
25-
abstract class FileDumper extends AbstractFileDumper
25+
abstract class FileDumper implements DumperInterface
2626
{
27+
/**
28+
* A template for the relative paths to files.
29+
*
30+
* @var string
31+
*/
32+
protected $relativePathTemplate = '%domain%.%locale%.%extension%';
33+
34+
/**
35+
* Make file backup before the dump.
36+
*
37+
* @var bool
38+
*/
39+
private $backup = true;
40+
41+
/**
42+
* Sets the template for the relative paths to files.
43+
*
44+
* @param string $relativePathTemplate A template for the relative paths to files
45+
*/
46+
public function setRelativePathTemplate($relativePathTemplate)
47+
{
48+
$this->relativePathTemplate = $relativePathTemplate;
49+
}
50+
51+
/**
52+
* Sets backup flag.
53+
*
54+
* @param bool
55+
*/
56+
public function setBackup($backup)
57+
{
58+
$this->backup = $backup;
59+
}
60+
2761
/**
2862
* {@inheritdoc}
2963
*/
30-
protected function formatCatalogue(MessageCatalogue $messages, $domain, $options = array())
64+
public function dump(MessageCatalogue $messages, $options = array())
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+
// backup
73+
$fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale());
74+
if (file_exists($fullpath)) {
75+
if ($this->backup) {
76+
copy($fullpath, $fullpath.'~');
77+
}
78+
} else {
79+
$directory = dirname($fullpath);
80+
if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
81+
throw new \RuntimeException(sprintf('Unable to create directory "%s".', $directory));
82+
}
83+
}
84+
// save file
85+
file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
86+
}
87+
}
88+
89+
/**
90+
* Transforms a domain of a message catalogue to its string representation.
91+
*
92+
* @param MessageCatalogue $messages
93+
* @param string $domain
94+
*
95+
* @return string representation
96+
*/
97+
protected function formatCatalogue(MessageCatalogue $messages, $domain, array $options = array())
3198
{
3299
return $this->format($messages, $domain);
33100
}
34101

35102
/**
36103
* Transforms a domain of a message catalogue to its string representation.
104+
* This function provided for backward compatibility.
37105
*
38106
* @param MessageCatalogue $messages
39107
* @param string $domain
40108
*
41109
* @return string representation
42110
*/
43111
abstract protected function format(MessageCatalogue $messages, $domain);
112+
113+
/**
114+
* Gets the file extension of the dumper.
115+
*
116+
* @return string file extension
117+
*/
118+
abstract protected function getExtension();
119+
120+
/**
121+
* Gets the relative file path using the template.
122+
*
123+
* @param string $domain The domain
124+
* @param string $locale The locale
125+
*
126+
* @return string The relative file path
127+
*/
128+
private function getRelativePath($domain, $locale)
129+
{
130+
return strtr($this->relativePathTemplate, array(
131+
'%domain%' => $domain,
132+
'%locale%' => $locale,
133+
'%extension%' => $this->getExtension(),
134+
));
135+
}
44136
}

src/Symfony/Component/Translation/Dumper/JsonFileDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class JsonFileDumper extends AbstractTreeFileDumper
2525
/**
2626
* {@inheritdoc}
2727
*/
28-
public function formatMessages(array $messages, $options = array())
28+
public function formatMessages(array $messages, array $options = array())
2929
{
3030
return json_encode($messages, JSON_PRETTY_PRINT);
3131
}

src/Symfony/Component/Translation/Dumper/PhpFileDumper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class PhpFileDumper extends AbstractTreeFileDumper
2121
/**
2222
* {@inheritdoc}
2323
*/
24-
protected function formatMessages(array $messages, $options = array())
24+
protected function formatMessages(array $messages, array $options = array())
2525
{
2626
$output = "<?php\n\nreturn ".var_export($messages, true).";\n";
2727

src/Symfony/Component/Translation/Dumper/YamlFileDumper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ class YamlFileDumper extends AbstractTreeFileDumper
2323
/**
2424
* {@inheritdoc}
2525
*/
26-
protected function formatMessages(array $messages, $options = array())
26+
protected function formatMessages(array $messages, array $options = array())
2727
{
28-
if (array_key_exists('inline', $options) && ($inline = (int)$options['inline']) > 0) {
28+
if (isset($options['inline']) && ($inline = (int) $options['inline']) > 0) {
2929
return Yaml::dump($messages, $inline);
30-
} else {
31-
return Yaml::dump($messages);
3230
}
31+
32+
return Yaml::dump($messages);
3333
}
3434

3535
/**

0 commit comments

Comments
 (0)