EnvVarProcessor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
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\DependencyInjection\Exception\EnvNotFoundException;
15
use Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
16
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
17
use Symfony\Contracts\Service\ResetInterface;
18
19
/**
20
 * @author Nicolas Grekas <[email protected]>
21
 */
22
class EnvVarProcessor implements EnvVarProcessorInterface, ResetInterface
23
{
24
    /** @var \Traversable<EnvVarLoaderInterface> */
25
    private \Traversable $loaders;
26
    /** @var \Traversable<EnvVarLoaderInterface> */
27
    private \Traversable $originalLoaders;
28
    private array $loadedVars = [];
29
30
    /**
31
     * @param \Traversable<EnvVarLoaderInterface>|null $loaders
32
     */
33
    public function __construct(
34
        private ContainerInterface $container,
35
        ?\Traversable $loaders = null,
36
    ) {
37
        $this->originalLoaders = $this->loaders = $loaders ?? new \ArrayIterator();
38
    }
39
40
    public static function getProvidedTypes(): array
41
    {
42
        return [
43
            'base64' => 'string',
44
            'bool' => 'bool',
45
            'not' => 'bool',
46
            'const' => 'bool|int|float|string|array',
47
            'csv' => 'array',
48
            'file' => 'string',
49
            'float' => 'float',
50
            'int' => 'int',
51
            'json' => 'array',
52
            'key' => 'bool|int|float|string|array',
53
            'url' => 'array',
54
            'query_string' => 'array',
55
            'resolve' => 'string',
56
            'default' => 'bool|int|float|string|array',
57
            'string' => 'string',
58
            'trim' => 'string',
59
            'require' => 'bool|int|float|string|array',
60
            'enum' => \BackedEnum::class,
0 ignored issues
show
Bug introduced by
The type BackedEnum was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
61
            'shuffle' => 'array',
62
            'defined' => 'bool',
63
            'urlencode' => 'string',
64
        ];
65
    }
66
67
    public function getEnv(string $prefix, string $name, \Closure $getEnv): mixed
68
    {
69
        $i = strpos($name, ':');
70
71
        if ('key' === $prefix) {
72
            if (false === $i) {
73
                throw new RuntimeException(\sprintf('Invalid env "key:%s": a key specifier should be provided.', $name));
74
            }
75
76
            $next = substr($name, $i + 1);
77
            $key = substr($name, 0, $i);
78
            $array = $getEnv($next);
79
80
            if (!\is_array($array)) {
81
                throw new RuntimeException(\sprintf('Resolved value of "%s" did not result in an array value.', $next));
82
            }
83
84
            if (!isset($array[$key]) && !\array_key_exists($key, $array)) {
85
                throw new EnvNotFoundException(\sprintf('Key "%s" not found in %s (resolved from "%s").', $key, json_encode($array), $next));
86
            }
87
88
            return $array[$key];
89
        }
90
91
        if ('enum' === $prefix) {
92
            if (false === $i) {
93
                throw new RuntimeException(\sprintf('Invalid env "enum:%s": a "%s" class-string should be provided.', $name, \BackedEnum::class));
94
            }
95
96
            $next = substr($name, $i + 1);
97
            $backedEnumClassName = substr($name, 0, $i);
98
            $backedEnumValue = $getEnv($next);
99
100
            if (!\is_string($backedEnumValue) && !\is_int($backedEnumValue)) {
101
                throw new RuntimeException(\sprintf('Resolved value of "%s" did not result in a string or int value.', $next));
102
            }
103
104
            if (!is_subclass_of($backedEnumClassName, \BackedEnum::class)) {
105
                throw new RuntimeException(\sprintf('"%s" is not a "%s".', $backedEnumClassName, \BackedEnum::class));
106
            }
107
108
            return $backedEnumClassName::tryFrom($backedEnumValue) ?? throw new RuntimeException(\sprintf('Enum value "%s" is not backed by "%s".', $backedEnumValue, $backedEnumClassName));
109
        }
110
111
        if ('defined' === $prefix) {
112
            try {
113
                return '' !== ($getEnv($name) ?? '');
114
            } catch (EnvNotFoundException) {
115
                return false;
116
            }
117
        }
118
119
        if ('default' === $prefix) {
120
            if (false === $i) {
121
                throw new RuntimeException(\sprintf('Invalid env "default:%s": a fallback parameter should be provided.', $name));
122
            }
123
124
            $next = substr($name, $i + 1);
125
            $default = substr($name, 0, $i);
126
127
            if ('' !== $default && !$this->container->hasParameter($default)) {
128
                throw new RuntimeException(\sprintf('Invalid env fallback in "default:%s": parameter "%s" not found.', $name, $default));
129
            }
130
131
            try {
132
                $env = $getEnv($next);
133
134
                if ('' !== $env && null !== $env) {
135
                    return $env;
136
                }
137
            } catch (EnvNotFoundException) {
138
                // no-op
139
            }
140
141
            return '' === $default ? null : $this->container->getParameter($default);
142
        }
143
144
        if ('file' === $prefix || 'require' === $prefix) {
145
            if (!\is_scalar($file = $getEnv($name))) {
146
                throw new RuntimeException(\sprintf('Invalid file name: env var "%s" is non-scalar.', $name));
147
            }
148
            if (!is_file($file)) {
149
                throw new EnvNotFoundException(\sprintf('File "%s" not found (resolved from "%s").', $file, $name));
150
            }
151
152
            if ('file' === $prefix) {
153
                return file_get_contents($file);
154
            }
155
156
            return require $file;
157
        }
158
159
        $returnNull = false;
160
        if ('' === $prefix) {
161
            if ('' === $name) {
162
                return null;
163
            }
164
            $returnNull = true;
165
            $prefix = 'string';
166
        }
167
168
        if (false !== $i || 'string' !== $prefix) {
169
            $env = $getEnv($name);
170
        } elseif ('' === ($env = $_ENV[$name] ?? (str_starts_with($name, 'HTTP_') ? null : ($_SERVER[$name] ?? null)))
171
            || (false !== $env && false === $env ??= getenv($name) ?? false) // null is a possible value because of thread safety issues
172
        ) {
173
            foreach ($this->loadedVars as $i => $vars) {
174
                if (false === $env = $vars[$name] ?? $env) {
175
                    continue;
176
                }
177
                if ($env instanceof \Stringable) {
178
                    $this->loadedVars[$i][$name] = $env = (string) $env;
179
                }
180
                if ('' !== ($env ?? '')) {
181
                    break;
182
                }
183
            }
184
185
            if (false === $env || '' === $env) {
186
                $loaders = $this->loaders;
187
                $this->loaders = new \ArrayIterator();
188
189
                try {
190
                    $i = 0;
191
                    $ended = true;
192
                    $count = $loaders instanceof \Countable ? $loaders->count() : 0;
193
                    foreach ($loaders as $loader) {
194
                        if (\count($this->loadedVars) > $i++) {
195
                            continue;
196
                        }
197
                        $this->loadedVars[] = $vars = $loader->loadEnvVars();
198
                        if (false === $env = $vars[$name] ?? $env) {
199
                            continue;
200
                        }
201
                        if ($env instanceof \Stringable) {
202
                            $this->loadedVars[array_key_last($this->loadedVars)][$name] = $env = (string) $env;
203
                        }
204
                        if ('' !== ($env ?? '')) {
205
                            $ended = false;
206
                            break;
207
                        }
208
                    }
209
                    if ($ended || $count === $i) {
210
                        $loaders = $this->loaders;
211
                    }
212
                } catch (ParameterCircularReferenceException) {
213
                    // skip loaders that need an env var that is not defined
214
                } finally {
215
                    $this->loaders = $loaders;
216
                }
217
            }
218
219
            if (false === $env) {
220
                if (!$this->container->hasParameter("env($name)")) {
221
                    throw new EnvNotFoundException(\sprintf('Environment variable not found: "%s".', $name));
222
                }
223
224
                $env = $this->container->getParameter("env($name)");
225
            }
226
        }
227
228
        if (null === $env) {
229
            if ($returnNull) {
230
                return null;
231
            }
232
233
            if (!isset(static::getProvidedTypes()[$prefix])) {
234
                throw new RuntimeException(\sprintf('Unsupported env var prefix "%s".', $prefix));
235
            }
236
237
            if (!\in_array($prefix, ['string', 'bool', 'not', 'int', 'float'], true)) {
238
                return null;
239
            }
240
        }
241
242
        if ('shuffle' === $prefix) {
243
            \is_array($env) ? shuffle($env) : throw new RuntimeException(\sprintf('Env var "%s" cannot be shuffled, expected array, got "%s".', $name, get_debug_type($env)));
244
245
            return $env;
246
        }
247
248
        if (null !== $env && !\is_scalar($env)) {
249
            throw new RuntimeException(\sprintf('Non-scalar env var "%s" cannot be cast to "%s".', $name, $prefix));
250
        }
251
252
        if ('string' === $prefix) {
253
            return (string) $env;
254
        }
255
256
        if (\in_array($prefix, ['bool', 'not'], true)) {
257
            $env = (bool) (filter_var($env, \FILTER_VALIDATE_BOOL) ?: filter_var($env, \FILTER_VALIDATE_INT) ?: filter_var($env, \FILTER_VALIDATE_FLOAT));
258
259
            return 'not' === $prefix xor $env;
260
        }
261
262
        if ('int' === $prefix) {
263
            if (null !== $env && false === $env = filter_var($env, \FILTER_VALIDATE_INT) ?: filter_var($env, \FILTER_VALIDATE_FLOAT)) {
264
                throw new RuntimeException(\sprintf('Non-numeric env var "%s" cannot be cast to int.', $name));
265
            }
266
267
            return (int) $env;
268
        }
269
270
        if ('float' === $prefix) {
271
            if (null !== $env && false === $env = filter_var($env, \FILTER_VALIDATE_FLOAT)) {
272
                throw new RuntimeException(\sprintf('Non-numeric env var "%s" cannot be cast to float.', $name));
273
            }
274
275
            return (float) $env;
276
        }
277
278
        if ('const' === $prefix) {
279
            if (!\defined($env)) {
280
                throw new RuntimeException(\sprintf('Env var "%s" maps to undefined constant "%s".', $name, $env));
281
            }
282
283
            return \constant($env);
284
        }
285
286
        if ('base64' === $prefix) {
287
            return base64_decode(strtr($env, '-_', '+/'));
288
        }
289
290
        if ('json' === $prefix) {
291
            $env = json_decode($env, true);
292
293
            if (\JSON_ERROR_NONE !== json_last_error()) {
294
                throw new RuntimeException(\sprintf('Invalid JSON in env var "%s": ', $name).json_last_error_msg());
295
            }
296
297
            if (null !== $env && !\is_array($env)) {
298
                throw new RuntimeException(\sprintf('Invalid JSON env var "%s": array or null expected, "%s" given.', $name, get_debug_type($env)));
299
            }
300
301
            return $env;
302
        }
303
304
        if ('url' === $prefix) {
305
            $params = parse_url($env);
306
307
            if (false === $params) {
308
                throw new RuntimeException(\sprintf('Invalid URL in env var "%s".', $name));
309
            }
310
            if (!isset($params['scheme'], $params['host'])) {
311
                throw new RuntimeException(\sprintf('Invalid URL in env var "%s": scheme and host expected.', $name));
312
            }
313
            if (('\\' !== \DIRECTORY_SEPARATOR || 'file' !== $params['scheme']) && false !== ($i = strpos($env, '\\')) && $i < strcspn($env, '?#')) {
314
                throw new RuntimeException(\sprintf('Invalid URL in env var "%s": backslashes are not allowed.', $name));
315
            }
316
            if (\ord($env[0]) <= 32 || \ord($env[-1]) <= 32 || \strlen($env) !== strcspn($env, "\r\n\t")) {
317
                throw new RuntimeException(\sprintf('Invalid URL in env var "%s": leading/trailing ASCII control characters or whitespaces are not allowed.', $name));
318
            }
319
            $params += [
320
                'port' => null,
321
                'user' => null,
322
                'pass' => null,
323
                'path' => null,
324
                'query' => null,
325
                'fragment' => null,
326
            ];
327
328
            $params['user'] = null !== $params['user'] ? rawurldecode($params['user']) : null;
329
            $params['pass'] = null !== $params['pass'] ? rawurldecode($params['pass']) : null;
330
331
            // remove the '/' separator
332
            $params['path'] = '/' === ($params['path'] ?? '/') ? '' : substr($params['path'], 1);
333
334
            return $params;
335
        }
336
337
        if ('query_string' === $prefix) {
338
            $queryString = parse_url($env, \PHP_URL_QUERY) ?: $env;
339
            parse_str($queryString, $result);
340
341
            return $result;
342
        }
343
344
        if ('resolve' === $prefix) {
345
            return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($name, $getEnv) {
346
                if (!isset($match[1])) {
347
                    return '%';
348
                }
349
350
                if (str_starts_with($match[1], 'env(') && str_ends_with($match[1], ')') && 'env()' !== $match[1]) {
351
                    $value = $getEnv(substr($match[1], 4, -1));
352
                } else {
353
                    $value = $this->container->getParameter($match[1]);
354
                }
355
356
                if (!\is_scalar($value)) {
357
                    throw new RuntimeException(\sprintf('Parameter "%s" found when resolving env var "%s" must be scalar, "%s" given.', $match[1], $name, get_debug_type($value)));
358
                }
359
360
                return $value;
361
            }, $env);
362
        }
363
364
        if ('csv' === $prefix) {
365
            return '' === $env ? [] : str_getcsv($env, ',', '"', '');
366
        }
367
368
        if ('trim' === $prefix) {
369
            return trim($env);
370
        }
371
372
        if ('urlencode' === $prefix) {
373
            return rawurlencode($env);
374
        }
375
376
        throw new RuntimeException(\sprintf('Unsupported env var prefix "%s" for env name "%s".', $prefix, $name));
377
    }
378
379
    public function reset(): void
380
    {
381
        $this->loadedVars = [];
382
        $this->loaders = $this->originalLoaders;
383
384
        if ($this->container instanceof Container) {
385
            $this->container->resetEnvCache();
0 ignored issues
show
Bug introduced by
The method resetEnvCache() does not exist on Symfony\Component\Depend...tion\ContainerInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Symfony\Component\Depend...aggedContainerInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

385
            $this->container->/** @scrutinizer ignore-call */ 
386
                              resetEnvCache();
Loading history...
386
        }
387
    }
388
}
389