Passed
Push — master ( abdb51...0ee754 )
by Dev
16:27 queued 01:19
created

PiedWebCMSExtension   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 20
c 1
b 1
f 0
dl 0
loc 49
rs 10
ccs 0
cts 23
cp 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 9 1
A loadConfigToParameters() 0 12 4
A parsAppsConfig() 0 8 2
A getAlias() 0 3 1
1
<?php
2
/**
3
 * todo: make it cleaner: https://symfony.com/doc/current/bundles/prepend_extension.html.
4
 */
5
6
namespace PiedWeb\CMSBundle\DependencyInjection;
7
8
use Symfony\Component\Config\FileLocator;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Extension\Extension;
11
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
12
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
13
use Symfony\Component\Finder\Finder;
14
use Symfony\Component\Yaml\Parser;
15
16
class PiedWebCMSExtension extends Extension //implements PrependExtensionInterface
17
{
18
    public function load(array $configs, ContainerBuilder $container)
19
    {
20
        $configuration = new Configuration(); //$configuration = $this->getConfiguration($configs, $container);
21
        $config = $this->processConfiguration($configuration, $configs);
22
23
        self::loadConfigToParameters($container, $config);
24
25
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
26
        $loader->load('services.yaml');
27
28
        // todo : https://symfony.com/doc/current/bundles/extension.html#adding-classes-to-compile
29
    }
30
31
    /**
32
     * @param string $prefix must contain the last
33
     *
34
     * @return void
35
     */
36
    protected static function loadConfigToParameters(ContainerBuilder $container, array $config, $prefix = '')
37
    {
38
        $container->setParameter('pwc', $config);
39
40
        foreach ($config as $key => $value) {
41
            if ('apps' === $key) {
42
                $container->setParameter('pwc.apps', self::parsAppsConfig($value));
43
            } elseif (is_array($value)) {
44
                self::loadConfigToParameters($container, $value, $prefix.$key.'.');
45
            } else {
46
                $container->setParameter('app.'.$prefix.$key, $value); // to deprecate in next release
47
                $container->setParameter('pwc.'.$prefix.$key, $value);
48
            }
49
        }
50
    }
51
52
    protected static function parsAppsConfig($apps)
53
    {
54
        $result = [];
55
        foreach ($apps as $app) {
56
            $result[$app['hosts'][0]] = $app;
57
        }
58
59
        return $result;
60
    }
61
62
    public function getAlias()
63
    {
64
        return 'piedweb_cms'; // change to pwc todo
65
    }
66
67
    /*
68
    public function prepend(ContainerBuilder $container)
69
    {
70
        // Load configurations for other package
71
        $parser = new Parser();
72
        $finder = Finder::create()->files()->name('*.yaml')->in(__DIR__.'/../Resources/config/packages/');
73
        foreach ($finder as $file) {
74
            $configs = $parser->parse(file_get_contents($file->getRealPath()));
75
            foreach ($configs as $name => $config) {
76
                $container->prependExtensionConfig($name, $config);
77
            }
78
        }
79
    }
80
    /**/
81
}
82