Passed
Push — master ( 47232d...719143 )
by Dāvis
04:16
created

SludioHelperExtension   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 101
rs 10
wmc 23

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlias() 0 3 1
D load() 0 31 10
A checkComponent() 0 11 3
B loadConfig() 0 29 6
A checkRequirements() 0 10 3
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
/**
13
 * This is the class that loads and manages your bundle configuration.
14
 *
15
 * @link http://symfony.com/doc/current/cookbook/bundles/extension.html
16
 */
17
class SludioHelperExtension extends Extension
18
{
19
    private function loadConfig(array $configs, ContainerBuilder $container)
20
    {
21
        $configuration = new Configuration($this->getAlias());
22
        /** @var $config array[] */
23
        $config = $this->processConfiguration($configuration, $configs);
24
25
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
26
        $files = [
27
            'components.yml',
28
            'parameters.yml',
29
            'services.yml',
30
        ];
31
        foreach ($files as $file) {
32
            if (file_exists(__DIR__.'/../Resources/config/'.$file)) {
33
                $loader->load($file);
34
            }
35
        }
36
37
        foreach ($config['other'] as $key => $other) {
38
            if (\is_array($other)) {
39
                foreach ($other as $variable => $value) {
40
                    $container->setParameter($this->getAlias().'.'.$key.'.'.$variable, $config['other'][$key][$variable]);
41
                }
42
            } else {
43
                $container->setParameter($this->getAlias().'.'.$key, $config['other'][$key]);
44
            }
45
        }
46
47
        return $config;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     * @throws \Exception
53
     */
54
    public function load(array $configs, ContainerBuilder $container)
55
    {
56
        $config = $this->loadConfig($configs, $container);
57
58
        foreach ($config['extensions'] as $key => $extension) {
59
            if (!isset($extension['enabled']) || $extension['enabled'] !== true) {
60
                continue;
61
            }
62
            if ($this->checkRequirements($key)) {
63
                $iterator = 0;
64
                /** @var $extension array */
65
                foreach ($extension as $variable => $value) {
66
                    $iterator++;
67
                    if ($iterator === 1) {
0 ignored issues
show
introduced by
The condition $iterator === 1 can never be true.
Loading history...
68
                        $files = [
69
                            'components.yml',
70
                            'parameters.yml',
71
                            'services.yml',
72
                        ];
73
                        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../'.ucfirst($key).'/Resources/config'));
74
                        foreach ($files as $file) {
75
                            if (file_exists(__DIR__.'/../'.ucfirst($key).'/Resources/config/'.$file)) {
76
                                $loader->load($file);
77
                            }
78
                        }
79
                    }
80
                    $container->setParameter($this->getAlias().'.'.$key.'.'.$variable, $config['extensions'][$key][$variable]);
81
                }
82
                if ($component = $this->checkComponent($key)) {
83
                    /** @var $component ConfigureInterface */
84
                    $component->configure($container, $this->getAlias());
85
                }
86
            }
87
        }
88
    }
89
90
    public function getAlias()
91
    {
92
        return 'sludio_helper';
93
    }
94
95
    private function checkRequirements($key)
96
    {
97
        $className = 'Sludio\\HelperBundle\\DependencyInjection\\Requirement\\'.ucfirst($key);
98
        if (class_exists($className) && method_exists($className, 'check')) {
99
            /** @var AbstractRequirement $class */
100
            $class = new $className();
101
            $class->check($key);
102
        }
103
104
        return true;
105
    }
106
107
    private function checkComponent($key)
108
    {
109
        $className = 'Sludio\\HelperBundle\\DependencyInjection\\Component\\'.ucfirst($key);
110
        if (class_exists($className)) {
111
            $class = new $className();
112
            if ($class instanceof ConfigureInterface) {
113
                return $class;
114
            }
115
        }
116
117
        return null;
118
    }
119
}
120