Skip to content

Commit

Permalink
Merge branch '4.4' into 5.0
Browse files Browse the repository at this point in the history
* 4.4:
  [DI] Fix EnvVar not loaded when Loader requires an env var
  Fixed #34713 Move new messages to intl domain when possible
  [FrameworkBundle] Fix small typo in output comment
  chown and chgrp should also accept int as owner and group
  Revert "Fixed translations file dumper behavior"
  Fix RememberMe with null password
  [Validator] Fix plurals for sr_Latn (Serbian language written in latin script) validation messages
  Set booted flag to false when test kernel is unset
  [FrameworkBundle] remove messenger cache if not enabled
  [PhpUnitBridge][SymfonyTestsListenerTrait] Remove some unneeded code
  [HttpClient] Fix strict parsing of response status codes
  fix PHP const mapping keys using the inline notation
  [SecurityBundle] Drop duplicated code
  [FrameworkBundle] Make sure one can use fragments.hinclude_default_template
  Fix that no-cache requires positive validation with the origin, even for fresh responses
  Improve upgrading instructions for deprecated router options
  [DI] Suggest typed argument when binding fails with untyped argument
  • Loading branch information
nicolas-grekas committed Jan 21, 2020
2 parents a75b1dd + f5d2ac4 commit 28e1054
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 deletions.
43 changes: 21 additions & 22 deletions Dumper/FileDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,36 @@ public function dump(MessageCatalogue $messages, array $options = [])
throw new InvalidArgumentException('The file dumper needs a path option.');
}

$hasMessageFormatter = class_exists(\MessageFormatter::class);

// save a file for each domain
foreach ($messages->getDomains() as $domain) {
if ($hasMessageFormatter) {
$defaultDomain = $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX;
$altDomain = $domain;
} else {
$defaultDomain = $domain;
$altDomain = $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX;
}
$defaultPath = $options['path'].'/'.$this->getRelativePath($defaultDomain, $messages->getLocale());
$altPath = $options['path'].'/'.$this->getRelativePath($altDomain, $messages->getLocale());

if (!file_exists($defaultPath) && file_exists($altPath)) {
[$defaultPath, $altPath] = [$altPath, $defaultPath];
}

if (!file_exists($defaultPath)) {
$directory = \dirname($defaultPath);
$fullpath = $options['path'].'/'.$this->getRelativePath($domain, $messages->getLocale());
if (!file_exists($fullpath)) {
$directory = \dirname($fullpath);
if (!file_exists($directory) && !@mkdir($directory, 0777, true)) {
throw new RuntimeException(sprintf('Unable to create directory "%s".', $directory));
}
}

if (file_exists($altPath)) {
// clear alternative translation file
file_put_contents($altPath, $this->formatCatalogue(new MessageCatalogue($messages->getLocale()), $altDomain, $options));
$intlDomain = $domain.MessageCatalogue::INTL_DOMAIN_SUFFIX;
$intlMessages = $messages->all($intlDomain);

if ($intlMessages) {
$intlPath = $options['path'].'/'.$this->getRelativePath($intlDomain, $messages->getLocale());
file_put_contents($intlPath, $this->formatCatalogue($messages, $intlDomain, $options));

$messages->replace([], $intlDomain);

try {
if ($messages->all($domain)) {
file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
}
continue;
} finally {
$messages->replace($intlMessages, $intlDomain);
}
}

file_put_contents($defaultPath, $this->formatCatalogue($messages, $domain, $options));
file_put_contents($fullpath, $this->formatCatalogue($messages, $domain, $options));
}
}

Expand Down
17 changes: 7 additions & 10 deletions Tests/Dumper/FileDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,11 @@ public function testDump()
$dumper = new ConcreteFileDumper();
$dumper->dump($catalogue, ['path' => $tempDir]);

$suffix = class_exists(\MessageFormatter::class) ? '+intl-icu' : '';
$this->assertFileExists($tempDir."/messages$suffix.en.concrete");
$this->assertFileExists($tempDir.'/messages.en.concrete');

@unlink($tempDir."/messages$suffix.en.concrete");
@unlink($tempDir.'/messages.en.concrete');
}

/**
* @requires extension intl
*/
public function testDumpIntl()
{
$tempDir = sys_get_temp_dir();
Expand All @@ -46,11 +42,13 @@ public function testDumpIntl()
$catalogue->add(['bar' => 'foo'], 'd2+intl-icu');

$dumper = new ConcreteFileDumper();
@unlink($tempDir.'/d2.en.concrete');
$dumper->dump($catalogue, ['path' => $tempDir]);

$this->assertFileNotExists($tempDir.'/d1.en.concrete');
$this->assertStringEqualsFile($tempDir.'/d1.en.concrete', 'foo=bar');
@unlink($tempDir.'/d1.en.concrete');

$this->assertStringEqualsFile($tempDir.'/d1+intl-icu.en.concrete', 'bar=foo&foo=bar');
$this->assertStringEqualsFile($tempDir.'/d1+intl-icu.en.concrete', 'bar=foo');
@unlink($tempDir.'/d1+intl-icu.en.concrete');

$this->assertFileNotExists($tempDir.'/d2.en.concrete');
Expand All @@ -62,8 +60,7 @@ public function testDumpCreatesNestedDirectoriesAndFile()
{
$tempDir = sys_get_temp_dir();
$translationsDir = $tempDir.'/test/translations';
$suffix = class_exists(\MessageFormatter::class) ? '+intl-icu' : '';
$file = $translationsDir."/messages$suffix.en.concrete";
$file = $translationsDir.'/messages.en.concrete';

$catalogue = new MessageCatalogue('en');
$catalogue->add(['foo' => 'bar']);
Expand Down

0 comments on commit 28e1054

Please sign in to comment.