ExtensionManager::initialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Openl10n\Cli\ServiceContainer;
4
5
use Openl10n\Cli\ServiceContainer\Configuration\ConfigurationTree;
6
use Openl10n\Cli\ServiceContainer\Exception\ConfigurationProcessingException;
7
use Openl10n\Cli\ServiceContainer\Extension\ConfiguredExtension;
8
use Symfony\Component\Config\Definition\Processor;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
class ExtensionManager
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $extensions;
17
18
    /**
19
     * @param array $extensions The extensions
20
     */
21
    public function __construct(array $extensions = [])
22
    {
23
        $this->extensions = $extensions;
24
    }
25
26
    /**
27
     * Initialize each extension with the given container.
28
     *
29
     * @param ContainerInterface $container
30
     */
31
    public function initialize(ContainerInterface $container)
32
    {
33
        foreach ($this->extensions as $extension) {
34
            $extension->initialize($container);
35
        }
36
    }
37
38
    /**
39
     * Load more services from the extensions with the given configuration.
40
     *
41
     * @param array              $rawConfigs The raw configuration
42
     * @param ContainerInterface $container  The service container
43
     *
44
     * @throws ConfigurationProcessingException If the raw configuration
45
     *         contains a key which does not match any extensions
46
     */
47
    public function load(array $rawConfigs, ContainerInterface $container)
48
    {
49
        // Validate configuration (only ConfiguredExtension are impacted)
50
        $extensions = array_filter($this->extensions, function ($extension) {
51
            return $extension instanceof ConfiguredExtension;
52
        });
53
54
        // Process configuration
55
        $configurationTree = new ConfigurationTree($extensions);
56
        $configs = (new Processor())->processConfiguration(
57
            $configurationTree,
58
            array('openl10n' => $rawConfigs)
59
        );
60
61
        // Load extension with correct configuration
62
        foreach ($extensions as $extension) {
63
            $name = $extension->getName();
64
65
            if (!isset($configs[$name])) {
66
                throw new ConfigurationProcessingException(sprintf('Missing configuration for node "%s"', $name));
67
            }
68
69
            $config = $configs[$name];
70
71
            $extension->load($config, $container);
72
        }
73
    }
74
}
75