comparison vendor/symfony/dependency-injection/EnvVarProcessor.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\DependencyInjection;
13
14 use Symfony\Component\Config\Util\XmlUtils;
15 use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
16 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
17
18 /**
19 * @author Nicolas Grekas <p@tchwork.com>
20 */
21 class EnvVarProcessor implements EnvVarProcessorInterface
22 {
23 private $container;
24
25 public function __construct(ContainerInterface $container)
26 {
27 $this->container = $container;
28 }
29
30 /**
31 * {@inheritdoc}
32 */
33 public static function getProvidedTypes()
34 {
35 return array(
36 'base64' => 'string',
37 'bool' => 'bool',
38 'const' => 'bool|int|float|string|array',
39 'file' => 'string',
40 'float' => 'float',
41 'int' => 'int',
42 'json' => 'array',
43 'resolve' => 'string',
44 'string' => 'string',
45 );
46 }
47
48 /**
49 * {@inheritdoc}
50 */
51 public function getEnv($prefix, $name, \Closure $getEnv)
52 {
53 $i = strpos($name, ':');
54
55 if ('file' === $prefix) {
56 if (!is_scalar($file = $getEnv($name))) {
57 throw new RuntimeException(sprintf('Invalid file name: env var "%s" is non-scalar.', $name));
58 }
59 if (!file_exists($file)) {
60 throw new RuntimeException(sprintf('Env "file:%s" not found: %s does not exist.', $name, $file));
61 }
62
63 return file_get_contents($file);
64 }
65
66 if (false !== $i || 'string' !== $prefix) {
67 if (null === $env = $getEnv($name)) {
68 return;
69 }
70 } elseif (isset($_ENV[$name])) {
71 $env = $_ENV[$name];
72 } elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
73 $env = $_SERVER[$name];
74 } elseif (false === ($env = getenv($name)) || null === $env) { // null is a possible value because of thread safety issues
75 if (!$this->container->hasParameter("env($name)")) {
76 throw new EnvNotFoundException($name);
77 }
78
79 if (null === $env = $this->container->getParameter("env($name)")) {
80 return;
81 }
82 }
83
84 if (!is_scalar($env)) {
85 throw new RuntimeException(sprintf('Non-scalar env var "%s" cannot be cast to %s.', $name, $prefix));
86 }
87
88 if ('string' === $prefix) {
89 return (string) $env;
90 }
91
92 if ('bool' === $prefix) {
93 return (bool) self::phpize($env);
94 }
95
96 if ('int' === $prefix) {
97 if (!is_numeric($env = self::phpize($env))) {
98 throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to int.', $name));
99 }
100
101 return (int) $env;
102 }
103
104 if ('float' === $prefix) {
105 if (!is_numeric($env = self::phpize($env))) {
106 throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to float.', $name));
107 }
108
109 return (float) $env;
110 }
111
112 if ('const' === $prefix) {
113 if (!defined($env)) {
114 throw new RuntimeException(sprintf('Env var "%s" maps to undefined constant "%s".', $name, $env));
115 }
116
117 return constant($env);
118 }
119
120 if ('base64' === $prefix) {
121 return base64_decode($env);
122 }
123
124 if ('json' === $prefix) {
125 $env = json_decode($env, true);
126
127 if (JSON_ERROR_NONE !== json_last_error()) {
128 throw new RuntimeException(sprintf('Invalid JSON in env var "%s": '.json_last_error_msg(), $name));
129 }
130
131 if (!is_array($env)) {
132 throw new RuntimeException(sprintf('Invalid JSON env var "%s": array expected, %s given.', $name, gettype($env)));
133 }
134
135 return $env;
136 }
137
138 if ('resolve' === $prefix) {
139 return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($name) {
140 if (!isset($match[1])) {
141 return '%';
142 }
143 $value = $this->container->getParameter($match[1]);
144 if (!is_scalar($value)) {
145 throw new RuntimeException(sprintf('Parameter "%s" found when resolving env var "%s" must be scalar, "%s" given.', $match[1], $name, gettype($value)));
146 }
147
148 return $value;
149 }, $env);
150 }
151
152 throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
153 }
154
155 private static function phpize($value)
156 {
157 if (!class_exists(XmlUtils::class)) {
158 throw new RuntimeException('The Symfony Config component is required to cast env vars to "bool", "int" or "float".');
159 }
160
161 return XmlUtils::phpize($value);
162 }
163 }