Completed
Push — master ( 1e3fe5...2c0ff3 )
by
unknown
02:52
created

src/DependencyInjection/BlastCoreExtension.php (11 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Blast Project package.
5
 *
6
 * Copyright (C) 2015-2017 Libre Informatique
7
 *
8
 * This file is licenced under the GNU LGPL v3.
9
 * For the full copyright and license information, please view the LICENSE.md
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Blast\CoreBundle\DependencyInjection;
14
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\Config\FileLocator;
17
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
18
use Symfony\Component\DependencyInjection\Loader\FileLoader;
19
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
20
use Symfony\Component\Yaml\Yaml;
21
22
/**
23
 * This is the class that loads and manages your bundle configuration.
24
 *
25
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
26
 */
27
class BlastCoreExtension extends Extension
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function load(array $configs, ContainerBuilder $container)
33
    {
34
        $config = $this->processConfiguration($this->getConfiguration([], $container), $configs);
35
        $this->initialize();
36
        $loader = $this->buildLoader($container);
37
38
        $this->loadServices($loader);
39
        $this->loadCodeGenerators($container, $config);
40
        $this->loadDataFixtures($container, $loader);
41
        $this->loadParameters($container);
42
        $this->loadSecurity($container);
43
        $this->loadSonataAdmin($container, $loader);
44
        $this->loadListeners($container, $config);
45
        $this->doLoad($container, $loader, $config);
46
    }
47
48
    /**
49
     * @return self
50
     */
51
    public function initialize()
52
    {
53
        $rc = new \ReflectionClass($this);
54
        $this->dir = dirname($rc->getFileName());
55
        $this->prefix = '/../Resources/config/';
56
        $this->bundlesPrefix = $this->prefix . 'bundles/';
57
        $this->suffix = '.yml';
58
        $this->file = 'blast';
59
60
        return $this;
61
    }
62
63
    /**
64
     * the buildLoader returns the required FileLoader.
65
     *
66
     * @param ContainerBuilder $container
67
     *
68
     * @return FileLoader
69
     */
70
    public function buildLoader(ContainerBuilder $container)
71
    {
72
        return new YamlFileLoader($container, new FileLocator($this->dir . $this->prefix));
73
    }
74
75
    /**
76
     * This method is called during the self::load() process, to add the logic related to SonataAdmin.
77
     *
78
     * @param FileLoader $loader
79
     *
80
     * @return self
81
     */
82
    public function loadServices(FileLoader $loader)
83
    {
84
        // services, admin & config files
85
        foreach (['services', 'admin', 'config'] as $fileName) {
86
            if (file_exists($this->dir . $this->prefix . $fileName . $this->suffix)) {
87
                $loader->load($fileName . $this->suffix);
88
            }
89
        }
90
91
        return $this;
92
    }
93
94
    /**
95
     * This method is called after loading the services in the self::load() process, to load code generators.
96
     *
97
     * @param ContainerBuilder $container
98
     * @param array            $config
99
     *
100
     * @return self
101
     */
102
    public function loadCodeGenerators(ContainerBuilder $container, array $config)
0 ignored issues
show
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
103
    {
104
        return $this;
105
    }
106
107
    /**
108
     * This method is called after loading the services in the self::load() process, to load data fixtures.
109
     *
110
     * @param ContainerBuilder $container
111
     * @param FileLoader       $loader
112
     *
113
     * @return self
114
     */
115
    public function loadDataFixtures(ContainerBuilder $container, FileLoader $loader)
0 ignored issues
show
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $loader is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
116
    {
117
        return $this;
118
    }
119
120
    /**
121
     * This method is called after loading the services in the self::load() process, to load data fixtures.
122
     *
123
     * @param ContainerBuilder $container
124
     *
125
     * @return self
126
     */
127
    public function loadParameters(ContainerBuilder $container)
128
    {
129
        // the blast.yml
130
        if (file_exists($this->dir . $this->prefix . $this->file . $this->suffix)) {
131
            $this->mergeParameter('blast', $container, $this->dir . $this->prefix);
132
        }
133
134
        return $this;
135
    }
136
137
    /**
138
     * This method is called at the end of the self::load() process, to add security related logic.
139
     *
140
     * @param ContainerBuilder $container
141
     *
142
     * @return self
143
     */
144
    public function loadSecurity(ContainerBuilder $container)
0 ignored issues
show
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
145
    {
146
        return $this;
147
    }
148
149
    /**
150
     * This method is called at the end of the self::load() process, to add the logic related to SonataAdmin.
151
     *
152
     * @param ContainerBuilder $container
153
     * @param FileLoader       $loader
154
     *
155
     * @return self
156
     */
157
    public function loadSonataAdmin(ContainerBuilder $container, FileLoader $loader)
0 ignored issues
show
The parameter $loader is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
158
    {
159
        if (file_exists($path = $this->dir . $this->bundlesPrefix . 'sonata_admin' . $this->suffix)) {
160
            $configSonataAdmin = Yaml::parse(
161
                file_get_contents($path)
162
            );
163
164
            DefaultParameters::getInstance($container)
165
                ->defineDefaultConfiguration($configSonataAdmin['default'])
166
            ;
167
        }
168
169
        return $this;
170
    }
171
172
    /**
173
     * This method is called during the self::load() process, to add the logic related to SonataAdmin.
174
     *
175
     * @param ContainerBuilder $container
176
     * @param array            $config
177
     *
178
     * @return self
179
     */
180
    public function loadListeners(ContainerBuilder $container, array $config)
0 ignored issues
show
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
181
    {
182
        return $this;
183
    }
184
185
    /**
186
     * This method is called at the end of the self::load() process, to add any logic needed.
187
     *
188
     * @param ContainerBuilder $container
189
     * @param FileLoader       $loader
190
     * @param array            $config
191
     *
192
     * @return self
193
     */
194
    public function doLoad(ContainerBuilder $container, FileLoader $loader, array $config)
0 ignored issues
show
The parameter $container is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $loader is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
195
    {
196
        return $this;
197
    }
198
199
    /**
200
     * @param string           $var       the parameter name
201
     * @param ContainerBuilder $container
202
     * @param string           $dir
203
     * @param string           $file_name
204
     *
205
     * @return self
206
     */
207
    protected function mergeParameter($var, $container, $dir, $file_name = 'blast.yml')
208
    {
209
        $loader = new YamlFileLoader($newContainer = new ContainerBuilder(), new FileLocator($dir));
210
        $loader->load($file_name);
211
212
        if (!$container->hasParameter($var) || !is_array($container->getParameter($var))) {
213
            $container->setParameter($var, []);
214
        }
215
216
        if (!is_array($newContainer->getParameter($var))) {
217
            return $this;
218
        }
219
220
        $container->setParameter($var, array_merge(
221
            $container->getParameter($var),
222
            $newContainer->getParameter($var)
223
        ));
224
225
        return $this;
226
    }
227
}
228