1
|
|
|
<?php |
2
|
|
|
/** A compiler pass that passes $_SERVER/$_ENV vars into the container parameters bag */ |
3
|
|
|
|
4
|
|
|
namespace Graviton\CoreBundle\Compiler; |
5
|
|
|
|
6
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
11
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
12
|
|
|
* @link http://swisscom.ch |
13
|
|
|
*/ |
14
|
|
|
class EnvParametersCompilerPass implements CompilerPassInterface |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* this is a workaround for a new symfony feature: |
19
|
|
|
* https://github.com/symfony/symfony/issues/7555 |
20
|
|
|
* |
21
|
|
|
* we *need* to be able to override any param with our env variables.. |
22
|
|
|
* so we do again, what the kernel did already here.. ;-) |
23
|
|
|
* |
24
|
|
|
* Since fabpot seems to have said bye to this feature we are |
25
|
|
|
* re-implementing it here. We are also adding some fancy json |
26
|
|
|
* parsing for hashes and arrays while at it. |
27
|
|
|
* |
28
|
|
|
* @todo add proper documentation on this "feature" to a README |
29
|
|
|
* |
30
|
|
|
* @param ContainerBuilder $container Container |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function process(ContainerBuilder $container) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
foreach (array_merge($_SERVER, $_ENV) as $key => $value) { |
37
|
|
|
if (0 === strpos($key, 'SYMFONY__')) { |
38
|
|
|
if (substr($value, 0, 1) == '[' || substr($value, 0, 1) == '{') { |
39
|
|
|
$value = json_decode($value, true); |
40
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
41
|
|
|
throw new \RuntimeException( |
42
|
|
|
sprintf('error "%s" in env variable "%s"', json_last_error_msg(), $key) |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
$container->setParameter(strtolower(str_replace('__', '.', substr($key, 9))), $value); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: