10000 feature #20928 [FrameworkBundle] don't load translator services if no… · symfony/symfony@8c0a41e · GitHub
[go: up one dir, main page]

Skip to content

Commit 8c0a41e

Browse files
committed
feature #20928 [FrameworkBundle] don't load translator services if not 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
2 parents 9f95654 + 1e67155 commit 8c0a41e

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
3.3.0
55
-----
66

7+
* Translation related services are not loaded anymore when the `framework.translator` option
8+
is disabled.
79
* Added `GlobalVariables::getToken()`
810

911
3.2.0

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\Common\Annotations\Reader;
1515
use Symfony\Bridge\Monolog\Processor\DebugProcessor;
1616
use Symfony\Component\Cache\Adapter\AdapterInterface;
17+
use Symfony\Component\Config\Loader\LoaderInterface;
1718
use Symfony\Component\DependencyInjection\Alias;
1819
use Symfony\Component\DependencyInjection\ChildDefinition;
1920
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -85,14 +86,22 @@ public function load(array $configs, ContainerBuilder $container)
8586
$this->annotationsConfigEnabled = $this->isConfigEnabled($container, $config['annotations']);
8687

8788
// A translator must always be registered (as support is included by
88-
// default in the Form component). If disabled, an identity translator
89-
// will be used and everything will still work as expected.
90-
if (class_exists('Symfony\Component\Translation\Translator') || $this->isConfigEnabled($container, $config['form'])) {
91-
if (!class_exists('Symfony\Component\Translation\Translator')) {
89+
// default in the Form and Validator component). If disabled, an identity
90+
// translator will be used and everything will still work as expected.
91+
if ($this->isConfigEnabled($container, $config['translator']) || $this->isConfigEnabled($container, $config['form']) || $this->isConfigEnabled($container, $config['validation'])) {
92+
if (!class_exists('Symfony\Component\Translation\Translator') && $this->isConfigEnabled($container, $config['translator'])) {
93+
throw new LogicException('Translation support cannot be enabled as the Translation component is not installed.');
94+
}
95+
96+
if (!class_exists('Symfony\Component\Translation\Translator') && $this->isConfigEnabled($container, $config['form'])) {
9297
throw new LogicException('Form support cannot be enabled as the Translation component is not installed.');
9398
}
9499

95-
$loader->load('translation.xml');
100+
if (!class_exists('Symfony\Component\Translation\Translator') && $this->isConfigEnabled($container, $config['validation'])) {
101+
throw new LogicException('Validation support cannot be enabled as the Translation component is not installed.');
102+
}
103+
104+
$loader->load('identity_translator.xml');
96105
}
97106

98107
if (isset($config['secret'])) {
@@ -165,7 +174,7 @@ public function load(array $configs, ContainerBuilder $container)
165174
$this->registerEsiConfiguration($config['esi'], $container, $loader);
166175
$this->registerSsiConfiguration($config['ssi'], $container, $loader);
167176
$this->registerFragmentsConfiguration($config['fragments'], $container, $loader);
168-
$this->registerTranslatorConfiguration($config['translator'], $container);
177+
$this->registerTranslatorConfiguration($config['translator'], $container, $loader);
169178
$this->registerProfilerConfiguration($config['profiler'], $container, $loader);
170179
$this->registerCacheConfiguration($config['cache'], $container);
171180
$this->registerWorkflowConfiguration($config['workflows'], $container, $loader);
@@ -802,15 +811,13 @@ private function createVersion(ContainerBuilder $container, $version, $format, $
802811
* @param array $config A translator configuration array
803812
* @param ContainerBuilder $container A ContainerBuilder instance
804813
*/
805-
private function registerTranslatorConfiguration(array $config, ContainerBuilder $container)
814+
private function registerTranslatorConfiguration(array $config, ContainerBuilder $container, LoaderInterface $loader)
806815
{
807816
if (!$this->isConfigEnabled($container, $config)) {
808817
return;
809818
}
810819

811-
if (!class_exists('Symfony\Component\Translation\Translator')) {
812-
throw new LogicException('Translation support cannot be enabled as the Translator component is not installed.');
813-
}
820+
$loader->load('translation.xml');
814821

815822
$this->translationConfigEnabled = true;
816823

src/Symfony/Bundle/FrameworkBundle/Resources/config/form_csrf.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<argument type="service" id="security.csrf.token_manager" />
1111
<argument>%form.type_extension.csrf.enabled%</argument>
1212
<argument>%form.type_extension.csrf.field_name%</argument>
13-
<argument type="service" id="translator.default" />
13+
<argument type="service" id="translator" />
1414
<argument>%validator.translation_domain%</argument>
1515
<argument type="service" id="form.server_params" />
1616
</service>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<services>
7+
<service id="translator" class="Symfony\Component\Translation\IdentityTranslator">
8+
<argument type="service" id="translator.selector" />
9+
</service>
10+
11+
<service id="translator.selector" class="Symfony\Component\Translation\MessageSelector" public="false" />
12+
</services>
13+
</container>

src/Symfony/Bundle/FrameworkBundle/Resources/config/translation.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@
2727
<tag name="monolog.logger" channel="translation" />
2828
</service>
2929

30-
<service id="translator" class="Symfony\Component\Translation\IdentityTranslator">
31-
<argument type="service" id="translator.selector" />
32-
</service>
33-
34-
<service id="translator.selector" class="Symfony\Component\Translation\MessageSelector" public="false" />
35-
3630
<service id="translation.loader.php" class="Symfony\Component\Translation\Loader\PhpFileLoader">
3731
<tag name="translation.loader" alias="php" />
3832
</service>

0 commit comments

Comments
 (0)
0