8000 Improve DX in Remotes Storages · symfony/symfony@aa50e05 · GitHub
[go: up one dir, main page]

Skip to content

Commit aa50e05

Browse files
committed
Improve DX in Remotes Storages
1 parent 9a230fd commit aa50e05

File tree

5 files changed

+49
-28
lines changed

5 files changed

+49
-28
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/translation_remotes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
->args([
2222
service('http_client')->ignoreOnInvalid(),
2323
service('translation.loader.xliff_raw'),
24+
service('logger')->nullOnInvalid(),
2425
param('kernel.default_locale'),
2526
])
2627
->abstract()

src/Symfony/Component/Translation/Bridge/Loco/LocoRemote.php

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

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

14+
use Psr\Log\LoggerInterface;
1415
use Symfony\Component\HttpFoundation\Response;
1516
use Symfony\Component\Translation\Exception\TransportException;
1617
use Symfony\Component\Translation\Loader\LoaderInterface;
@@ -33,14 +34,23 @@ class LocoRemote extends AbstractRemote
3334
{
3435
protected const HOST = 'localise.biz';
3536

37+
/** @var string */
3638
private $apiKey;
39+
40+
/** @var LoaderInterface|null */
3741
private $loader;
42+
43+
/** @var LoggerInterface|null */
44+
private $logger;
45+
46+
/** @var string|null */
3847
private $defaultLocale;
3948

40-
public function __construct(string $apiKey, HttpClientInterface $client = null, LoaderInterface $loader = null, string $defaultLocale = null)
49+
public function __construct(string $apiKey, HttpClientInterface $client = null, LoaderInterface $loader = null, LoggerInterface $logger = null, string $defaultLocale = null)
4150
{
4251
$this->apiKey = $apiKey;
4352
$this->loader = $loader;
53+
$this->logger = $logger;
4454
$this->defaultLocale = $defaultLocale;
4555

4656
parent::__construct($client);
@@ -84,9 +94,7 @@ public function read(array $domains, array $locales): TranslatorBag
8494

8595
foreach ($locales as $locale) {
8696
$response = $this->client->request('GET', sprintf('https://%s/api/export/locale/%s.xlf?filter=%s', $this->getEndpoint(), $locale, $filter), [
87-
'headers' => [
88-
'Authorization' => 'Loco '.$this->apiKey,
89-
],
97+
'headers' => $this->getDefaultHeaders(),
9098
]);
9199

92100
$responseContent = $response->getContent(false);
@@ -110,8 +118,8 @@ public function delete(TranslatorBag $translations): void
110118
{
111119
$deletedIds = [];
112120

113-
foreach ($translations->all() as $locale => $messages) {
114-
foreach ($messages as $domain => $messages) {
121+
foreach ($translations->all() as $locale => $domainMessages) {
122+
foreach ($domainMessages as $domain => $messages) {
115123
foreach ($messages as $id => $message) {
116124
if (\in_array($id, $deletedIds)) {
117125
continue;
@@ -125,12 +133,17 @@ public function delete(TranslatorBag $translations): void
125133
}
126134
}
127135

136+
protected function getDefaultHeaders(): array
137+
{
138+
return [
139+
'Authorization' => 'Loco '.$this->apiKey,
140+
];
141+
}
142+
128143
private function createAsset(string $id): void
129144
{
130145
$response = $this->client->request('POST', sprintf('https://%s/api/assets', $this->getEndpoint()), [
131-
'headers' => [
132-
'Authorization' => 'Loco '.$this->apiKey,
133-
],
146+
'headers' => $this->getDefaultHeaders(),
134147
'body' => [
135148
'name' => $id,
136149
'id' => $id,
@@ -140,7 +153,9 @@ private function createAsset(string $id): void
140153
]);
141154

142155
if (Response::HTTP_CONFLICT === $response->getStatusCode()) {
143-
// Translation key already exists in Loco, do nothing
156+
$this->logger->warning(sprintf('Translation key (%s) already exists in Loco.', $id), [
157+
'id' => $id,
158+
]);
144159
} elseif (Response::HTTP_CREATED !== $response->getStatusCode()) {
145160
throw new TransportException(sprintf('Unable to add new translation key (%s) to Loco: (status code: "%s") "%s".', $id, $response->getStatusCode(), $response->getContent(false)), $response);
146161
}
@@ -149,9 +164,7 @@ private function createAsset(string $id): void
149164
private function translateAsset(string $id, string $message, string $locale): void
150165
{
151166
$response = $this->client->request('POST', sprintf('https://%s/api/translations/%s/%s', $this->getEndpoint(), $id, $locale), [
152-
'headers' => [
153-
'Authorization' => 'Loco '.$this->apiKey,
154-
],
167+
'headers' => $this->getDefaultHeaders(),
155168
'body' => $message,
156169
]);
157170

@@ -169,9 +182,7 @@ private function tagsAssets(array $ids, string $tag): void
169182
}
170183

171184
$response = $this->client->request('POST', sprintf('https://%s/api/tags/%s.json', $this->getEndpoint(), $tag), [
172-
'headers' => [
173-
'Authorization' => 'Loco '.$this->apiKey,
174-
],
185+
'headers' => $this->getDefaultHeaders(),
175186
'body' => $idsAsString,
176187
]);
177188

@@ -182,10 +193,8 @@ private function tagsAssets(array $ids, string $tag): void
182193

183194
private function createTag(string $tag): void
184195
{
185-
$response = $this->client->request('POST', sprintf('https://%s/api/tags.json', $this->getEndpoint(), $tag), [
186-
'headers' => [
187-
'Authorization' => 'Loco '.$this->apiKey,
188-
],
196+
$response = $this->client->request('POST', sprintf('https://%s/api/tags.json', $this->getEndpoint()), [
197+
'headers' => $this->getDefaultHeaders(),
189198
'body' => [
190199
'name' => $tag,
191200
],
@@ -199,9 +208,7 @@ private function createTag(string $tag): void
199208
private function getTags(): array
200209
{
201210
$response = $this->client->request('GET', sprintf('https://%s/api/tags.json', $this->getEndpoint()), [
202-
'headers' => [
203-
'Authorization' => 'Loco '.$this->apiKey,
204-
],
211+
'headers' => $this->getDefaultHeaders(),
205212
]);
206213

207214
$content = $response->getContent(false);
@@ -216,9 +223,7 @@ private function getTags(): array
216223
private function deleteAsset(string $id): void
217224
{
218225
$response = $this->client->request('DELETE', sprintf('https://%s/api/assets/%s.json', $this->getEndpoint(), $id), [
219-
'headers' => [
220-
'Authorization' => 'Loco '.$this->apiKey,
221-
],
226+
'headers' => $this->getDefaultHeaders(),
222227
]);
223228

224229
if (Response::HTTP_OK !== $response->getStatusCode()) {

src/Symfony/Component/Translation/Bridge/Loco/LocoRemoteFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function create(Dsn $dsn): RemoteInterface
2929
$port = $dsn->getPort();
3030

3131
if ('loco' === $scheme) {
32-
return (new LocoRemote($apiKey, $this->client, $this->loader, $this->defaultLocale))
32+
return (new LocoRemote($apiKey, $this->client, $this->logger, $this->loader, $this->defaultLocale))
3333
->setHost($host)
3434
->setPort($port)
3535
;

src/Symfony/Component/Translation/Remote/AbstractRemote.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ protected function getDefaultHost(): string
6464
{
6565
return static::HOST;
6666
}
67+
68+
protected function getDefaultHeaders(): array
69+
{
70+
return [];
71+
}
6772
}

src/Symfony/Component/Translation/Remote/AbstractRemoteFactory.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,30 @@
1111

1212
namespace Symfony\Component\Translation\Remote;
1313

14+
use Psr\Log\LoggerInterface;
1415
use Symfony\Component\Translation\Exception\IncompleteDsnException;
1516
use Symfony\Component\Translation\Loader\LoaderInterface;
1617
use Symfony\Contracts\HttpClient\HttpClientInterface;
1718

1819
abstract class AbstractRemoteFactory implements RemoteFactoryInterface
1920
{
21+
/** @var HttpClientInterface|null */
2022
protected $client;
23+
24+
/** @var LoaderInterface|null */
2125
protected $loader;
26+
27+
/** @var LoggerInterface|null */
28+
protected $logger;
29+
30+
/** @var string|null */
2231
protected $defaultLocale;
2332

24-
public function __construct(HttpClientInterface $client = null, LoaderInterface $loader = null, string $defaultLocale = null)
33+
public function __construct(HttpClientInterface $client = null, LoaderInterface $loader = null, LoggerInterface $logger = null, string $defaultLocale = null)
2534
{
2635
$this->client = $client;
2736
$this->loader = $loader;
37+
$this->logger = $logger;
2838
$this->defaultLocale = $defaultLocale;
2939
}
3040

0 commit comments

Comments
 (0)
0