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

Skip to content

Commit 0ed46bb

Browse files
committed
[Translation][File dumper] allow get file content without writing in file.
1 parent 95ccd3b commit 0ed46bb

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* deprecated Translator::getMessages(), rely on TranslatorBagInterface::getCatalogue() instead.
8+
* added option `save_handler` to FileDumper
89
* added option `json_encoding` to JsonFileDumper
910
* added options `as_tree`, `inline` to YamlFileDumper
1011
* added support for XLIFF target and tool attributes.

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,38 @@ public function setBackup($backup)
6363
*/
6464
public function dump(MessageCatalogue $messages, $options = array())
6565
{
66-
if (!array_key_exists('path', $options)) {
67-
throw new \InvalidArgumentException('The file dumper needs a path option.');
66+
if (!array_key_exists('path', $options) && !array_key_exists('save_handler', $options)) {
67+
throw new \InvalidArgumentException('The file dumper needs a path or save_handler option.');
6868
}
6969

7070
// save a file for each domain
7171
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.'~');
72+
$content = $this->formatCatalogue($messages, $domain, $options);
73+
if (array_key_exists('path', $options)) {
74+
// backup
75+
$fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale());
76+
if (file_exists($fullpath)) {
77+
if ($this->backup) {
78+
copy($fullpath, $fullpath.'~');
79+
}
80+
} else {
81+
$directory = dirname($fullpath);
82+
if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
83+
throw new \RuntimeException(sprintf('Unable to create directory "%s".', $directory));
84+
}
7785
}
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));
86+
87+
// save file
88+
file_put_contents($fullpath, $content);
89+
}
90+
91+
if (array_key_exists('save_handler', $options)) {
92+
if (!is_callable($options['save_handler'])) {
93+
throw new \InvalidArgumentException(sprintf('Invalid type for save_handler option. Expected callable, but got "%s".', gettype($options['save_handler'])));
8294
}
95+
96+
call_user_func($options['save_handler'], $content);
8397
}
84-
// save file
85-
file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
8698
}
8799
}
88100

src/Symfony/Component/Translation/Tests/Dumper/FileDumperTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@
1616

1717
class FileDumperTest extends \PHPUnit_Framework_TestCase
1818
{
19+
public function testDumpWithSaveHandler()
20+
{
21+
$catalogue = new MessageCatalogue('en');
22+
$catalogue->add(array('foo' => 'bar'));
23+
24+
$dumper = new ConcreteFileDumper();
25+
26+
$dumpContent = '';
27+
$dumper->dump($catalogue, array('save_handler' => function ($content) use (&$dumpContent) {
28+
$dumpContent = $content;
29+
}));
30+
31+
$this->assertEquals(serialize($catalogue->all()), $dumpContent);
32+
}
33+
1934
public function testDumpBackupsFileIfExisting()
2035
{
2136
$tempDir = sys_get_temp_dir();
@@ -60,7 +75,7 @@ class ConcreteFileDumper extends FileDumper
6075
{
6176
protected function format(MessageCatalogue $messages, $domain)
6277
{
63-
return '';
78+
return serialize($messages->all());
6479
}
6580

6681
protected function getExtension()

0 commit comments

Comments
 (0)