Passed
Push — master ( 80df18...80df18 )
by Dāvis
04:16
created

SludioHelperExtension::checkRequirements()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 1
dl 0
loc 10
rs 9.4285
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
/**
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
20
    public function getAlias()
21
    {
22
        return 'sludio_helper';
23
    }
24
25
    private function checkComponent($key)
26
    {
27
        $className = 'Sludio\\HelperBundle\\DependencyInjection\\Component\\'.ucfirst($key);
28
        if (class_exists($className)) {
29
            $class = new $className();
30
            if ($class instanceof ConfigureInterface) {
31
                return $class;
32
            }
33
        }
34
35
        return null;
36
    }
37
38
    private function checkRequirements($key)
39
    {
40
        $className = 'Sludio\\HelperBundle\\DependencyInjection\\Requirement\\'.ucfirst($key);
41
        if (class_exists($className) && method_exists($className, 'check')) {
42
            /** @var AbstractRequirement $class */
43
            $class = new $className();
44
            $class->check();
45
        }
46
47
        return true;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     * @throws \Exception
53
     */
54
    public function load(array $configs, ContainerBuilder $container)
55
    {
56
        $configuration = new Configuration($this->getAlias());
57
        /** @var $config array[] */
58
        $config = $this->processConfiguration($configuration, $configs);
59
60
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
61
        $files = [
62
            'components.yml',
63
            'parameters.yml',
64
            'services.yml',
65
        ];
66
        foreach ($files as $file) {
67
            if (file_exists(__DIR__.'/../Resources/config/'.$file)) {
68
                $loader->load($file);
69
            }
70
        }
71
72
        foreach ($config['other'] as $key => $other) {
73
            if (\is_array($other)) {
74
                foreach ($other as $variable => $value) {
75
                    $container->setParameter($this->getAlias().'.'.$key.'.'.$variable, $config['other'][$key][$variable]);
76
                }
77
            } else {
78
                $container->setParameter($this->getAlias().'.'.$key, $config['other'][$key]);
79
            }
80
        }
81
82
        foreach ($config['extensions'] as $key => $extension) {
83
            if (!isset($extension['enabled']) || $extension['enabled'] !== true) {
84
                continue;
85
            }
86
            if ($this->checkRequirements($key)) {
87
                $iterator = 0;
88
                /** @var $extension array */
89
                foreach ($extension as $variable => $value) {
90
                    $iterator++;
91
                    if ($iterator === 1) {
92
                        $files = [
93
                            'components.yml',
94
                            'parameters.yml',
95
                            'services.yml',
96
                        ];
97
                        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../'.ucfirst($key).'/Resources/config'));
98
                        foreach ($files as $file) {
99
                            if (file_exists(__DIR__.'/../'.ucfirst($key).'/Resources/config/'.$file)) {
100
                                $loader->load($file);
101
                            }
102
                        }
103
                    }
104
                    $container->setParameter($this->getAlias().'.'.$key.'.'.$variable, $config['extensions'][$key][$variable]);
105
                }
106
                if ($component = $this->checkComponent($key)) {
107
                    /** @var $component ConfigureInterface */
108
                    $component->configure($container, $this->getAlias());
109
                }
110
            }
111
        }
112
    }
113
}
114