Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 303bb73

Browse files
committed
remove translation data collector when not usable
1 parent 3441b15 commit 303bb73

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
17+
/**
18+
* @author Christian Flothmann <[email protected]>
19+
*/
20+
class DataCollectorTranslatorPass implements CompilerPassInterface
21+
{
22+
public function process(ContainerBuilder $container)
23+
{
24+
if (!$container->has('translator')) {
25+
return;
26+
}
27+
28+
$translatorClass = $container->findDefinition('translator')->getClass();
29+
30+
if (!is_subclass_of($translatorClass, 'Symfony\Component\Translation\TranslatorBagInterface')) {
31+
$container->removeDefinition('translator.data_collector');
32+
$container->removeDefinition('data_collector.translation');
33+
}
34+
}
35+
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddValidatorInitializersPass;
1616
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass;
1717
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
18+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass;
1819
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass;
1920
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RoutingResolverPass;
2021
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
@@ -88,6 +89,7 @@ public function build(ContainerBuilder $container)
8889
$container->addCompilerPass(new TranslationDumperPass());
8990
$container->addCompilerPass(new FragmentRendererPass(), PassConfig::TYPE_AFTER_REMOVING);
9091
$container->addCompilerPass(new SerializerPass());
92+
$container->addCompilerPass(new DataCollectorTranslatorPass());
9193

9294
if ($container->getParameter('kernel.debug')) {
9395
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
13+
14+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
use Symfony\Component\Translation\TranslatorInterface;
18+
19+
class DataCollectorTranslatorPassTest extends \PHPUnit_Framework_TestCase
20+
{
21+
private $container;
22+
private $dataCollectorTranslatorPass;
23+
24+
protected function setUp()
25+
{
26+
$this->container = new ContainerBuilder();
27+
$this->dataCollectorTranslatorPass = new DataCollectorTranslatorPass();
28+
29+
$this->container->register('translator.data_collector', 'Symfony\Component\Translation\DataCollectorTranslator')
30+
->setPublic(false)
31+
->setDecoratedService('translator')
32+
->setArguments(array(new Reference('translator.data_collector.inner')))
33+
;
34+
35+
$this->container->register('data_collector.translation', 'Symfony\Component\Translation\DataCollector\TranslationDataCollector')
36+
->setArguments(array(new Reference('translator.data_collector')))
37+
;
38+
}
39+
40+
public function testProcessKeepsDataCollectorTranslatorIfItImplementsTranslatorBagInterface()
41+
{
42+
$this->container->register('translator', 'Symfony\Component\Translation\Translator');
43+
44+
$this->dataCollectorTranslatorPass->process($this->container);
45+
46+
$this->assertTrue($this->container->hasDefinition('translator.data_collector'));
47+
}
48+
49+
public function testProcessKeepsDataCollectorIfTranslatorImplementsTranslatorBagInterface()
50+
{
51+
$this->container->register('translator', 'Symfony\Component\Translation\Translator');
52+
53+
$this->dataCollectorTranslatorPass->process($this->container);
54+
55+
$this->assertTrue($this->container->hasDefinition('data_collector.translation'));
56+
}
57+
58+
public function testProcessRemovesDataCollectorTranslatorIfItDoesNotImplementTranslatorBagInterface()
59+
{
60+
$this->container->register('translator', 'Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\TranslatorWithTranslatorBag');
61+
62+
$this->dataCollectorTranslatorPass->process($this->container);
63+
64+
$this->assertFalse($this->container->hasDefinition('translator.data_collector'));
65+
}
66+
67+
public function testProcessRemovesDataCollectorIfTranslatorDoesNotImplementTranslatorBagInterface()
68+
{
69+
$this->container->register('translator', 'Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\TranslatorWithTranslatorBag');
70+
71+
$this->dataCollectorTranslatorPass->process($this->container);
72+
73+
$this->assertFalse($this->container->hasDefinition('data_collector.translation'));
74+
}
75+
}
76+
77+
class TranslatorWithTranslatorBag implements TranslatorInterface
78+
{
79+
public function trans($id, array $parameters = array(), $domain = null, $locale = null)
80+
{
81+
}
82+
83+
public function transChoice($id, $number, array $parameters = array(), $domain = null, $locale = null)
84+
{
85+
}
86+
87+
public function setLocale($locale)
88+
{
89+
}
90+
91+
public function getLocale()
92+
{
93+
}
94+
}

0 commit comments

Comments
 (0)