Passed
Push — master ( 84d9c2...9fa36a )
by Dev
10:32
created

PiedWebCMSExtension::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 2
rs 10
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();
21
        $config = $this->processConfiguration($configuration, $configs);
22
23
        // Better idea to get config everywhere ?
24
        // not get config every where and load only what's need
25
        self::loadConfigHelper($container, $config);
26
27
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
28
        $loader->load('services.yaml');
29
30
        // todo : https://symfony.com/doc/current/bundles/extension.html#adding-classes-to-compile
31
    }
32
33
    /**
34
     * @param string $prefix must contain the last
35
     *
36
     * @return void
37
     */
38
    protected static function loadConfigHelper(ContainerBuilder $container, array $config, $prefix = '')
39
    {
40
        foreach ($config as $key => $value) {
41
            if (is_array($value)) {
42
                self::loadConfigHelper($container, $value, $prefix.$key.'.');
43
            } else {
44
                $container->setParameter('app.'.$prefix.$key, $value); // to deprecate in next release
45
                $container->setParameter('pwc.'.$prefix.$key, $value);
46
            }
47
        }
48
    }
49
50
    public function getAlias()
51
    {
52
        return 'piedweb_cms'; // change to pwc todo
53
    }
54
55
    /*
56
    public function prepend(ContainerBuilder $container)
57
    {
58
        // Load configurations for other package
59
        $parser = new Parser();
60
        $finder = Finder::create()->files()->name('*.yaml')->in(__DIR__.'/../Resources/config/packages/');
61
        foreach ($finder as $file) {
62
            $configs = $parser->parse(file_get_contents($file->getRealPath()));
63
            foreach ($configs as $name => $config) {
64
                $container->prependExtensionConfig($name, $config);
65
            }
66
        }
67
    }
68
    /**/
69
}
70