bug #21637 [FrameworkBundle] remove translation data collector when not usable (xabbuh)

This PR was merged into the 2.7 branch.

Discussion
----------

[FrameworkBundle] remove translation data collector when not usable

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

Commits
-------

303bb73971 remove translation data collector when not usable
This commit is contained in:
Fabien Potencier 2017-02-17 08:12:20 -08:00
commit 9d2bbc6dbe
3 changed files with 131 additions and 0 deletions

View File

@ -0,0 +1,35 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* @author Christian Flothmann <christian.flothmann@sensiolabs.de>
*/
class DataCollectorTranslatorPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->has('translator')) {
return;
}
$translatorClass = $container->findDefinition('translator')->getClass();
if (!is_subclass_of($translatorClass, 'Symfony\Component\Translation\TranslatorBagInterface')) {
$container->removeDefinition('translator.data_collector');
$container->removeDefinition('data_collector.translation');
}
}
}

View File

@ -15,6 +15,7 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintVal
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddValidatorInitializersPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RoutingResolverPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
@ -88,6 +89,7 @@ class FrameworkBundle extends Bundle
$container->addCompilerPass(new TranslationDumperPass());
$container->addCompilerPass(new FragmentRendererPass(), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new SerializerPass());
$container->addCompilerPass(new DataCollectorTranslatorPass());
if ($container->getParameter('kernel.debug')) {
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);

View File

@ -0,0 +1,94 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Translation\TranslatorInterface;
class DataCollectorTranslatorPassTest extends \PHPUnit_Framework_TestCase
{
private $container;
private $dataCollectorTranslatorPass;
protected function setUp()
{
$this->container = new ContainerBuilder();
$this->dataCollectorTranslatorPass = new DataCollectorTranslatorPass();
$this->container->register('translator.data_collector', 'Symfony\Component\Translation\DataCollectorTranslator')
->setPublic(false)
->setDecoratedService('translator')
->setArguments(array(new Reference('translator.data_collector.inner')))
;
$this->container->register('data_collector.translation', 'Symfony\Component\Translation\DataCollector\TranslationDataCollector')
->setArguments(array(new Reference('translator.data_collector')))
;
}
public function testProcessKeepsDataCollectorTranslatorIfItImplementsTranslatorBagInterface()
{
$this->container->register('translator', 'Symfony\Component\Translation\Translator');
$this->dataCollectorTranslatorPass->process($this->container);
$this->assertTrue($this->container->hasDefinition('translator.data_collector'));
}
public function testProcessKeepsDataCollectorIfTranslatorImplementsTranslatorBagInterface()
{
$this->container->register('translator', 'Symfony\Component\Translation\Translator');
$this->dataCollectorTranslatorPass->process($this->container);
$this->assertTrue($this->container->hasDefinition('data_collector.translation'));
}
public function testProcessRemovesDataCollectorTranslatorIfItDoesNotImplementTranslatorBagInterface()
{
$this->container->register('translator', 'Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\TranslatorWithTranslatorBag');
$this->dataCollectorTranslatorPass->process($this->container);
$this->assertFalse($this->container->hasDefinition('translator.data_collector'));
}
public function testProcessRemovesDataCollectorIfTranslatorDoesNotImplementTranslatorBagInterface()
{
$this->container->register('translator', 'Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\TranslatorWithTranslatorBag');
$this->dataCollectorTranslatorPass->process($this->container);
$this->assertFalse($this->container->hasDefinition('data_collector.translation'));
}
}
class TranslatorWithTranslatorBag implements TranslatorInterface
{
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
{
}
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
{
}
public function setLocale($locale)
{
}
public function getLocale()
{
}
}