Completed
Pull Request — master (#6)
by De Cramer
11:31
created

PluginPass::process()   D

Complexity

Conditions 9
Paths 19

Size

Total Lines 37
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 4.909
c 0
b 0
f 0
cc 9
eloc 18
nc 19
nop 1
1
<?php
2
3
namespace eXpansion\Core\DependencyInjection\Compiler;
4
5
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
8
/**
9
 * PluginPass to register all plugins to the plugin manager.
10
 *
11
 * @package eXpansion\Core\DependencyInjection\Compiler
12
 */
13
class PluginPass implements CompilerPassInterface
14
{
15
    /**
16
     * Register all data providers to the Manager.
17
     *
18
     * @param ContainerBuilder $container
19
     */
20
    public function process(ContainerBuilder $container)
21
    {
22
        if (!$container->has('expansion.core.services.plugin_manager')) {
23
            return;
24
        }
25
26
        $pluginsData = [];
27
28
        // Find all Data Provider services.
29
        $plugins = $container->findTaggedServiceIds('expansion.plugin');
30
        foreach ($plugins as $id => $tags) {
31
            foreach ($tags as $attributes) {
32
                $pluginsData[$id]['dataProviders'][] = $attributes['data_provider'];
33
            }
34
        }
35
36
        // FInd the parent services.
37
        $plugins = $container->findTaggedServiceIds('expansion.plugin.parent');
38
        foreach ($plugins as $id => $tags) {
39
            foreach ($tags as $attributes) {
40
                $pluginsData[$id]['parent'][] = $attributes['parent'];
41
            }
42
        }
43
44
        // Get the data provider manager service definition to register data providers into.
45
        $definition = $container->getDefinition('expansion.core.services.plugin_manager');
46
47
        foreach ($pluginsData as $pluginId => $data)
48
        {
49
            $definition->addMethodCall('registerPlugin', [
50
                    $pluginId,
51
                    empty($data['dataProviders']) ? [] : $data['dataProviders'],
52
                    empty($data['parent']) ? [] : $data['parent'],
53
                ]
54
            );
55
        }
56
    }
57
}