2016-10-22 17:25:15 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\DependencyInjection;
|
|
|
|
|
|
|
|
use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
|
2018-09-21 12:50:11 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Exception\LogicException;
|
2016-10-22 17:25:15 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
|
|
|
|
2017-09-22 12:07:23 +01:00
|
|
|
/**
|
|
|
|
* @author Nicolas Grekas <p@tchwork.com>
|
|
|
|
*/
|
2016-10-22 17:25:15 +01:00
|
|
|
class EnvVarProcessor implements EnvVarProcessorInterface
|
|
|
|
{
|
|
|
|
private $container;
|
|
|
|
|
|
|
|
public function __construct(ContainerInterface $container)
|
|
|
|
{
|
|
|
|
$this->container = $container;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public static function getProvidedTypes()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'base64' => 'string',
|
|
|
|
'bool' => 'bool',
|
|
|
|
'const' => 'bool|int|float|string|array',
|
2017-12-29 17:26:27 +00:00
|
|
|
'csv' => 'array',
|
2016-10-22 17:25:15 +01:00
|
|
|
'file' => 'string',
|
|
|
|
'float' => 'float',
|
|
|
|
'int' => 'int',
|
|
|
|
'json' => 'array',
|
2018-05-04 17:10:27 +01:00
|
|
|
'key' => 'bool|int|float|string|array',
|
2016-10-22 17:25:15 +01:00
|
|
|
'resolve' => 'string',
|
2018-10-25 08:05:21 +01:00
|
|
|
'default' => 'bool|int|float|string|array',
|
2016-10-22 17:25:15 +01:00
|
|
|
'string' => 'string',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function getEnv($prefix, $name, \Closure $getEnv)
|
|
|
|
{
|
|
|
|
$i = strpos($name, ':');
|
|
|
|
|
2018-05-04 17:10:27 +01:00
|
|
|
if ('key' === $prefix) {
|
|
|
|
if (false === $i) {
|
2018-10-25 08:05:21 +01:00
|
|
|
throw new RuntimeException(sprintf('Invalid env "key:%s": a key specifier should be provided.', $name));
|
2018-05-04 17:10:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$next = substr($name, $i + 1);
|
|
|
|
$key = substr($name, 0, $i);
|
|
|
|
$array = $getEnv($next);
|
|
|
|
|
2018-07-26 09:59:12 +01:00
|
|
|
if (!\is_array($array)) {
|
2018-05-04 17:10:27 +01:00
|
|
|
throw new RuntimeException(sprintf('Resolved value of "%s" did not result in an array value.', $next));
|
|
|
|
}
|
2018-10-25 08:05:21 +01:00
|
|
|
|
|
|
|
if (!isset($array[$key]) && !array_key_exists($key, $array)) {
|
|
|
|
throw new EnvNotFoundException(sprintf('Key "%s" not found in "%s" (resolved from "%s").', $key, json_encode($array), $next));
|
2018-05-04 17:10:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $array[$key];
|
|
|
|
}
|
|
|
|
|
2018-10-25 08:05:21 +01:00
|
|
|
if ('default' === $prefix) {
|
|
|
|
if (false === $i) {
|
|
|
|
throw new RuntimeException(sprintf('Invalid env "default:%s": a fallback parameter should be provided.', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
$next = substr($name, $i + 1);
|
|
|
|
$default = substr($name, 0, $i);
|
|
|
|
|
|
|
|
if (!$this->container->hasParameter($default)) {
|
|
|
|
throw new RuntimeException(sprintf('Invalid env fallback in "default:%s": parameter "%s" not found.', $name, $default));
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return $getEnv($next);
|
|
|
|
} catch (EnvNotFoundException $e) {
|
|
|
|
return $this->container->getParameter($default);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-22 17:25:15 +01:00
|
|
|
if ('file' === $prefix) {
|
|
|
|
if (!is_scalar($file = $getEnv($name))) {
|
|
|
|
throw new RuntimeException(sprintf('Invalid file name: env var "%s" is non-scalar.', $name));
|
|
|
|
}
|
|
|
|
if (!file_exists($file)) {
|
2018-10-25 08:05:21 +01:00
|
|
|
throw new EnvNotFoundException(sprintf('File "%s" not found (resolved from "%s").', $file, $name));
|
2016-10-22 17:25:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return file_get_contents($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (false !== $i || 'string' !== $prefix) {
|
|
|
|
if (null === $env = $getEnv($name)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} elseif (isset($_ENV[$name])) {
|
|
|
|
$env = $_ENV[$name];
|
2017-10-28 17:49:05 +01:00
|
|
|
} elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
|
|
|
|
$env = $_SERVER[$name];
|
2016-10-22 17:25:15 +01:00
|
|
|
} elseif (false === ($env = getenv($name)) || null === $env) { // null is a possible value because of thread safety issues
|
|
|
|
if (!$this->container->hasParameter("env($name)")) {
|
2018-10-25 08:05:21 +01:00
|
|
|
throw new EnvNotFoundException(sprintf('Environment variable not found: "%s".', $name));
|
2016-10-22 17:25:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $env = $this->container->getParameter("env($name)")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_scalar($env)) {
|
|
|
|
throw new RuntimeException(sprintf('Non-scalar env var "%s" cannot be cast to %s.', $name, $prefix));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('string' === $prefix) {
|
|
|
|
return (string) $env;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('bool' === $prefix) {
|
2018-10-31 11:40:33 +00:00
|
|
|
return (bool) (filter_var($env, FILTER_VALIDATE_BOOLEAN) ?: filter_var($env, FILTER_VALIDATE_INT) ?: filter_var($env, FILTER_VALIDATE_FLOAT));
|
2016-10-22 17:25:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ('int' === $prefix) {
|
2018-10-31 11:40:33 +00:00
|
|
|
if (false === $env = filter_var($env, FILTER_VALIDATE_INT) ?: filter_var($env, FILTER_VALIDATE_FLOAT)) {
|
2016-10-22 17:25:15 +01:00
|
|
|
throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to int.', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int) $env;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('float' === $prefix) {
|
2018-10-31 11:40:33 +00:00
|
|
|
if (false === $env = filter_var($env, FILTER_VALIDATE_FLOAT)) {
|
2016-10-22 17:25:15 +01:00
|
|
|
throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to float.', $name));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (float) $env;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('const' === $prefix) {
|
2018-07-26 09:45:46 +01:00
|
|
|
if (!\defined($env)) {
|
2016-10-22 17:25:15 +01:00
|
|
|
throw new RuntimeException(sprintf('Env var "%s" maps to undefined constant "%s".', $name, $env));
|
|
|
|
}
|
|
|
|
|
2018-07-26 09:45:46 +01:00
|
|
|
return \constant($env);
|
2016-10-22 17:25:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ('base64' === $prefix) {
|
|
|
|
return base64_decode($env);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('json' === $prefix) {
|
2017-09-22 11:57:45 +01:00
|
|
|
$env = json_decode($env, true);
|
2016-10-22 17:25:15 +01:00
|
|
|
|
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) {
|
|
|
|
throw new RuntimeException(sprintf('Invalid JSON in env var "%s": '.json_last_error_msg(), $name));
|
|
|
|
}
|
|
|
|
|
2018-07-26 09:55:25 +01:00
|
|
|
if (null !== $env && !\is_array($env)) {
|
|
|
|
throw new RuntimeException(sprintf('Invalid JSON env var "%s": array or null expected, %s given.', $name, \gettype($env)));
|
2016-10-22 17:25:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $env;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('resolve' === $prefix) {
|
|
|
|
return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($name) {
|
|
|
|
if (!isset($match[1])) {
|
|
|
|
return '%';
|
|
|
|
}
|
|
|
|
$value = $this->container->getParameter($match[1]);
|
|
|
|
if (!is_scalar($value)) {
|
2018-07-26 09:45:46 +01:00
|
|
|
throw new RuntimeException(sprintf('Parameter "%s" found when resolving env var "%s" must be scalar, "%s" given.', $match[1], $name, \gettype($value)));
|
2016-10-22 17:25:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}, $env);
|
|
|
|
}
|
|
|
|
|
2017-12-29 17:26:27 +00:00
|
|
|
if ('csv' === $prefix) {
|
|
|
|
return str_getcsv($env);
|
|
|
|
}
|
|
|
|
|
2016-10-22 17:25:15 +01:00
|
|
|
throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
|
|
|
|
}
|
|
|
|
}
|