Skip to content

[FrameworkBundle][Translator] Make the Translator works with any PSR-11 container #22010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2017

Conversation

chalasr
Copy link
Member

@chalasr chalasr commented Mar 15, 2017

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? yes
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR n/a

Uses a service-locator for collected translation loaders and replace the single call of getParameter() by an optional constructor argument.

@@ -54,8 +55,21 @@ class Translator extends BaseTranslator implements WarmableInterface
*
* @throws InvalidArgumentException
*/
public function __construct(ContainerInterface $container, MessageSelector $selector, $loaderIds = array(), array $options = array())
public function __construct(ContainerInterface $container, MessageSelector $selector, $defaultLocale = null, $loaderIds = array(), array $options = array())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing "array" type hint for $loaderIds

$loaderIds = $defaultLocale;

if (!$container instanceof SymfonyContainerInterface) {
throw new \InvalidArgumentException(sprintf('A default locale must be passed as 3rd argument of %s() since version 3.3, the argument will be mandatory in 4.0.', __METHOD__));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest moving this before the trigger_error, and making the message simpler:
Missing third $defaultLocale argument.

@chalasr chalasr force-pushed the psr11-translator branch 2 times, most recently from 5bec128 to bd666f8 Compare March 16, 2017 08:50
@chalasr
Copy link
Member Author

chalasr commented Mar 16, 2017

@nicolas-grekas comments addressed

@chalasr chalasr force-pushed the psr11-translator branch 2 times, most recently from 7d881cc to 94073df Compare March 16, 2017 09:17
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

<argument type="service" id="translator.selector" />
<argument type="collection" /> <!-- translation loaders -->
<argument>%kernel.default_locale%</argument>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not pass the default locale as an option instead?

Copy link
Member Author

@chalasr chalasr Mar 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The argument exists in the parent constructor with getter/setter (unlike other options), I find consistent to have it here. An option would be fine too

@chalasr chalasr force-pushed the psr11-translator branch 4 times, most recently from 7faf677 to c5d9781 Compare March 16, 2017 23:43
@chalasr
Copy link
Member Author

chalasr commented Mar 16, 2017

$defaultLocale argument replaced by an option.

@nicolas-grekas
Copy link
Member

Not sure about the change: the option is not optional (it's required ;) ), which means the 4th arg is now mandatory also, but the signature tells the contrary. WDYT @fabpot ? Was that what you had in mind?

@fabpot
Copy link
Member

fabpot commented Mar 17, 2017

My point was just that we already have an $options array with 3 different keys and that adding a 4th one for the locale seemed natural to me and allowed to keep BC.

@chalasr chalasr force-pushed the psr11-translator branch 5 times, most recently from a8f4c75 to ef8c191 Compare March 17, 2017 15:09
@chalasr
Copy link
Member Author

chalasr commented Mar 17, 2017

Given it would be the only mandatory option and we would have to provide an equivalent BC layer in both cases, I changed it back to an argument.

@chalasr chalasr force-pushed the psr11-translator branch 5 times, most recently from 240639a to 58e3db1 Compare March 18, 2017 09:41
@chalasr
Copy link
Member Author

chalasr commented Mar 18, 2017

rebased

;

$translator = new Translator($container, new MessageSelector());
$container = $this->getMockBuilder('Psr\Container\ContainerInterface')->getMock();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Psr\Container\ContainerInterface::class ?

@chalasr chalasr force-pushed the psr11-translator branch 3 times, most recently from aead0cd to f9f7f3f Compare March 22, 2017 15:35
@chalasr
Copy link
Member Author

chalasr commented Mar 22, 2017

Tests green, ready.

@fabpot
Copy link
Member

fabpot commented Mar 22, 2017

Thank you @chalasr.

@fabpot fabpot merged commit 85177a6 into symfony:master Mar 22, 2017
fabpot added a commit that referenced this pull request Mar 22, 2017
…s with any PSR-11 container (chalasr)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[FrameworkBundle][Translator] Make the Translator works with any PSR-11 container

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Uses a service-locator for collected translation loaders and replace the single call of `getParameter()` by an optional constructor argument.

Commits
-------

85177a6 [FrameworkBundle] Make Translator works with any PSR-11 container
@chalasr chalasr deleted the psr11-translator branch March 22, 2017 16:33
@fabpot fabpot mentioned this pull request May 1, 2017
nicolas-grekas added a commit that referenced this pull request May 21, 2017
…sses (chalasr)

This PR was merged into the 4.0-dev branch.

Discussion
----------

Remove deprecated container injections and compiler passes

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | ~~#21451 #21625 #21284 #22010~~ #22805
| License       | MIT
| Doc PR        | n/a

Commits
-------

16a2fcf Remove deprecated container injections and compiler passes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants