|
| 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 | +class EnvVarProcessor implements EnvVarProcessorInterface |
| 19 | +{ |
| 20 | + private $container; |
| 21 | + |
| 22 | + public function __construct(ContainerInterface $container) |
| 23 | + { |
| 24 | + $this->container = $container; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * {@inheritdoc} |
| 29 | + */ |
| 30 | + public static function getProvidedTypes() |
| 31 | + { |
| 32 | + return array( |
| 33 | + 'base64' => 'string', |
| 34 | + 'bool' => 'bool', |
| 35 | + 'const' => 'bool|int|float|string|array', |
| 36 | + 'file' => 'string', |
| 37 | + 'float' => 'float', |
| 38 | + 'int' => 'int', |
| 39 | + 'json' => 'array', |
| 40 | + 'resolve' => 'string', |
| 41 | + 'string' => 'string', |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * {@inheritdoc} |
| 47 | + */ |
| 48 | + public function getEnv($prefix, $name, \Closure $getEnv) |
| 49 | + { |
| 50 | + $i = strpos($name, ':'); |
| 51 | + |
| 52 | + if ('file' === $prefix) { |
| 53 | + if (!is_scalar($file = $getEnv($name))) { |
| 54 | + throw new RuntimeException(sprintf('Invalid file name: env var "%s" is non-scalar.', $name)); |
| 55 | + } |
| 56 | + if (!file_exists($file)) { |
| 57 | + throw new RuntimeException(sprintf('Env "file:%s" not found: %s does not exist.', $name, $file)); |
| 58 | + } |
| 59 | + |
| 60 | + return file_get_contents($file); |
| 61 | + } |
| 62 | + |
| 63 | + if (false !== $i || 'string' !== $prefix) { |
| 64 | + if (null === $env = $getEnv($name)) { |
| 65 | + return; |
| 66 | + } |
| 67 | + } elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) { |
| 68 | + $env = $_SERVER[$name]; |
| 69 | + } elseif (isset($_ENV[$name])) { |
| 70 | + $env = $_ENV[$name]; |
| 71 | + } elseif (false === ($env = getenv($name)) || null === $env) { // null is a possible value because of thread safety issues |
| 72 | + if (!$this->container->hasParameter("env($name)")) { |
| 73 | + throw new EnvNotFoundException($name); |
| 74 | + } |
| 75 | + |
| 76 | + if (null === $env = $this->container->getParameter("env($name)")) { |
| 77 | + return; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if (!is_scalar($env)) { |
| 82 | + throw new RuntimeException(sprintf('Non-scalar env var "%s" cannot be cast to %s.', $name, $prefix)); |
| 83 | + } |
| 84 | + |
| 85 | + if ('string' === $prefix) { |
| 86 | + return (string) $env; |
| 87 | + } |
| 88 | + |
| 89 | + if ('bool' === $prefix) { |
| 90 | + return (bool) self::phpize($env); |
| 91 | + } |
| 92 | + |
| 93 | + if ('int' === $prefix) { |
| 94 | + if (!is_numeric($env = self::phpize($env))) { |
| 95 | + throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to int.', $name)); |
| 96 | + } |
| 97 | + |
| 98 | + return (int) $env; |
| 99 | + } |
| 100 | + |
| 101 | + if ('float' === $prefix) { |
| 102 | + if (!is_numeric($env = self::phpize($env))) { |
| 103 | + throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to float.', $name)); |
| 104 | + } |
| 105 | + |
| 106 | + return (float) $env; |
| 107 | + } |
| 108 | + |
| 109 | + if ('const' === $prefix) { |
| 110 | + if (!defined($env)) { |
| 111 | + throw new RuntimeException(sprintf('Env var "%s" maps to undefined constant "%s".', $name, $env)); |
| 112 | + } |
| 113 | + |
| 114 | + return constant($name); |
| 115 | + } |
| 116 | + |
| 117 | + if ('base64' === $prefix) { |
| 118 | + return base64_decode($env); |
| 119 | + } |
| 120 | + |
| 121 | + if ('json' === $prefix) { |
| 122 | + $env = json_decode($env, true, JSON_BIGINT_AS_STRING); |
| 123 | + |
| 124 | + if (JSON_ERROR_NONE !== json_last_error()) { |
| 125 | + throw new RuntimeException(sprintf('Invalid JSON in env var "%s": '.json_last_error_msg(), $name)); |
| 126 | + } |
| 127 | + |
| 128 | + if (!is_array($env)) { |
| 129 | + throw new RuntimeException(sprintf('Invalid JSON env var "%s": array expected, %s given.', $name, gettype($env))); |
| 130 | + } |
| 131 | + |
| 132 | + return $env; |
| 133 | + } |
| 134 | + |
| 135 | + if ('resolve' === $prefix) { |
| 136 | + return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($name) { |
| 137 | + if (!isset($match[1])) { |
| 138 | + return '%'; |
| 139 | + } |
| 140 | + $value = $this->container->getParameter($match[1]); |
| 141 | + if (!is_scalar($value)) { |
| 142 | + throw new RuntimeException(sprintf('Parameter "%s" found when resolving env var "%s" must be scalar, "%s" given.', $match[1], $name, gettype($value))); |
| 143 | + } |
| 144 | + |
| 145 | + return $value; |
| 146 | + }, $env); |
| 147 | + } |
| 148 | + |
| 149 | + throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix)); |
| 150 | + } |
| 151 | + |
| 152 | + private static function phpize($value) |
| 153 | + { |
| 154 | + if (!class_exists(XmlUtils::class)) { |
| 155 | + throw new RuntimeException('The Symfony Config component is required to cast env vars to "bool", "int" or "float".'); |
| 156 | + } |
| 157 | + |
| 158 | + return XmlUtils::phpize($value); |
| 159 | + } |
| 160 | +} |
0 commit comments