CoreCompilerPass   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 8

Importance

Changes 0
Metric Value
wmc 7
cbo 8
dl 0
loc 89
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 18 1
A findBldrServicesTaggedWith() 0 11 2
A buildBuilder() 0 15 1
A addSubscribers() 0 7 2
A buildTaskRegistry() 0 13 1
1
<?php
2
3
/**
4
 * This file is part of Bldr.io
5
 *
6
 * (c) Aaron Scherer <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE
10
 */
11
12
namespace Bldr\Block\Core\CompilerPass;
13
14
use Bldr\Application;
15
use Bldr\Block\Core\Command as CoreCommand;
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Definition;
19
use Symfony\Component\DependencyInjection\Reference;
20
21
/**
22
 * @author Aaron Scherer <[email protected]>
23
 */
24
class CoreCompilerPass implements CompilerPassInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function process(ContainerBuilder $container)
30
    {
31
        $this->buildBuilder($container);
32
        $this->addSubscribers($container);
33
        $this->buildTaskRegistry($container);
34
35
        /** @var Application $application */
36
        $application = $container->get('application');
37
38
        $application->addCommands(
39
            [
40
                new CoreCommand\RunCommand(),
41
                new CoreCommand\TaskCommand(),
42
                new CoreCommand\Task\InfoCommand(),
43
                new CoreCommand\Task\ListCommand()
44
            ]
45
        );
46
    }
47
48
    /**
49
     * @param ContainerBuilder $container
50
     * @param string           $tagName
51
     *
52
     * @return Reference[]
53
     */
54
    private function findBldrServicesTaggedWith(ContainerBuilder $container, $tagName)
55
    {
56
        $serviceIds = array_keys($container->findTaggedServiceIds($tagName));
57
58
        $services = [];
59
        foreach ($serviceIds as $id) {
60
            $services[] = new Reference($id);
61
        }
62
63
        return $services;
64
    }
65
66
    /**
67
     * @param ContainerBuilder $container
68
     */
69
    private function buildBuilder(ContainerBuilder $container)
70
    {
71
        $container->setDefinition(
72
            'bldr.builder',
73
            new Definition(
74
                'Bldr\Block\Core\Service\Builder',
75
                [
76
                    new Reference('bldr.dispatcher'),
77
                    new Reference('output'),
78
                    new Reference('helper_set'),
79
                    new Reference('bldr.registry.task')
80
                ]
81
            )
82
        );
83
    }
84
85
    /**
86
     * @param ContainerBuilder $container
87
     */
88
    private function addSubscribers(ContainerBuilder $container)
89
    {
90
        $dispatcher = $container->findDefinition('bldr.dispatcher');
91
        foreach ($this->findBldrServicesTaggedWith($container, 'bldr_subscriber') as $subscriber) {
92
            $dispatcher->addMethodCall('addSubscriber', [$subscriber]);
93
        }
94
    }
95
96
    /**
97
     * @param ContainerBuilder $container
98
     */
99
    private function buildTaskRegistry(ContainerBuilder $container)
100
    {
101
        $container->setDefinition(
102
            'bldr.registry.task',
103
            new Definition(
104
                'Bldr\Registry\TaskRegistry',
105
                [
106
                    new Reference('bldr.dispatcher'),
107
                    $this->findBldrServicesTaggedWith($container, 'bldr')
108
                ]
109
            )
110
        );
111
    }
112
}
113