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

Skip to content

Commit 8969df2

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

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,34 @@ 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('format_callback', $options)) {
67+
throw new \InvalidArgumentException('The file dumper needs a path or format_callback 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.'~');
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));
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+
}
8285
}
86+
87+
// save file
88+
file_put_contents($fullpath, $content);
89+
}
90+
91+
if (array_key_exists('format_callback', $options) && is_callable($options['format_callback'])) {
92+
call_user_func($options['format_callback'], $content);
8393
}
84-
// save file
85-
file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
8694
}
8795
}
8896

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@
1616

1717
class FileDumperTest extends \PHPUnit_Framework_TestCase
1818
{
19+
public function testDumpWithFormatCallback()
20+
{
21+
$catalogue = new MessageCatalogue('en');
22+
$catalogue->add(array('foo' => 'bar'));
23+
24+
$dumper = new ConcreteFileDumper();
25+
$format = false;
26+
$dumper->dump($catalogue, array('format_callback' => function ($content) use (&$format) {
27+
$format = true;
28+
}));
29+
30+
$this->assertTrue($format);
31+
}
32+
1933
public function testDumpBackupsFileIfExisting()
2034
{
2135
$tempDir = sys_get_temp_dir();

0 commit comments

Comments
 (0)