blob: fab6b20ae909f35f188f3a49b5021f26044a0074 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Translation\DependencyInjection;
13
14use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15use Symfony\Component\DependencyInjection\ContainerBuilder;
16use Symfony\Component\DependencyInjection\Exception\RuntimeException;
17use Symfony\Component\DependencyInjection\Reference;
18
19/**
20 * Adds tagged translation.extractor services to translation extractor.
21 */
22class TranslationExtractorPass implements CompilerPassInterface
23{
24 private $extractorServiceId;
25 private $extractorTag;
26
27 public function __construct(string $extractorServiceId = 'translation.extractor', string $extractorTag = 'translation.extractor')
28 {
29 if (0 < \func_num_args()) {
30 trigger_deprecation('symfony/translation', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
31 }
32
33 $this->extractorServiceId = $extractorServiceId;
34 $this->extractorTag = $extractorTag;
35 }
36
37 public function process(ContainerBuilder $container)
38 {
39 if (!$container->hasDefinition($this->extractorServiceId)) {
40 return;
41 }
42
43 $definition = $container->getDefinition($this->extractorServiceId);
44
45 foreach ($container->findTaggedServiceIds($this->extractorTag, true) as $id => $attributes) {
46 if (!isset($attributes[0]['alias'])) {
47 throw new RuntimeException(sprintf('The alias for the tag "translation.extractor" of service "%s" must be set.', $id));
48 }
49
50 $definition->addMethodCall('addExtractor', [$attributes[0]['alias'], new Reference($id)]);
51 }
52 }
53}