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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ CHANGELOG
3.3.0
-----

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

3.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Doctrine\Common\Annotations\Reader;
use Symfony\Bridge\Monolog\Processor\DebugProcessor;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -85,14 +86,22 @@ public function load(array $configs, ContainerBuilder $container)
$this->annotationsConfigEnabled = $this->isConfigEnabled($container, $config['annotations']);

// 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')) {
// default in the Form and Validator component). If disabled, an identity
// translator will be used and everything will still work as expected.
if ($this->isConfigEnabled($container, $config['translator']) || $this->isConfigEnabled($container, $config['form']) || $this->isConfigEnabled($container, $config['validation'])) {
if (!class_exists('Symfony\Component\Translation\Translator') && $this->isConfigEnabled($container, $config['translator'])) {
throw new LogicException('Translation support cannot be enabled as the Translation component is not installed.');
}

if (!class_exists('Symfony\Component\Translation\Translator') && $this->isConfigEnabled($container, $config['form'])) {
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.

}

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

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

if (isset($config['secret'])) {
Expand Down Expand Up @@ -165,7 +174,7 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerEsiConfiguration($config['esi'], $container, $loader);
$this->registerSsiConfiguration($config['ssi'], $container, $loader);
$this->registerFragmentsConfiguration($config['fragments'], $container, $loader);
$this->registerTranslatorConfiguration($config['translator'], $container);
$this->registerTranslatorConfiguration($config['translator'], $container, $loader);
$this->registerProfilerConfiguration($config['profiler'], $container, $loader);
$this->registerCacheConfiguration($config['cache'], $container);
$this->registerWorkflowConfiguration($config['workflows'], $container, $loader);
Expand Down Expand Up @@ -802,15 +811,13 @@ private function createVersion(ContainerBuilder $container, $version, $format, $
* @param array $config A translator configuration array
* @param ContainerBuilder $container A ContainerBuilder instance
*/
private function registerTranslatorConfiguration(array $config, ContainerBuilder $container)
private function registerTranslatorConfiguration(array $config, ContainerBuilder $container, LoaderInterface $loader)
{
if (!$this->isConfigEnabled($container, $config)) {
return;
}

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

$this->translationConfigEnabled = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<argument type="service" id="security.csrf.token_manager" />
<argument>%form.type_extension.csrf.enabled%</argument>
<argument>%form.type_extension.csrf.field_name%</argument>
<argument type="service" id="translator.default" />
<argument type="service" id="translator" />
<argument>%validator.translation_domain%</argument>
<argument type="service" id="form.server_params" />
</service>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="translator" class="Symfony\Component\Translation\IdentityTranslator">
<argument type="service" id="translator.selector" />
</service>

<service id="translator.selector" class="Symfony\Component\Translation\MessageSelector" public="false" />
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@
<tag name="monolog.logger" channel="translation" />
</service>

<service id="translator" class="Symfony\Component\Translation\IdentityTranslator">
<argument type="service" id="translator.selector" />
</service>

<service id="translator.selector" class="Symfony\Component\Translation\MessageSelector" public="false" />

<service id="translation.loader.php" class="Symfony\Component\Translation\Loader\PhpFileLoader">
<tag name="translation.loader" alias="php" />
</service>
Expand Down