Skip to content

Commit c78358f

Browse files
committed
minor symfony#40944 [Translation] Use proven DSN class from Notifier and rename package (OskarStark)
This PR was merged into the 5.3-dev branch. Discussion ---------- [Translation] Use proven DSN class from Notifier and rename package | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | --- This class is already in use, no need to introduce it as experimental cc `@welcoMattic` Commits ------- a7979c4 [Translation] Use proven DSN class from Notifier
2 parents 5506f2a + a7979c4 commit c78358f

File tree

14 files changed

+269
-90
lines changed

14 files changed

+269
-90
lines changed

.github/composer-config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"symfony/http-kernel": "source",
77
"symfony/messenger": "source",
88
"symfony/notifier": "source",
9+
"symfony/translation": "source",
910
"symfony/validator": "source",
1011
"*": "dist"
1112
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,7 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
13601360
$parentPackages = ['symfony/framework-bundle', 'symfony/translation', 'symfony/http-client'];
13611361

13621362
foreach ($classToServices as $class => $service) {
1363-
$package = sprintf('symfony/%s-translation', substr($service, \strlen('translation.provider_factory.')));
1363+
$package = sprintf('symfony/%s-translation-provider', substr($service, \strlen('translation.provider_factory.')));
13641364

13651365
if (!$container->hasDefinition('http_client') || !ContainerBuilder::willBeAvailable($package, $class, $parentPackages)) {
13661366
$container->removeDefinition($service);

src/Symfony/Component/Translation/Bridge/Loco/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "symfony/loco-translation",
2+
"name": "symfony/loco-translation-provider",
33
"type": "symfony-bridge",
4-
"description": "Symfony Loco Translation Bridge",
4+
"description": "Symfony Loco Translation Provider Bridge",
55
"keywords": ["loco", "translation", "provider"],
66
"homepage": "https://symfony.com",
77
"license": "MIT",

src/Symfony/Component/Translation/Bridge/Loco/phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</php>
1414

1515
<testsuites>
16-
<testsuite name="Symfony Loco Translation Bridge Test Suite">
16+
<testsuite name="Symfony Loco Translation Provider Bridge Test Suite">
1717
<directory>./Tests/</directory>
1818
</testsuite>
1919
</testsuites>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation\Exception;
13+
14+
/**
15+
* @author Oskar Stark <oskarstark@googlemail.com>
16+
*/
17+
class MissingRequiredOptionException extends IncompleteDsnException
18+
{
19+
public function __construct(string $option, string $dsn = null, ?\Throwable $previous = null)
20+
{
21+
$message = sprintf('The option "%s" is required but missing.', $option);
22+
23+
parent::__construct($message, $dsn, $previous);
24+
}
25+
}

src/Symfony/Component/Translation/Exception/UnsupportedSchemeException.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class UnsupportedSchemeException extends LogicException
1919
private const SCHEME_TO_PACKAGE_MAP = [
2020
'loco' => [
2121
'class' => Bridge\Loco\Provider\LocoProviderFactory::class,
22-
'package' => 'symfony/loco-translation',
22+
'package' => 'symfony/loco-translation-provider',
2323
],
2424
];
2525

@@ -31,14 +31,14 @@ public function __construct(Dsn $dsn, string $name = null, array $supported = []
3131
}
3232
$package = self::SCHEME_TO_PACKAGE_MAP[$provider] ?? null;
3333
if ($package && !class_exists($package['class'])) {
34-
parent::__construct(sprintf('Unable to synchronize translations via "%s" as the providers is not installed; try running "composer require %s".', $provider, $package['package']));
34+
parent::__construct(sprintf('Unable to synchronize translations via "%s" as the provider is not installed; try running "composer require %s".', $provider, $package['package']));
3535

3636
return;
3737
}
3838

3939
$message = sprintf('The "%s" scheme is not supported', $dsn->getScheme());
4040
if ($name && $supported) {
41-
$message .= sprintf('; supported schemes for translation providers "%s" are: "%s"', $name, implode('", "', $supported));
41+
$message .= sprintf('; supported schemes for translation provider "%s" are: "%s"', $name, implode('", "', $supported));
4242
}
4343

4444
parent::__construct($message.'.');

src/Symfony/Component/Translation/Provider/Dsn.php

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
namespace Symfony\Component\Translation\Provider;
1313

1414
use Symfony\Component\Translation\Exception\InvalidArgumentException;
15+
use Symfony\Component\Translation\Exception\MissingRequiredOptionException;
1516

1617
/**
17-
* @author Mathieu Santostefano <msantostefano@protonmail.com>
18-
*
19-
* @experimental in 5.3
18+
* @author Fabien Potencier <fabien@symfony.com>
19+
* @author Oskar Stark <oskarstark@googlemail.com>
2020
*/
2121
final class Dsn
2222
{
@@ -25,45 +25,33 @@ final class Dsn
2525
private $user;
2626
private $password;
2727
private $port;
28-
private $options;
2928
private $path;
30-
private $dsn;
29+
private $options;
30+
private $originalDsn;
3131

32-
public function __construct(string $scheme, string $host, ?string $user = null, ?string $password = null, ?int $port = null, array $options = [], ?string $path = null)
32+
public function __construct(string $dsn)
3333
{
34-
$this->scheme = $scheme;
35-
$this->host = $host;
36-
$this->user = $user;
37-
$this->password = $password;
38-
$this->port = $port;
39-
$this->options = $options;
40-
$this->path = $path;
41-
}
34+
$this->originalDsn = $dsn;
4235

43-
public static function fromString(string $dsn): self
44-
{
4536
if (false === $parsedDsn = parse_url($dsn)) {
4637
throw new InvalidArgumentException(sprintf('The "%s" translation provider DSN is invalid.', $dsn));
4738
}
4839

4940
if (!isset($parsedDsn['scheme'])) {
5041
throw new InvalidArgumentException(sprintf('The "%s" translation provider DSN must contain a scheme.', $dsn));
5142
}
43+
$this->scheme = $parsedDsn['scheme'];
5244

5345
if (!isset($parsedDsn['host'])) {
5446
throw new InvalidArgumentException(sprintf('The "%s" translation provider DSN must contain a host (use "default" by default).', $dsn));
5547
}
48+
$this->host = $parsedDsn['host'];
5649

57-
$user = isset($parsedDsn['user']) ? urldecode($parsedDsn['user']) : null;
58-
$password = isset($parsedDsn['pass']) ? urldecode($parsedDsn['pass']) : null;
59-
$port = $parsedDsn['port'] ?? null;
60-
$path = $parsedDsn['path'] ?? null;
61-
parse_str($parsedDsn['query'] ?? '', $query);
62-
63-
$dsnObject = new self($parsedDsn['scheme'], $parsedDsn['host'], $user, $password, $port, $query, $path);
64-
$dsnObject->dsn = $dsn;
65-
66-
return $dsnObject;
50+
$this->user = '' !== ($parsedDsn['user'] ?? '') ? urldecode($parsedDsn['user']) : null;
51+
$this->password = '' !== ($parsedDsn['pass'] ?? '') ? urldecode($parsedDsn['pass']) : null;
52+
$this->port = $parsedDsn['port'] ?? null;
53+
$this->path = $parsedDsn['path'] ?? null;
54+
parse_str($parsedDsn['query'] ?? '', $this->options);
6755
}
6856

6957
public function getScheme(): string
@@ -96,13 +84,27 @@ public function getOption(string $key, $default = null)
9684
return $this->options[$key] ?? $default;
9785
}
9886

87+
public function getRequiredOption(string $key)
88+
{
89+
if (!\array_key_exists($key, $this->options) || '' === trim($this->options[$key])) {
90+
throw new MissingRequiredOptionException($key);
91+
}
92+
93+
return $this->options[$key];
94+
}
95+
96+
public function getOptions(): array
97+
{
98+
return $this->options;
99+
}
100+
99101
public function getPath(): ?string
100102
{
101103
return $this->path;
102104
}
103105

104106
public function getOriginalDsn(): string
105107
{
106-
return $this->dsn;
108+
return $this->originalDsn;
107109
}
108110
}

src/Symfony/Component/Translation/Provider/NullProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class NullProvider implements ProviderInterface
2323
{
2424
public function __toString(): string
2525
{
26-
return NullProviderFactory::SCHEME.'://default';
26+
return 'null';
2727
}
2828

2929
public function write(TranslatorBagInterface $translatorBag, bool $override = false): void

src/Symfony/Component/Translation/Provider/NullProviderFactory.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,17 @@
2020
*/
2121
final class NullProviderFactory extends AbstractProviderFactory
2222
{
23-
const SCHEME = 'null';
24-
2523
public function create(Dsn $dsn): ProviderInterface
2624
{
27-
if (self::SCHEME === $dsn->getScheme()) {
25+
if ('null' === $dsn->getScheme()) {
2826
return new NullProvider();
2927
}
3028

31-
throw new UnsupportedSchemeException($dsn, self::SCHEME, $this->getSupportedSchemes());
29+
throw new UnsupportedSchemeException($dsn, 'null', $this->getSupportedSchemes());
3230
}
3331

3432
protected function getSupportedSchemes(): array
3533
{
36-
return [self::SCHEME];
34+
return ['null'];
3735
}
3836
}

src/Symfony/Component/Translation/Provider/TranslationProviderCollectionFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function fromConfig(array $config): TranslationProviderCollection
3737
$providers = [];
3838
foreach ($config as $name => $currentConfig) {
3939
$providers[$name] = $this->fromDsnObject(
40-
Dsn::fromString($currentConfig['dsn']),
40+
new Dsn($currentConfig['dsn']),
4141
!$currentConfig['locales'] ? $this->enabledLocales : $currentConfig['locales'],
4242
!$currentConfig['domains'] ? [] : $currentConfig['domains']
4343
);

0 commit comments

Comments
 (0)