remove translation data collector when not usable

This commit is contained in:
Christian Flothmann 2017-02-16 21:43:11 +01:00
parent 3441b1586f
commit 303bb73971
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()
{
}
}