PiedWebCMSExtension::prepend()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
eloc 9
c 1
b 1
f 0
nc 7
nop 1
dl 0
loc 15
ccs 0
cts 0
cp 0
crap 30
rs 9.6111
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
        $pwcTemplate = Configuration::DEFAULT_TEMPLATE;
41
42
        foreach ($config as $key => $value) {
43
            if ('template' === $key) {
44
                $pwcTemplate = $value;
45
            }
46
47
            if ('apps' === $key) {
48
                $container->setParameter('pwc.apps', self::parsAppsConfig($value, $pwcTemplate));
49
            } elseif (\is_array($value)) {
50
                self::loadConfigToParameters($container, $value, $prefix.$key.'.');
51
            } else {
52
                $container->setParameter('app.'.$prefix.$key, $value); // to deprecate in next release
53
                $container->setParameter('pwc.'.$prefix.$key, $value);
54
            }
55
        }
56
    }
57
58
    protected static function parsAppsConfig($apps, $pwcTemplate)
59
    {
60
        $result = [];
61
        foreach ($apps as $app) {
62
            if (! $app['template']) {
63
                $app['template'] = $pwcTemplate;
64
            }
65
            $result[$app['hosts'][0]] = $app;
66
        }
67
68
        return $result;
69
    }
70
71
    public function getAlias()
72
    {
73
        return 'piedweb_cms'; // change to pwc todo
74
    }
75
76
    public function prepend(ContainerBuilder $container)
77
    {
78
        // Load configurations for other package
79
        $parser = new Parser();
80
        $finder = Finder::create()->files()->name('*.yaml')->in(__DIR__.'/../Resources/config/packages/');
81
        foreach ($finder as $file) {
82
            $configs = $parser->parse(file_get_contents($file->getRealPath()));
83
            if ('sonata_admin_blob' == substr($file->getRealPath(), 0, -5)) {
84
                // check if extension is loaded
85
            }
86
            foreach ($configs as $name => $config) {
87
                if ('piedweb_cms' == $name) { // this file is just for doc purpose //|| 'security' == $name
88
                    continue;
89
                }
90
                $container->prependExtensionConfig($name, $config);
91
            }
92
        }
93
    }
94
}
95