Skip to content

Commit 6d2666e

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

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ CHANGELOG
55
-----
66

77
* deprecated Translator::getMessages(), rely on TranslatorBagInterface::getCatalogue() instead.
8-
* added option `json_encoding` to JsonFileDumper
9-
* added options `as_tree`, `inline` to YamlFileDumper
8+
* added option `save_handler` to FileDumper.
9+
* added option `json_encoding` to JsonFileDumper.
10+
* added options `as_tree`, `inline` to YamlFileDumper.
1011
* added support for XLIFF target and tool attributes.
1112
* added message parameters to DataCollectorTranslator.
1213
* [DEPRECATION] The `DiffOperation` class has been deprecated and

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy