Passed
Push — master ( d41aff...ed0030 )
by Dāvis
03:46
created

SludioHelperExtension::load()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 4
nop 2
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\DependencyInjection;
4
5
use Sludio\HelperBundle\DependencyInjection\Component\ConfigureInterface;
6
use Sludio\HelperBundle\DependencyInjection\Requirement\AbstractRequirement;
7
use Symfony\Component\Config\FileLocator;
8
use Symfony\Component\DependencyInjection\ContainerBuilder;
9
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
10
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
11
12
if (!\defined('SLUDIO_HELPER')) {
13
    define('SLUDIO_HELPER', 'sludio_helper');
14
}
15
16
/**
17
 * This is the class that loads and manages your bundle configuration.
18
 *
19
 * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
20
 */
21
class SludioHelperExtension extends Extension
22
{
23
    private static $files = [
24
        'components.yml',
25
        'parameters.yml',
26
        'services.yml',
27
    ];
28
29
    /**
30
     * {@inheritdoc}
31
     *
32
     * @throws \Exception
33
     */
34
    public function load(array $configs, ContainerBuilder $container)
35
    {
36
        $config = $this->loadConfig($configs, $container);
37
38
        foreach ($config['extensions'] as $key => $extension) {
39
            /** @var $extension array */
40
            foreach ($extension as $variable => $value) {
41
                if ($value === reset($extension)) {
42
                    $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../'.ucfirst($key).'/Resources/config'));
43
                    foreach (self::$files as $file) {
44
                        if (file_exists(__DIR__.'/../'.ucfirst($key).'/Resources/config/'.$file)) {
45
                            $loader->load($file);
46
                        }
47
                    }
48
                }
49
                $container->setParameter($this->getAlias().'.'.$key.'.'.$variable, $value);
50
            }
51
            $this->checkComponent($key, $container, $this->getAlias());
52
        }
53
    }
54
55
    private function unsetExtension(array &$extensions = [])
56
    {
57
        foreach ($extensions as $key => $extension) {
58
            if (empty($extension['enabled'])) {
59
                unset($extensions[$key]);
60
            } else {
61
                $this->checkRequirements($key);
62
            }
63
        }
64
    }
65
66
    private function loadConfig(array $configs, ContainerBuilder $container)
67
    {
68
        $configuration = new Configuration($this->getAlias());
69
        /** @var $config array[] */
70
        $config = $this->processConfiguration($configuration, $configs);
71
72
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
73
        foreach (self::$files as $file) {
74
            if (file_exists(__DIR__.'/../Resources/config/'.$file)) {
75
                $loader->load($file);
76
            }
77
        }
78
79
        foreach ($config['other'] as $key => $other) {
80
            if (\is_array($other)) {
81
                foreach ($other as $variable => $value) {
82
                    $container->setParameter($this->getAlias().'.'.$key.'.'.$variable, $value);
83
                }
84
            } else {
85
                $container->setParameter($this->getAlias().'.'.$key, $other);
86
            }
87
        }
88
        $this->unsetExtension($config['extensions']);
89
90
        return $config;
91
    }
92
93
    public function getAlias()
94
    {
95
        return \SLUDIO_HELPER;
96
    }
97
98
    private function checkRequirements($key)
99
    {
100
        $className = 'Sludio\\HelperBundle\\DependencyInjection\\Requirement\\'.ucfirst($key);
101
        if (class_exists($className) && method_exists($className, 'check')) {
102
            /** @var AbstractRequirement $class */
103
            $class = new $className();
104
            $class->check($key);
105
        }
106
107
        return true;
108
    }
109
110
    /**
111
     * @param                  $key
112
     * @param ContainerBuilder $container
113
     * @param                  $alias
114
     */
115
    private function checkComponent($key, ContainerBuilder $container, $alias)
116
    {
117
        $className = 'Sludio\\HelperBundle\\DependencyInjection\\Component\\'.ucfirst($key);
118
        if (class_exists($className) && method_exists($className, 'configure')) {
119
            $class = new $className();
120
            if ($class instanceof ConfigureInterface) {
121
                $class->configure($container, $alias);
122
            }
123
        }
124
    }
125
}
126