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\Bundle\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'] 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) |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
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) |
|
|
|
|
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)->defineDefaultConfiguration($configSonataAdmin['default']); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* This method is called during the self::load() process, to add the logic related to SonataAdmin. |
172
|
|
|
* |
173
|
|
|
* @param ContainerBuilder $container |
174
|
|
|
* @param array $config |
175
|
|
|
* |
176
|
|
|
* @return self |
177
|
|
|
*/ |
178
|
|
|
public function loadListeners(ContainerBuilder $container, array $config) |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* This method is called at the end of the self::load() process, to add any logic needed. |
185
|
|
|
* |
186
|
|
|
* @param ContainerBuilder $container |
187
|
|
|
* @param FileLoader $loader |
188
|
|
|
* @param array $config |
189
|
|
|
* |
190
|
|
|
* @return self |
191
|
|
|
*/ |
192
|
|
|
public function doLoad(ContainerBuilder $container, FileLoader $loader, array $config) |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param string $var the parameter name |
199
|
|
|
* @param ContainerBuilder $container |
200
|
|
|
* @param string $dir |
201
|
|
|
* @param string $file_name |
202
|
|
|
* |
203
|
|
|
* @return self |
204
|
|
|
*/ |
205
|
|
|
protected function mergeParameter($var, $container, $dir, $file_name = 'blast.yml') |
206
|
|
|
{ |
207
|
|
|
$loader = new YamlFileLoader($newContainer = new ContainerBuilder(), new FileLocator($dir)); |
208
|
|
|
$loader->load($file_name); |
209
|
|
|
|
210
|
|
|
if (!$container->hasParameter($var) || !is_array($container->getParameter($var))) { |
211
|
|
|
$container->setParameter($var, []); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
if (!is_array($newContainer->getParameter($var))) { |
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$container->setParameter($var, array_merge( |
219
|
|
|
$container->getParameter($var), |
220
|
|
|
$newContainer->getParameter($var) |
221
|
|
|
)); |
222
|
|
|
|
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: