From 74c951fa042c279828ea19c0fea593eba8885d8a Mon Sep 17 00:00:00 2001 From: Thierry Thuon Date: Fri, 5 May 2017 12:35:04 +0200 Subject: [PATCH] [FrameworkBundle][Translation] Move translation compiler pass --- UPGRADE-3.4.md | 12 +++ UPGRADE-4.0.md | 12 +++ .../Bundle/FrameworkBundle/CHANGELOG.md | 6 ++ .../Compiler/TranslationDumperPass.php | 22 ++--- .../Compiler/TranslationExtractorPass.php | 29 ++---- .../Compiler/TranslatorPass.php | 45 ++------- .../FrameworkBundle/FrameworkBundle.php | 12 +-- .../Compiler/TranslatorPassTest.php | 3 + .../FrameworkExtensionTest.php | 2 +- .../Bundle/FrameworkBundle/composer.json | 4 +- .../Component/Translation/CHANGELOG.md | 7 ++ .../TranslationDumperPass.php | 44 +++++++++ .../TranslationExtractorPass.php | 49 +++++++++ .../DependencyInjection/TranslatorPass.php | 63 ++++++++++++ .../TranslationDumperPassTest.php | 66 +++++++++++++ .../TranslationExtractorPassTest.php | 99 +++++++++++++++++++ .../TranslationPassTest.php | 50 ++++++++++ .../Component/Translation/composer.json | 2 + 18 files changed, 444 insertions(+), 83 deletions(-) create mode 100644 src/Symfony/Component/Translation/DependencyInjection/TranslationDumperPass.php create mode 100644 src/Symfony/Component/Translation/DependencyInjection/TranslationExtractorPass.php create mode 100644 src/Symfony/Component/Translation/DependencyInjection/TranslatorPass.php create mode 100644 src/Symfony/Component/Translation/Tests/DependencyInjection/TranslationDumperPassTest.php create mode 100644 src/Symfony/Component/Translation/Tests/DependencyInjection/TranslationExtractorPassTest.php create mode 100644 src/Symfony/Component/Translation/Tests/DependencyInjection/TranslationPassTest.php diff --git a/UPGRADE-3.4.md b/UPGRADE-3.4.md index a609da081c..cc015a7f3f 100644 --- a/UPGRADE-3.4.md +++ b/UPGRADE-3.4.md @@ -38,6 +38,18 @@ FrameworkBundle will be removed in 4.0. Use the `--prefix` option with an empty string as value instead (e.g. `--prefix=""`) + * The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationDumperPass` + class has been deprecated and will be removed in 4.0. Use the + `Symfony\Component\Translation\DependencyInjection\TranslationDumperPass` class instead. + + * The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationExtractorPass` + class has been deprecated and will be removed in 4.0. Use the + `Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass` class instead. + + * The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass` + class has been deprecated and will be removed in 4.0. Use the + `Symfony\Component\Translation\DependencyInjection\TranslatorPass` class instead. + Process ------- diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index b1027dc026..79ed0fad77 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -348,6 +348,18 @@ FrameworkBundle * The `--no-prefix` option of the `translation:update` command has been removed. + * The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationDumperPass` + class has been removed. Use the + `Symfony\Component\Translation\DependencyInjection\TranslationDumperPass` class instead. + + * The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationExtractorPass` + class has been removed. Use the + `Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass` class instead. + + * The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass` + class has been removed. Use the + `Symfony\Component\Translation\DependencyInjection\TranslatorPass` class instead. + HttpFoundation -------------- diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index c089a8b40a..22fc00ade0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -10,6 +10,12 @@ CHANGELOG require symfony/stopwatch` in your `dev` environment. * Deprecated using the `KERNEL_DIR` environment variable with `KernelTestCase::getKernelClass()`. * Deprecated the `KernelTestCase::getPhpUnitXmlDir()` and `KernelTestCase::getPhpUnitCliConfigArgument()` methods. + * Deprecated `TranslationDumperPass`, use + `Symfony\Component\Translation\DependencyInjection\TranslationDumperPass` instead + * Deprecated `TranslationExtractorPass`, use + `Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass` instead + * Deprecated `TranslatorPass`, use + `Symfony\Component\Translation\DependencyInjection\TranslatorPass` instead 3.3.0 ----- diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslationDumperPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslationDumperPass.php index 541b06b31e..38e52e8cd9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslationDumperPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslationDumperPass.php @@ -11,25 +11,15 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; -use Symfony\Component\DependencyInjection\Reference; -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +@trigger_error(sprintf('The %s class is deprecated since version 3.4 and will be removed in 4.0. Use Symfony\Component\Translation\DependencyInjection\TranslationDumperPass instead.', TranslationDumperPass::class), E_USER_DEPRECATED); + +use Symfony\Component\Translation\DependencyInjection\TranslationDumperPass as BaseTranslationDumperPass; /** * Adds tagged translation.formatter services to translation writer. + * + * @deprecated since version 3.4, to be removed in 4.0. Use {@link BaseTranslationDumperPass instead}. */ -class TranslationDumperPass implements CompilerPassInterface +class TranslationDumperPass extends BaseTranslationDumperPass { - public function process(ContainerBuilder $container) - { - if (!$container->hasDefinition('translation.writer')) { - return; - } - - $definition = $container->getDefinition('translation.writer'); - - foreach ($container->findTaggedServiceIds('translation.dumper', true) as $id => $attributes) { - $definition->addMethodCall('addDumper', array($attributes[0]['alias'], new Reference($id))); - } - } } diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslationExtractorPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslationExtractorPass.php index 4dc8d3985c..985809eeb1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslationExtractorPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslationExtractorPass.php @@ -11,30 +11,15 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; -use Symfony\Component\DependencyInjection\Reference; -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; -use Symfony\Component\DependencyInjection\Exception\RuntimeException; +@trigger_error(sprintf('The %s class is deprecated since version 3.4 and will be removed in 4.0. Use Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass instead.', TranslationExtractorPass::class), E_USER_DEPRECATED); + +use Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass as BaseTranslationExtractorPass; /** - * Adds tagged translation.extractor services to translation extractor. + * Adds tagged translation.formatter services to translation writer. + * + * @deprecated since version 3.4, to be removed in 4.0. Use {@link BaseTranslationDumperPass instead}. */ -class TranslationExtractorPass implements CompilerPassInterface +class TranslationExtractorPass extends BaseTranslationExtractorPass { - public function process(ContainerBuilder $container) - { - if (!$container->hasDefinition('translation.extractor')) { - return; - } - - $definition = $container->getDefinition('translation.extractor'); - - foreach ($container->findTaggedServiceIds('translation.extractor', true) as $id => $attributes) { - if (!isset($attributes[0]['alias'])) { - throw new RuntimeException(sprintf('The alias for the tag "translation.extractor" of service "%s" must be set.', $id)); - } - - $definition->addMethodCall('addExtractor', array($attributes[0]['alias'], new Reference($id))); - } - } } diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslatorPass.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslatorPass.php index 29e251f274..7bba462937 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslatorPass.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TranslatorPass.php @@ -11,42 +11,15 @@ namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler; -use Symfony\Component\DependencyInjection\Reference; -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; -use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; +@trigger_error(sprintf('The %s class is deprecated since version 3.4 and will be removed in 4.0. Use Symfony\Component\Translation\DependencyInjection\TranslatorPass instead.', TranslatorPass::class), E_USER_DEPRECATED); -class TranslatorPass implements CompilerPassInterface +use Symfony\Component\Translation\DependencyInjection\TranslatorPass as BaseTranslatorPass; + +/** + * Adds tagged translation.formatter services to translation writer. + * + * @deprecated since version 3.4, to be removed in 4.0. Use {@link BaseTranslatorPass instead}. + */ +class TranslatorPass extends BaseTranslatorPass { - public function process(ContainerBuilder $container) - { - if (!$container->hasDefinition('translator.default')) { - return; - } - - $loaders = array(); - $loaderRefs = array(); - foreach ($container->findTaggedServiceIds('translation.loader', true) as $id => $attributes) { - $loaderRefs[$id] = new Reference($id); - $loaders[$id][] = $attributes[0]['alias']; - if (isset($attributes[0]['legacy-alias'])) { - $loaders[$id][] = $attributes[0]['legacy-alias']; - } - } - - if ($container->hasDefinition('translation.loader')) { - $definition = $container->getDefinition('translation.loader'); - foreach ($loaders as $id => $formats) { - foreach ($formats as $format) { - $definition->addMethodCall('addLoader', array($format, $loaderRefs[$id])); - } - } - } - - $container - ->findDefinition('translator.default') - ->replaceArgument(0, ServiceLocatorTagPass::register($container, $loaderRefs)) - ->replaceArgument(3, $loaders) - ; - } } diff --git a/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php b/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php index 99ab06b337..a9290c697c 100644 --- a/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php +++ b/src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php @@ -19,14 +19,11 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolClearer use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass; -use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheClearerPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddExpressionLanguageProvidersPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ContainerBuilderDebugDumpPass; -use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationExtractorPass; -use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslationDumperPass; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass; use Symfony\Component\Config\DependencyInjection\ConfigCachePass; use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass; @@ -45,6 +42,9 @@ use Symfony\Component\Form\DependencyInjection\FormPass; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\Config\Resource\ClassExistenceResource; +use Symfony\Component\Translation\DependencyInjection\TranslationDumperPass; +use Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass; +use Symfony\Component\Translation\DependencyInjection\TranslatorPass; use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass; use Symfony\Component\Validator\DependencyInjection\AddValidatorInitializersPass; use Symfony\Component\Workflow\DependencyInjection\ValidateWorkflowsPass; @@ -93,13 +93,13 @@ class FrameworkBundle extends Bundle $container->addCompilerPass(new AddAnnotationsCachedReaderPass(), PassConfig::TYPE_BEFORE_REMOVING); $this->addCompilerPassIfExists($container, AddValidatorInitializersPass::class); $this->addCompilerPassIfExists($container, AddConsoleCommandPass::class); - $container->addCompilerPass(new TranslatorPass()); + $this->addCompilerPassIfExists($container, TranslatorPass::class); $container->addCompilerPass(new LoggingTranslatorPass()); $container->addCompilerPass(new AddCacheWarmerPass()); $container->addCompilerPass(new AddCacheClearerPass()); $container->addCompilerPass(new AddExpressionLanguageProvidersPass()); - $container->addCompilerPass(new TranslationExtractorPass()); - $container->addCompilerPass(new TranslationDumperPass()); + $this->addCompilerPassIfExists($container, TranslationExtractorPass::class); + $this->addCompilerPassIfExists($container, TranslationDumperPass::class); $container->addCompilerPass(new FragmentRendererPass(), PassConfig::TYPE_AFTER_REMOVING); $this->addCompilerPassIfExists($container, SerializerPass::class); $this->addCompilerPassIfExists($container, PropertyInfoPass::class); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/TranslatorPassTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/TranslatorPassTest.php index a92f734ecc..d31fb18a12 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/TranslatorPassTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/TranslatorPassTest.php @@ -18,6 +18,9 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; +/** + * @group legacy + */ class TranslatorPassTest extends TestCase { public function testValidCollector() diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index 4f86c85d4f..b7f606e874 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -12,7 +12,6 @@ namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection; use Doctrine\Common\Annotations\Annotation; -use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass; use Symfony\Bundle\FullStack; use Symfony\Bundle\FrameworkBundle\Tests\TestCase; use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddAnnotationsCachedReaderPass; @@ -41,6 +40,7 @@ use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader; use Symfony\Component\Serializer\Normalizer\DataUriNormalizer; use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer; +use Symfony\Component\Translation\DependencyInjection\TranslatorPass; use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass; abstract class FrameworkExtensionTest extends TestCase diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index 9699ae85d6..940a49a4de 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -47,7 +47,7 @@ "symfony/security-csrf": "~2.8|~3.0|~4.0", "symfony/serializer": "~3.3|~4.0", "symfony/stopwatch": "~2.8|~3.0|~4.0", - "symfony/translation": "~3.2|~4.0", + "symfony/translation": "~3.4|~4.0", "symfony/templating": "~2.8|~3.0|~4.0", "symfony/validator": "~3.4|~4.0", "symfony/var-dumper": "~3.3|~4.0", @@ -68,7 +68,7 @@ "symfony/form": "<3.3", "symfony/property-info": "<3.3", "symfony/serializer": "<3.3", - "symfony/translation": "<3.2", + "symfony/translation": "<3.4", "symfony/validator": "<3.4", "symfony/workflow": "<3.3" }, diff --git a/src/Symfony/Component/Translation/CHANGELOG.md b/src/Symfony/Component/Translation/CHANGELOG.md index 349faceb0c..baa791b0ad 100644 --- a/src/Symfony/Component/Translation/CHANGELOG.md +++ b/src/Symfony/Component/Translation/CHANGELOG.md @@ -1,6 +1,13 @@ CHANGELOG ========= +3.4.0 +----- + + * Added `TranslationDumperPass` + * Added `TranslationExtractorPass` + * Added `TranslatorPass` + 3.2.0 ----- diff --git a/src/Symfony/Component/Translation/DependencyInjection/TranslationDumperPass.php b/src/Symfony/Component/Translation/DependencyInjection/TranslationDumperPass.php new file mode 100644 index 0000000000..1ca79cf00a --- /dev/null +++ b/src/Symfony/Component/Translation/DependencyInjection/TranslationDumperPass.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\DependencyInjection; + +use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; + +/** + * Adds tagged translation.formatter services to translation writer. + */ +class TranslationDumperPass implements CompilerPassInterface +{ + private $writerServiceId; + private $dumperTag; + + public function __construct($writerServiceId = 'translation.writer', $dumperTag = 'translation.dumper') + { + $this->writerServiceId = $writerServiceId; + $this->dumperTag = $dumperTag; + } + + public function process(ContainerBuilder $container) + { + if (!$container->hasDefinition($this->writerServiceId)) { + return; + } + + $definition = $container->getDefinition($this->writerServiceId); + + foreach ($container->findTaggedServiceIds($this->dumperTag, true) as $id => $attributes) { + $definition->addMethodCall('addDumper', array($attributes[0]['alias'], new Reference($id))); + } + } +} diff --git a/src/Symfony/Component/Translation/DependencyInjection/TranslationExtractorPass.php b/src/Symfony/Component/Translation/DependencyInjection/TranslationExtractorPass.php new file mode 100644 index 0000000000..0610518795 --- /dev/null +++ b/src/Symfony/Component/Translation/DependencyInjection/TranslationExtractorPass.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\DependencyInjection; + +use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\Exception\RuntimeException; + +/** + * Adds tagged translation.extractor services to translation extractor. + */ +class TranslationExtractorPass implements CompilerPassInterface +{ + private $extractorServiceId; + private $extractorTag; + + public function __construct($extractorServiceId = 'translation.extractor', $extractorTag = 'translation.extractor') + { + $this->extractorServiceId = $extractorServiceId; + $this->extractorTag = $extractorTag; + } + + public function process(ContainerBuilder $container) + { + if (!$container->hasDefinition($this->extractorServiceId)) { + return; + } + + $definition = $container->getDefinition($this->extractorServiceId); + + foreach ($container->findTaggedServiceIds($this->extractorTag, true) as $id => $attributes) { + if (!isset($attributes[0]['alias'])) { + throw new RuntimeException(sprintf('The alias for the tag "translation.extractor" of service "%s" must be set.', $id)); + } + + $definition->addMethodCall('addExtractor', array($attributes[0]['alias'], new Reference($id))); + } + } +} diff --git a/src/Symfony/Component/Translation/DependencyInjection/TranslatorPass.php b/src/Symfony/Component/Translation/DependencyInjection/TranslatorPass.php new file mode 100644 index 0000000000..e144f6b74e --- /dev/null +++ b/src/Symfony/Component/Translation/DependencyInjection/TranslatorPass.php @@ -0,0 +1,63 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\DependencyInjection; + +use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; + +class TranslatorPass implements CompilerPassInterface +{ + private $translatorServiceId; + private $loaderServiceId; + private $loaderTag; + + public function __construct($translatorServiceId = 'translator.default', $loaderServiceId = 'translation.loader', $loaderTag = 'translation.loader') + { + $this->translatorServiceId = $translatorServiceId; + $this->loaderServiceId = $loaderServiceId; + $this->loaderTag = $loaderTag; + } + + public function process(ContainerBuilder $container) + { + if (!$container->hasDefinition($this->translatorServiceId)) { + return; + } + + $loaders = array(); + $loaderRefs = array(); + foreach ($container->findTaggedServiceIds($this->loaderTag, true) as $id => $attributes) { + $loaderRefs[$id] = new Reference($id); + $loaders[$id][] = $attributes[0]['alias']; + if (isset($attributes[0]['legacy-alias'])) { + $loaders[$id][] = $attributes[0]['legacy-alias']; + } + } + + if ($container->hasDefinition($this->loaderServiceId)) { + $definition = $container->getDefinition($this->loaderServiceId); + foreach ($loaders as $id => $formats) { + foreach ($formats as $format) { + $definition->addMethodCall('addLoader', array($format, $loaderRefs[$id])); + } + } + } + + $container + ->findDefinition($this->translatorServiceId) + ->replaceArgument(0, ServiceLocatorTagPass::register($container, $loaderRefs)) + ->replaceArgument(3, $loaders) + ; + } +} diff --git a/src/Symfony/Component/Translation/Tests/DependencyInjection/TranslationDumperPassTest.php b/src/Symfony/Component/Translation/Tests/DependencyInjection/TranslationDumperPassTest.php new file mode 100644 index 0000000000..845200e712 --- /dev/null +++ b/src/Symfony/Component/Translation/Tests/DependencyInjection/TranslationDumperPassTest.php @@ -0,0 +1,66 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Tests\DependencyInjection; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\Translation\DependencyInjection\TranslationDumperPass; + +class TranslationDumperPassTest extends TestCase +{ + public function testProcess() + { + $definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->disableOriginalConstructor()->getMock(); + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->disableOriginalConstructor()->getMock(); + + $container->expects($this->once()) + ->method('hasDefinition') + ->with('translation.writer') + ->will($this->returnValue(true)); + + $container->expects($this->once()) + ->method('getDefinition') + ->with('translation.writer') + ->will($this->returnValue($definition)); + + $valueTaggedServiceIdsFound = array( + 'foo.id' => array( + array('alias' => 'bar.alias'), + ), + ); + $container->expects($this->once()) + ->method('findTaggedServiceIds') + ->with('translation.dumper', true) + ->will($this->returnValue($valueTaggedServiceIdsFound)); + + $definition->expects($this->once())->method('addMethodCall')->with('addDumper', array('bar.alias', new Reference('foo.id'))); + + $translationDumperPass = new TranslationDumperPass(); + $translationDumperPass->process($container); + } + + public function testProcessNoDefinitionFound() + { + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->disableOriginalConstructor()->getMock(); + + $container->expects($this->once()) + ->method('hasDefinition') + ->with('translation.writer') + ->will($this->returnValue(false)); + + $container->expects($this->never())->method('getDefinition'); + $container->expects($this->never())->method('findTaggedServiceIds'); + + $translationDumperPass = new TranslationDumperPass(); + $translationDumperPass->process($container); + } +} diff --git a/src/Symfony/Component/Translation/Tests/DependencyInjection/TranslationExtractorPassTest.php b/src/Symfony/Component/Translation/Tests/DependencyInjection/TranslationExtractorPassTest.php new file mode 100644 index 0000000000..76d2b999cf --- /dev/null +++ b/src/Symfony/Component/Translation/Tests/DependencyInjection/TranslationExtractorPassTest.php @@ -0,0 +1,99 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Tests\DependencyInjection; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass; + +class TranslationExtractorPassTest extends TestCase +{ + public function testProcess() + { + $definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->disableOriginalConstructor()->getMock(); + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->disableOriginalConstructor()->getMock(); + + $container->expects($this->once()) + ->method('hasDefinition') + ->with('translation.extractor') + ->will($this->returnValue(true)); + + $container->expects($this->once()) + ->method('getDefinition') + ->with('translation.extractor') + ->will($this->returnValue($definition)); + + $valueTaggedServiceIdsFound = array( + 'foo.id' => array( + array('alias' => 'bar.alias'), + ), + ); + $container->expects($this->once()) + ->method('findTaggedServiceIds') + ->with('translation.extractor', true) + ->will($this->returnValue($valueTaggedServiceIdsFound)); + + $definition->expects($this->once())->method('addMethodCall')->with('addExtractor', array('bar.alias', new Reference('foo.id'))); + + $translationDumperPass = new TranslationExtractorPass(); + $translationDumperPass->process($container); + } + + public function testProcessNoDefinitionFound() + { + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->disableOriginalConstructor()->getMock(); + + $container->expects($this->once()) + ->method('hasDefinition') + ->with('translation.extractor') + ->will($this->returnValue(false)); + + $container->expects($this->never())->method('getDefinition'); + $container->expects($this->never())->method('findTaggedServiceIds'); + + $translationDumperPass = new TranslationExtractorPass(); + $translationDumperPass->process($container); + } + + /** + * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException + * @expectedExceptionMessage The alias for the tag "translation.extractor" of service "foo.id" must be set. + */ + public function testProcessMissingAlias() + { + $definition = $this->getMockBuilder('Symfony\Component\DependencyInjection\Definition')->disableOriginalConstructor()->getMock(); + $container = $this->getMockBuilder('Symfony\Component\DependencyInjection\ContainerBuilder')->disableOriginalConstructor()->getMock(); + + $container->expects($this->once()) + ->method('hasDefinition') + ->with('translation.extractor') + ->will($this->returnValue(true)); + + $container->expects($this->once()) + ->method('getDefinition') + ->with('translation.extractor') + ->will($this->returnValue($definition)); + + $valueTaggedServiceIdsFound = array( + 'foo.id' => array(), + ); + $container->expects($this->once()) + ->method('findTaggedServiceIds') + ->with('translation.extractor', true) + ->will($this->returnValue($valueTaggedServiceIdsFound)); + + $definition->expects($this->never())->method('addMethodCall'); + + $translationDumperPass = new TranslationExtractorPass(); + $translationDumperPass->process($container); + } +} diff --git a/src/Symfony/Component/Translation/Tests/DependencyInjection/TranslationPassTest.php b/src/Symfony/Component/Translation/Tests/DependencyInjection/TranslationPassTest.php new file mode 100644 index 0000000000..8a4dff70a7 --- /dev/null +++ b/src/Symfony/Component/Translation/Tests/DependencyInjection/TranslationPassTest.php @@ -0,0 +1,50 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Translation\Tests\DependencyInjection; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\Translation\DependencyInjection\TranslatorPass; + +class TranslationPassTest extends TestCase +{ + public function testValidCollector() + { + $loader = (new Definition()) + ->addTag('translation.loader', array('alias' => 'xliff', 'legacy-alias' => 'xlf')); + + $translator = (new Definition()) + ->setArguments(array(null, null, null, null)); + + $container = new ContainerBuilder(); + $container->setDefinition('translator.default', $translator); + $container->setDefinition('translation.loader', $loader); + + $pass = new TranslatorPass(); + $pass->process($container); + + $expected = (new Definition()) + ->addTag('translation.loader', array('alias' => 'xliff', 'legacy-alias' => 'xlf')) + ->addMethodCall('addLoader', array('xliff', new Reference('translation.loader'))) + ->addMethodCall('addLoader', array('xlf', new Reference('translation.loader'))) + ; + $this->assertEquals($expected, $loader); + + $this->assertSame(array('translation.loader' => array('xliff', 'xlf')), $translator->getArgument(3)); + + $expected = array('translation.loader' => new ServiceClosureArgument(new Reference('translation.loader'))); + $this->assertEquals($expected, $container->getDefinition((string) $translator->getArgument(0))->getArgument(0)); + } +} diff --git a/src/Symfony/Component/Translation/composer.json b/src/Symfony/Component/Translation/composer.json index 4b4897e8e3..3f59d4214f 100644 --- a/src/Symfony/Component/Translation/composer.json +++ b/src/Symfony/Component/Translation/composer.json @@ -21,12 +21,14 @@ }, "require-dev": { "symfony/config": "~2.8|~3.0|~4.0", + "symfony/dependency-injection": "~3.4|~4.0", "symfony/intl": "^2.8.18|^3.2.5|~4.0", "symfony/yaml": "~3.3|~4.0", "psr/log": "~1.0" }, "conflict": { "symfony/config": "<2.8", + "symfony/dependency-injection": "<3.4", "symfony/yaml": "<3.3" }, "suggest": {