Skip to content

[FrameworkBundle] don't load translator services if not required #20928

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
Dec 16, 2016

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented Dec 14, 2016

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

One step further could be to remove all the loader services (or not register them at all) if only the identity translator is used (i.e. when only forms are enabled, but not translations), but that could be seen as a BC break.

@@ -87,7 +87,7 @@ public function load(array $configs, ContainerBuilder $container)
// A translator must always be registered (as support is included by
// default in the Form component). If disabled, an identity translator
// will be used and everything will still work as expected.
if (class_exists('Symfony\Component\Translation\Translator') || $this->isConfigEnabled($container, $config['form'])) {
if ($this->isConfigEnabled($container, $config['translator']) || $this->isConfigEnabled($container, $config['form'])) {
if (!class_exists('Symfony\Component\Translation\Translator')) {
throw new LogicException('Form support cannot be enabled as the Translation component is not installed.');
Copy link
Member

Choose a reason for hiding this comment

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

The error message should probably be different for the first case.

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, updated it.

@@ -87,8 +87,12 @@ public function load(array $configs, ContainerBuilder $container)
// A translator must always be registered (as support is included by
// default in the Form component). If disabled, an identity translator
// will be used and everything will still work as expected.
if (class_exists('Symfony\Component\Translation\Translator') || $this->isConfigEnabled($container, $config['form'])) {
if (!class_exists('Symfony\Component\Translation\Translator')) {
if ($this->isConfigEnabled($container, $config['translator']) || $this->isConfigEnabled($container, $config['form'])) {
Copy link
Member

Choose a reason for hiding this comment

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

What about remove this condition then?

Copy link
Member Author

Choose a reason for hiding this comment

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

We still need it to not always load the translation.xml file (in case both the form and the translator section are not enabled).

Copy link
Member

Choose a reason for hiding this comment

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

ah right, Github did not show the whole if code, so I as confused.

@fabpot
Copy link
Member

fabpot commented Dec 15, 2016

I would indeed go one step further and only register the identity if translator is not enabled explicitly. We should then document this change in the CHANGELOG, but I think we should avoid as much as possible auto-registration like we have now. This will be even more important with Symfony Flex as we don't require symfony/symfony and ask users to explicitly register anything they want to use.

@xabbuh xabbuh force-pushed the issue-20791 branch 2 times, most recently from 8711b67 to 1f86628 Compare December 15, 2016 12:58
@xabbuh
Copy link
Member Author

xabbuh commented Dec 15, 2016

Fair enough, I included that change too.

@BPScott
Copy link
Contributor

BPScott commented Dec 16, 2016

Woo, thank you @xabbuh! The identity translation stuff seemed a little fiddly and I'm totally unfamiliar with it so I didn't dare open a PR myself :)

A small review: I spy the class already has formConfigEnabled and translationConfigEnabled properties that are set elsewhere, could the translation activation checks be done a little later in this file after those values are set to avoid repeated calls to isConfigEnabled()? It might also be adding an extra level of nesting to the if structuring to avoid multiple calls to class_exists('Symfony\Component\Translation\Translator') too. Something like:

if ($this->isConfigEnabled($container, $config['translator']) || $this->isConfigEnabled($container, $config['form']) || $this->isConfigEnabled($container, $config['validation'])) {
    if (!class_exists('Symfony\Component\Translation\Translator') {
        if ($this->isConfigEnabled($container, $config['translator'])) {
            throw new LogicException('Translation support cannot be enabled as the Translation component is not installed.');
        }
        if ($this->isConfigEnabled($container, $config['form'])) {
            throw new LogicException('Form support cannot be enabled as the Translation component is not installed.');
        }
        if ($this->isConfigEnabled($container, $config['validation'])) {
            throw new LogicException('Validator support cannot be enabled as the Translation component is not installed.');
        }
    }

    $loader->load('identity_translator.xml');
}

@xabbuh
Copy link
Member Author

xabbuh commented Dec 16, 2016

@BPScott I don't think that's necessary as this code is only executed during compilation.

@linaori
Copy link
Contributor

linaori commented Dec 16, 2016

@xabbuh could this cause issues related to: #20949 ?

@xabbuh
Copy link
Member Author

xabbuh commented Dec 16, 2016

@iltar This would solve #20949. Well, at least it will provide a more meaningful error message.

Copy link
Contributor

@aitboudad aitboudad left a comment

Choose a reason for hiding this comment

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

LGTM

@fabpot
Copy link
Member

fabpot commented Dec 16, 2016

Thank you @xabbuh.

@fabpot fabpot merged commit 1e67155 into symfony:master Dec 16, 2016
fabpot added a commit that referenced this pull request Dec 16, 2016
…t required (xabbuh)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[FrameworkBundle] don't load translator services if not required

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

One step further could be to remove all the loader services (or not register them at all) if only the identity translator is used (i.e. when only forms are enabled, but not translations), but that could be seen as a BC break.

Commits
-------

1e67155 don't load translator services if not required
@xabbuh xabbuh deleted the issue-20791 branch December 16, 2016 16:38
@inelgnu
Copy link
Contributor

inelgnu commented Jan 21, 2017

@xabbuh "templating.helper.translator" service has a dependency on translator and will not work if templating engine is set to "php" (see templating_php.xml)
cc @fabpot @aitboudad

@aitboudad
Copy link
Contributor

@inalgnu can you provide a PR that fixes the issue ?

@inelgnu
Copy link
Contributor

inelgnu commented Jan 22, 2017

@aitboudad done here #21374

@xabbuh
Copy link
Member Author

xabbuh commented Mar 15, 2017

see #22006

fabpot added a commit that referenced this pull request Mar 17, 2017
…s disabled (xabbuh)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[FrameworkBundle] remove translator helper if Translator is disabled

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20928 (comment), #21374
| License       | MIT
| Doc PR        |

Commits
-------

25ea510 remove translator helper if Translator is disabled
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this pull request Mar 17, 2017
…s disabled (xabbuh)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[FrameworkBundle] remove translator helper if Translator is disabled

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | symfony/symfony#20928 (comment), #21374
| License       | MIT
| Doc PR        |

Commits
-------

25ea510ba4 remove translator helper if Translator is disabled
@fabpot fabpot mentioned this pull request May 1, 2017
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.

7 participants