Completed
Push — master ( 91bc8f...12870b )
by Nikola
05:50
created

TwigLoaderCompilerPass   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 41
wmc 9
lcom 0
cbo 4
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
D process() 0 38 9
1
<?php
2
/*
3
 * This file is part of the QueryResourcesLoader Bundle, an RunOpenCode project.
4
 *
5
 * (c) 2016 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Bundle\QueryResourcesLoader\DependencyInjection\CompilerPass;
11
12
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
13
use Symfony\Component\DependencyInjection\ContainerBuilder;
14
use Symfony\Component\DependencyInjection\Exception\LogicException;
15
use Symfony\Component\DependencyInjection\Reference;
16
17
class TwigLoaderCompilerPass implements CompilerPassInterface
18
{
19
    public function process(ContainerBuilder $container)
20
    {
21
        if (false === $container->hasDefinition('run_open_code.query_resources_loader.twig')) {
22
            return;
23
        }
24
25
        // register additional Query loaders
26
        $loaderIds = $container->findTaggedServiceIds('run_open_code.query_resources_loader.twig.loader');
27
28
        if (count($loaderIds) === 0) {
29
            throw new LogicException('No twig loaders found. You need to tag at least one loader with "run_open_code.query_resources_loader.twig.loader"');
30
        }
31
32
        if (count($loaderIds) === 1) {
33
            $container->setAlias('run_open_code.query_resources_loader.twig.loader', key($loaderIds));
34
        } else {
35
            $chainLoader = $container->getDefinition('run_open_code.query_resources_loader.twig.loader.chain');
36
37
            $prioritizedLoaders = array();
38
39
            foreach ($loaderIds as $id => $tags) {
40
                foreach ($tags as $tag) {
41
                    $priority = isset($tag['priority']) ? $tag['priority'] : 0;
42
                    $prioritizedLoaders[$priority][] = $id;
43
                }
44
            }
45
46
            krsort($prioritizedLoaders);
47
48
            foreach ($prioritizedLoaders as $loaders) {
49
                foreach ($loaders as $loader) {
50
                    $chainLoader->addMethodCall('addLoader', array(new Reference($loader)));
51
                }
52
            }
53
54
            $container->setAlias('run_open_code.query_resources_loader.twig.loader', 'run_open_code.query_resources_loader.twig.loader.chain');
55
        }
56
    }
57
}
58