4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection;
14 use Symfony\Component\Config\Util\XmlUtils;
15 use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
16 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
19 * @author Nicolas Grekas <p@tchwork.com>
21 class EnvVarProcessor implements EnvVarProcessorInterface
25 public function __construct(ContainerInterface $container)
27 $this->container = $container;
33 public static function getProvidedTypes()
38 'const' => 'bool|int|float|string|array',
43 'resolve' => 'string',
51 public function getEnv($prefix, $name, \Closure $getEnv)
53 $i = strpos($name, ':');
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));
59 if (!file_exists($file)) {
60 throw new RuntimeException(sprintf('Env "file:%s" not found: %s does not exist.', $name, $file));
63 return file_get_contents($file);
66 if (false !== $i || 'string' !== $prefix) {
67 if (null === $env = $getEnv($name)) {
70 } elseif (isset($_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);
79 if (null === $env = $this->container->getParameter("env($name)")) {
84 if (!is_scalar($env)) {
85 throw new RuntimeException(sprintf('Non-scalar env var "%s" cannot be cast to %s.', $name, $prefix));
88 if ('string' === $prefix) {
92 if ('bool' === $prefix) {
93 return (bool) self::phpize($env);
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));
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));
112 if ('const' === $prefix) {
113 if (!\defined($env)) {
114 throw new RuntimeException(sprintf('Env var "%s" maps to undefined constant "%s".', $name, $env));
117 return \constant($env);
120 if ('base64' === $prefix) {
121 return base64_decode($env);
124 if ('json' === $prefix) {
125 $env = json_decode($env, true);
127 if (JSON_ERROR_NONE !== json_last_error()) {
128 throw new RuntimeException(sprintf('Invalid JSON in env var "%s": '.json_last_error_msg(), $name));
131 if (!\is_array($env)) {
132 throw new RuntimeException(sprintf('Invalid JSON env var "%s": array expected, %s given.', $name, \gettype($env)));
138 if ('resolve' === $prefix) {
139 return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($name) {
140 if (!isset($match[1])) {
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)));
152 throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
155 private static function phpize($value)
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".');
161 return XmlUtils::phpize($value);