diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/DataCollectorTranslatorPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/DataCollectorTranslatorPass.php new file mode 100644 index 0000000000000..04a57a2e95727 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/DataCollectorTranslatorPass.php @@ -0,0 +1,35 @@ + + * + * 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 + */ +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'); + } + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php b/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php index 97f6122ff7bdb..01f400d7d5e44 100644 --- a/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php +++ b/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php @@ -15,6 +15,7 @@ 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 @@ public function build(ContainerBuilder $container) $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); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/DataCollectorTranslatorPassTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/DataCollectorTranslatorPassTest.php new file mode 100644 index 0000000000000..813fb9f6028d8 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/DataCollectorTranslatorPassTest.php @@ -0,0 +1,94 @@ + + * + * 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() + { + } +}