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

Skip to content

Commit 95e0bd2

Browse files
[Contracts] extract LocaleAwareInterface out of TranslatorInterface
1 parent c7fe1b6 commit 95e0bd2

File tree

19 files changed

+61
-74
lines changed

19 files changed

+61
-74
lines changed

src/Symfony/Bridge/Twig/Tests/Extension/Fixtures/StubTranslator.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,4 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
1919
{
2020
return '[trans]'.$id.'[/trans]';
2121
}
22-
23-
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
24-
{
25-
return '[trans]'.$id.'[/trans]';
26-
}
27-
28-
public function setLocale($locale)
29-
{
30-
}
31-
32-
public function getLocale()
33-
{
34-
}
3522
}

src/Symfony/Bridge/Twig/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^7.1.3",
20-
"symfony/contracts": "^1.0",
20+
"symfony/contracts": "^1.0.2",
2121
"twig/twig": "^1.35|^2.4.4"
2222
},
2323
"require-dev": {

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/DataCollectorTranslatorPassTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,4 @@ class TranslatorWithTranslatorBag implements TranslatorInterface
111111
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
112112
{
113113
}
114-
115-
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
116-
{
117-
}
118-
119-
public function setLocale($locale)
120-
{
121-
}
122-
123-
public function getLocale()
124-
{
125-
}
126114
}

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/Fixtures/StubTranslator.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,4 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
1919
{
2020
return '[trans]'.$id.'[/trans]';
2121
}
22-
23-
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
24-
{
25-
return '[trans]'.$id.'[/trans]';
26-
}
27-
28-
public function setLocale($locale)
29-
{
30-
}
31-
32-
public function getLocale()
33-
{
34-
}
3522
}

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919
"php": "^7.1.3",
2020
"ext-xml": "*",
2121
"symfony/cache": "~4.2",
22-
"symfony/dependency-injection": "^4.2",
2322
"symfony/config": "~4.2",
23+
"symfony/contracts": "^1.0.2",
24+
"symfony/dependency-injection": "^4.2",
2425
"symfony/event-dispatcher": "^4.1",
2526
"symfony/http-foundation": "^4.1.2",
2627
"symfony/http-kernel": "^4.2",

src/Symfony/Component/HttpKernel/EventListener/TranslatorListener.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
1818
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1919
use Symfony\Component\HttpKernel\KernelEvents;
20-
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
21-
use Symfony\Contracts\Translation\TranslatorInterface;
20+
use Symfony\Component\Translation\TranslatorInterface;
21+
use Symfony\Contracts\Translation\LocaleAwareInterface;
2222

2323
/**
2424
* Synchronizes the locale between the request and the translator.
@@ -31,12 +31,12 @@ class TranslatorListener implements EventSubscriberInterface
3131
private $requestStack;
3232

3333
/**
34-
* @param TranslatorInterface $translator
34+
* @param LocaleAwareInterface $translator
3535
*/
3636
public function __construct($translator, RequestStack $requestStack)
3737
{
38-
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
39-
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
38+
if (!$translator instanceof TranslatorInterface && !$translator instanceof LocaleAwareInterface) {
39+
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, LocaleAwareInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
4040
}
4141
$this->translator = $translator;
4242
$this->requestStack = $requestStack;

src/Symfony/Component/HttpKernel/Tests/EventListener/TranslatorListenerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1818
use Symfony\Component\HttpKernel\EventListener\TranslatorListener;
1919
use Symfony\Component\HttpKernel\HttpKernelInterface;
20-
use Symfony\Contracts\Translation\TranslatorInterface;
20+
use Symfony\Contracts\Translation\LocaleAwareInterface;
2121

2222
class TranslatorListenerTest extends TestCase
2323
{
@@ -27,7 +27,7 @@ class TranslatorListenerTest extends TestCase
2727

2828
protected function setUp()
2929
{
30-
$this->translator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
30+
$this->translator = $this->getMockBuilder(LocaleAwareInterface::class)->getMock();
3131
$this->requestStack = $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack')->getMock();
3232
$this->listener = new TranslatorListener($this->translator, $this->requestStack);
3333
}

src/Symfony/Component/HttpKernel/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^7.1.3",
20-
"symfony/contracts": "^1.0",
20+
"symfony/contracts": "^1.0.2",
2121
"symfony/event-dispatcher": "~4.1",
2222
"symfony/http-foundation": "^4.1.1",
2323
"symfony/debug": "~3.4|~4.0",

src/Symfony/Component/Translation/DataCollectorTranslator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Translation\Exception\InvalidArgumentException;
1515
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
16+
use Symfony\Contracts\Translation\LocaleAwareInterface;
1617
use Symfony\Contracts\Translation\TranslatorInterface;
1718

1819
/**
@@ -39,8 +40,8 @@ public function __construct($translator)
3940
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
4041
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
4142
}
42-
if (!$translator instanceof TranslatorBagInterface) {
43-
throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', \get_class($translator)));
43+
if (!$translator instanceof TranslatorBagInterface || !$translator instanceof LocaleAwareInterface) {
44+
throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.', \get_class($translator)));
4445
}
4546

4647
$this->translator = $translator;

src/Symfony/Component/Translation/LoggingTranslator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\Translation\Exception\InvalidArgumentException;
1616
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
17+
use Symfony\Contracts\Translation\LocaleAwareInterface;
1718
use Symfony\Contracts\Translation\TranslatorInterface;
1819

1920
/**
@@ -37,8 +38,8 @@ public function __construct($translator, LoggerInterface $logger)
3738
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
3839
throw new \TypeError(sprintf('Argument 1 passed to %s() must be an instance of %s, %s given.', __METHOD__, TranslatorInterface::class, \is_object($translator) ? \get_class($translator) : \gettype($translator)));
3940
}
40-
if (!$translator instanceof TranslatorBagInterface) {
41-
throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface and TranslatorBagInterface.', \get_class($translator)));
41+
if (!$translator instanceof TranslatorBagInterface || !$translator instanceof LocaleAwareInterface) {
42+
throw new InvalidArgumentException(sprintf('The Translator "%s" must implement TranslatorInterface, TranslatorBagInterface and LocaleAwareInterface.', \get_class($translator)));
4243
}
4344

4445
$this->translator = $translator;

src/Symfony/Component/Translation/TranslatorInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Translation;
1313

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

1617
/**
1718
* TranslatorInterface.
@@ -20,7 +21,7 @@
2021
*
2122
* @deprecated since Symfony 4.2, use Symfony\Contracts\Translation\TranslatorInterface instead
2223
*/
23-
interface TranslatorInterface
24+
interface TranslatorInterface extends LocaleAwareInterface
2425
{
2526
/**
2627
* Translates the given message.

src/Symfony/Component/Translation/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^7.1.3",
20-
"symfony/contracts": "^1.0",
20+
"symfony/contracts": "^1.0.2",
2121
"symfony/polyfill-mbstring": "~1.0"
2222
},
2323
"require-dev": {

src/Symfony/Component/Validator/Tests/DependencyInjection/AddValidatorInitializersPassTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\DependencyInjection\Reference;
1818
use Symfony\Component\Validator\DependencyInjection\AddValidatorInitializersPass;
1919
use Symfony\Component\Validator\Util\LegacyTranslatorProxy;
20+
use Symfony\Contracts\Translation\LocaleAwareInterface;
2021
use Symfony\Contracts\Translation\TranslatorInterface;
2122
use Symfony\Contracts\Translation\TranslatorTrait;
2223

@@ -72,7 +73,7 @@ public function testLegacyTranslatorProxy()
7273
}
7374
}
7475

75-
class TestTranslator implements TranslatorInterface
76+
class TestTranslator implements TranslatorInterface, LocaleAwareInterface
7677
{
7778
use TranslatorTrait;
7879
}

src/Symfony/Component/Validator/Util/LegacyTranslatorProxy.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Util;
1313

1414
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
15+
use Symfony\Contracts\Translation\LocaleAwareInterface;
1516
use Symfony\Contracts\Translation\TranslatorInterface;
1617

1718
/**
@@ -23,6 +24,9 @@ class LegacyTranslatorProxy implements LegacyTranslatorInterface, TranslatorInte
2324

2425
public function __construct(TranslatorInterface $translator)
2526
{
27+
if (!$translator instanceof LocaleAwareInterface) {
28+
throw new \InvalidArgumentException(sprintf('The translator passed to "%s()" must implement "%s".', __METHOD__, LocaleAwareInterface::class));
29+
}
2630
$this->translator = $translator;
2731
}
2832

src/Symfony/Component/Validator/ValidatorBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
3131
use Symfony\Component\Validator\Util\LegacyTranslatorProxy;
3232
use Symfony\Component\Validator\Validator\RecursiveValidator;
33+
use Symfony\Contracts\Translation\LocaleAwareInterface;
3334
use Symfony\Contracts\Translation\TranslatorInterface;
3435
use Symfony\Contracts\Translation\TranslatorTrait;
3536

@@ -332,7 +333,7 @@ public function getValidator()
332333
$translator = $this->translator;
333334

334335
if (null === $translator) {
335-
$translator = new class() implements TranslatorInterface {
336+
$translator = new class() implements TranslatorInterface, LocaleAwareInterface {
336337
use TranslatorTrait;
337338
};
338339
// Force the locale to be 'en' when no translator is provided rather than relying on the Intl default locale

src/Symfony/Component/Validator/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^7.1.3",
20-
"symfony/contracts": "^1.0",
20+
"symfony/contracts": "^1.0.2",
2121
"symfony/polyfill-ctype": "~1.8",
2222
"symfony/polyfill-mbstring": "~1.0"
2323
},
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Contracts\Translation;
13+
14+
interface LocaleAwareInterface
15+
{
16+
/**
17+
* Sets the current locale.
18+
*
19+
* @param string $locale The locale
20+
*
21+
* @throws \InvalidArgumentException If the locale contains invalid characters
22+
*/
23+
public function setLocale($locale);
24+
25+
/**
26+
* Returns the current locale.
27+
*
28+
* @return string The locale
29+
*/
30+
public function getLocale();
31+
}

src/Symfony/Contracts/Translation/TranslatorInterface.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,4 @@ interface TranslatorInterface
6262
* @throws \InvalidArgumentException If the locale contains invalid characters
6363
*/
6464
public function trans($id, array $parameters = array(), $domain = null, $locale = null);
65-
66-
/**
67-
* Sets the current locale.
68-
*
69-
* @param string $locale The locale
70-
*
71-
* @throws \InvalidArgumentException If the locale contains invalid characters
72-
*/
73-
public function setLocale($locale);
74-
75-
/**
76-
* Returns the current locale.
77-
*
78-
* @return string The locale
79-
*/
80-
public function getLocale();
8165
}

src/Symfony/Contracts/Translation/TranslatorTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Component\Translation\Exception\InvalidArgumentException;
1515

1616
/**
17-
* A trait to help implement TranslatorInterface.
17+
* A trait to help implement TranslatorInterface and LocaleAwareInterface.
1818
*
1919
* @author Fabien Potencier <[email protected]>
2020
*/

0 commit comments

Comments
 (0)