Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
bug #21637 [FrameworkBundle] remove translation data collector when n…
…ot 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
-------

303bb73 remove translation data collector when not usable
  • Loading branch information
fabpot committed Feb 17, 2017
2 parents 68d6415 + 303bb73 commit 9d2bbc6
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
@@ -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');
}
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
@@ -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()
{
}
}

0 comments on commit 9d2bbc6

Please sign in to comment.