Completed
Push — master ( f3c39a...26f1f7 )
by Tobias
08:36
created

ExternalTranslatorPass::process()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.439
cc 6
eloc 14
nc 9
nop 1
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[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 Translation\Bundle\DependencyInjection\CompilerPass;
13
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
use Symfony\Component\DependencyInjection\Reference;
18
19
/**
20
 * @author Tobias Nyholm <[email protected]>
21
 */
22
class ExternalTranslatorPass implements CompilerPassInterface
23
{
24
    public function process(ContainerBuilder $container)
25
    {
26
        /* @var Definition $def */
27
        if (!$container->hasDefinition('php_translation.translator_service.external_translator')) {
28
            return;
29
        }
30
31
        $services = $container->findTaggedServiceIds('php_translation.external_translator');
32
        $translators = [];
33
        foreach ($services as $id => $tags) {
34
            foreach ($tags as $tag) {
35
                if (!isset($tag['priority'])) {
36
                    $tag['priority'] = 0;
37
                }
38
                $translators[$id] = $tag['priority'];
39
            }
40
        }
41
42
        // Sort by priority
43
        asort($translators);
44
45
        $def = $container->getDefinition('php_translation.translator_service.external_translator');
46
        foreach ($translators as $id => $prio) {
47
            $def->addMethodCall('addTranslatorService', [new Reference($id)]);
48
        }
49
    }
50
}
51