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

Skip to content

Commit 54b38e7

Browse files
welcoMatticnicolas-grekas
authored andcommitted
[Translation] [Phrase] Refacto ReadConfig and WriteConfig into arrays
1 parent f9327be commit 54b38e7

File tree

6 files changed

+87
-476
lines changed

6 files changed

+87
-476
lines changed

src/Symfony/Component/Translation/Bridge/Phrase/Config/ReadConfig.php

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

src/Symfony/Component/Translation/Bridge/Phrase/PhraseProvider.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@
1111

1212
namespace Symfony\Component\Translation\Bridge\Phrase;
1313

14-
use Psr\Cache\CacheItemInterface;
1514
use Psr\Cache\CacheItemPoolInterface;
1615
use Psr\Log\LoggerInterface;
1716
use Symfony\Component\Mime\Part\DataPart;
1817
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
19-
use Symfony\Component\Translation\Bridge\Phrase\Config\ReadConfig;
20-
use Symfony\Component\Translation\Bridge\Phrase\Config\WriteConfig;
2118
use Symfony\Component\Translation\Dumper\XliffFileDumper;
2219
use Symfony\Component\Translation\Exception\ProviderException;
2320
use Symfony\Component\Translation\Loader\LoaderInterface;
@@ -42,8 +39,9 @@ public function __construct(
4239
private readonly CacheItemPoolInterface $cache,
4340
private readonly string $defaultLocale,
4441
private readonly string $endpoint,
45-
private readonly ReadConfig $readConfig,
46-
private readonly WriteConfig $writeConfig,
42+
private array $readConfig,
43+
private array $writeConfig,
44+
private readonly bool $isFallbackLocaleEnabled = false,
4745
) {
4846
}
4947

@@ -58,7 +56,7 @@ public function write(TranslatorBagInterface $translatorBag): void
5856

5957
foreach ($translatorBag->getCatalogues() as $catalogue) {
6058
foreach ($catalogue->getDomains() as $domain) {
61-
if (0 === \count($catalogue->all($domain))) {
59+
if (!\count($catalogue->all($domain))) {
6260
continue;
6361
}
6462

@@ -67,7 +65,9 @@ public function write(TranslatorBagInterface $translatorBag): void
6765
$content = $this->xliffFileDumper->formatCatalogue($catalogue, $domain, ['default_locale' => $this->defaultLocale]);
6866
$filename = sprintf('%d-%s-%s.xlf', date('YmdHis'), $domain, $catalogue->getLocale());
6967

70-
$fields = array_merge($this->writeConfig->setTag($domain)->setLocale($phraseLocale)->getOptions(), ['file' => new DataPart($content, $filename, 'application/xml')]);
68+
$this->writeConfig['tags'] = $domain;
69+
$this->writeConfig['locale_id'] = $phraseLocale;
70+
$fields = array_merge($this->writeConfig, ['file' => new DataPart($content, $filename, 'application/xml')]);
7171

7272
$formData = new FormDataPart($fields);
7373

@@ -93,13 +93,13 @@ public function read(array $domains, array $locales): TranslatorBag
9393
$phraseLocale = $this->getLocale($locale);
9494

9595
foreach ($domains as $domain) {
96-
$this->readConfig->setTag($domain);
96+
$this->readConfig['tags'] = $domain;
9797

98-
if ($this->readConfig->isFallbackLocaleEnabled() && null !== $fallbackLocale = $this->getFallbackLocale($locale)) {
99-
$this->readConfig->setFallbackLocale($fallbackLocale);
98+
if ($this->isFallbackLocaleEnabled && null !== $fallbackLocale = $this->getFallbackLocale($locale)) {
99+
$this->readConfig['fallback_locale_id'] = $fallbackLocale;
100100
}
101101

102-
$cacheKey = $this->generateCacheKey($locale, $domain, $this->readConfig->getOptions());
102+
$cacheKey = $this->generateCacheKey($locale, $domain, $this->readConfig);
103103
$cacheItem = $this->cache->getItem($cacheKey);
104104

105105
$headers = [];
@@ -110,7 +110,7 @@ public function read(array $domains, array $locales): TranslatorBag
110110
}
111111

112112
$response = $this->httpClient->request('GET', 'locales/'.$phraseLocale.'/download', [
113-
'query' => $this->readConfig->getOptions(),
113+
'query' => $this->readConfig,
114114
'headers' => $headers,
115115
]);
116116

@@ -124,7 +124,7 @@ public function read(array $domains, array $locales): TranslatorBag
124124
$translatorBag->addCatalogue($this->loader->load($content, $locale, $domain));
125125

126126
// using weak etags, responses for requests with fallback locale enabled can not be reliably cached...
127-
if (false === $this->readConfig->isFallbackLocaleEnabled()) {
127+
if (!$this->isFallbackLocaleEnabled) {
128128
$headers = $response->getHeaders(false);
129129
$cacheItem->set(['etag' => $headers['etag'][0], 'modified' => $headers['last-modified'][0], 'content' => $content]);
130130
$this->cache->save($cacheItem);

src/Symfony/Component/Translation/Bridge/Phrase/PhraseProviderFactory.php

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
use Psr\Cache\CacheItemPoolInterface;
1515
use Psr\Log\LoggerInterface;
16-
use Symfony\Component\Translation\Bridge\Phrase\Config\ReadConfig;
17-
use Symfony\Component\Translation\Bridge\Phrase\Config\WriteConfig;
1816
use Symfony\Component\Translation\Dumper\XliffFileDumper;
1917
use Symfony\Component\Translation\Exception\UnsupportedSchemeException;
2018
use Symfony\Component\Translation\Loader\LoaderInterface;
@@ -29,6 +27,18 @@
2927
class PhraseProviderFactory extends AbstractProviderFactory
3028
{
3129
private const HOST = 'api.phrase.com';
30+
private const READ_CONFIG_DEFAULT = [
31+
'file_format' => 'symfony_xliff',
32+
'include_empty_translations' => '1',
33+
'tags' => [],
34+
'format_options' => [
35+
'enclose_in_cdata' => '1',
36+
],
37+
];
38+
private const WRITE_CONFIG_DEFAULT = [
39+
'file_format' => 'symfony_xliff',
40+
'update_translations' => '1',
41+
];
3242

3343
public function __construct(
3444
private readonly HttpClientInterface $httpClient,
@@ -60,14 +70,46 @@ public function create(Dsn $dsn): ProviderInterface
6070
],
6171
]);
6272

63-
$readConfig = ReadConfig::fromDsn($dsn);
64-
$writeConfig = WriteConfig::fromDsn($dsn);
73+
$readConfig = $this->readConfigFromDsn($dsn);
74+
$writeConfig = $this->writeConfigFromDsn($dsn);
6575

66-
return new PhraseProvider($client, $this->logger, $this->loader, $this->xliffFileDumper, $this->cache, $this->defaultLocale, $endpoint, $readConfig, $writeConfig);
76+
return new PhraseProvider($client, $this->logger, $this->loader, $this->xliffFileDumper, $this->cache, $this->defaultLocale, $endpoint, $readConfig, $writeConfig, $this->isFallbackLocaleEnabled($dsn));
6777
}
6878

6979
protected function getSupportedSchemes(): array
7080
{
7181
return ['phrase'];
7282
}
83+
84+
private function isFallbackLocaleEnabled(Dsn $dsn): bool
85+
{
86+
$options = $dsn->getOptions()['read'] ?? [];
87+
88+
return filter_var($options['fallback_locale_enabled'] ?? false, \FILTER_VALIDATE_BOOL);
89+
}
90+
91+
private function readConfigFromDsn(Dsn $dsn): array
92+
{
93+
$options = $dsn->getOptions()['read'] ?? [];
94+
95+
// enforce empty translations when fallback locale is enabled
96+
if ($this->isFallbackLocaleEnabled($dsn)) {
97+
$options['include_empty_translations'] = '1';
98+
}
99+
100+
unset($options['file_format'], $options['tags'], $options['tag'], $options['fallback_locale_id'], $options['fallback_locale_enabled']);
101+
102+
$options['format_options'] = array_merge(self::READ_CONFIG_DEFAULT['format_options'], $options['format_options'] ?? []);
103+
104+
return array_merge(self::READ_CONFIG_DEFAULT, $options);
105+
}
106+
107+
private function writeConfigFromDsn(Dsn $dsn): array
108+
{
109+
$options = $dsn->getOptions()['write'] ?? [];
110+
111+
unset($options['file_format'], $options['tags'], $options['locale_id'], $options['file']);
112+
113+
return array_merge(self::WRITE_CONFIG_DEFAULT, $options);
114+
}
73115
}

0 commit comments

Comments
 (0)