Chris@14: Chris@14: * Chris@14: * For the full copyright and license information, please view the LICENSE Chris@14: * file that was distributed with this source code. Chris@14: */ Chris@14: Chris@14: namespace Symfony\Component\DependencyInjection; Chris@14: Chris@14: use Symfony\Component\Config\Util\XmlUtils; Chris@14: use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException; Chris@14: use Symfony\Component\DependencyInjection\Exception\RuntimeException; Chris@14: Chris@14: /** Chris@14: * @author Nicolas Grekas
Chris@14: */ Chris@14: class EnvVarProcessor implements EnvVarProcessorInterface Chris@14: { Chris@14: private $container; Chris@14: Chris@14: public function __construct(ContainerInterface $container) Chris@14: { Chris@14: $this->container = $container; Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public static function getProvidedTypes() Chris@14: { Chris@17: return [ Chris@14: 'base64' => 'string', Chris@14: 'bool' => 'bool', Chris@14: 'const' => 'bool|int|float|string|array', Chris@14: 'file' => 'string', Chris@14: 'float' => 'float', Chris@14: 'int' => 'int', Chris@14: 'json' => 'array', Chris@14: 'resolve' => 'string', Chris@14: 'string' => 'string', Chris@17: ]; Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function getEnv($prefix, $name, \Closure $getEnv) Chris@14: { Chris@14: $i = strpos($name, ':'); Chris@14: Chris@14: if ('file' === $prefix) { Chris@14: if (!is_scalar($file = $getEnv($name))) { Chris@14: throw new RuntimeException(sprintf('Invalid file name: env var "%s" is non-scalar.', $name)); Chris@14: } Chris@14: if (!file_exists($file)) { Chris@14: throw new RuntimeException(sprintf('Env "file:%s" not found: %s does not exist.', $name, $file)); Chris@14: } Chris@14: Chris@14: return file_get_contents($file); Chris@14: } Chris@14: Chris@14: if (false !== $i || 'string' !== $prefix) { Chris@14: if (null === $env = $getEnv($name)) { Chris@14: return; Chris@14: } Chris@14: } elseif (isset($_ENV[$name])) { Chris@14: $env = $_ENV[$name]; Chris@14: } elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) { Chris@14: $env = $_SERVER[$name]; Chris@14: } elseif (false === ($env = getenv($name)) || null === $env) { // null is a possible value because of thread safety issues Chris@14: if (!$this->container->hasParameter("env($name)")) { Chris@14: throw new EnvNotFoundException($name); Chris@14: } Chris@14: Chris@14: if (null === $env = $this->container->getParameter("env($name)")) { Chris@14: return; Chris@14: } Chris@14: } Chris@14: Chris@14: if (!is_scalar($env)) { Chris@14: throw new RuntimeException(sprintf('Non-scalar env var "%s" cannot be cast to %s.', $name, $prefix)); Chris@14: } Chris@14: Chris@14: if ('string' === $prefix) { Chris@14: return (string) $env; Chris@14: } Chris@14: Chris@14: if ('bool' === $prefix) { Chris@14: return (bool) self::phpize($env); Chris@14: } Chris@14: Chris@14: if ('int' === $prefix) { Chris@14: if (!is_numeric($env = self::phpize($env))) { Chris@14: throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to int.', $name)); Chris@14: } Chris@14: Chris@14: return (int) $env; Chris@14: } Chris@14: Chris@14: if ('float' === $prefix) { Chris@14: if (!is_numeric($env = self::phpize($env))) { Chris@14: throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to float.', $name)); Chris@14: } Chris@14: Chris@14: return (float) $env; Chris@14: } Chris@14: Chris@14: if ('const' === $prefix) { Chris@17: if (!\defined($env)) { Chris@14: throw new RuntimeException(sprintf('Env var "%s" maps to undefined constant "%s".', $name, $env)); Chris@14: } Chris@14: Chris@17: return \constant($env); Chris@14: } Chris@14: Chris@14: if ('base64' === $prefix) { Chris@14: return base64_decode($env); Chris@14: } Chris@14: Chris@14: if ('json' === $prefix) { Chris@14: $env = json_decode($env, true); Chris@14: Chris@14: if (JSON_ERROR_NONE !== json_last_error()) { Chris@14: throw new RuntimeException(sprintf('Invalid JSON in env var "%s": '.json_last_error_msg(), $name)); Chris@14: } Chris@14: Chris@17: if (!\is_array($env)) { Chris@17: throw new RuntimeException(sprintf('Invalid JSON env var "%s": array expected, %s given.', $name, \gettype($env))); Chris@14: } Chris@14: Chris@14: return $env; Chris@14: } Chris@14: Chris@14: if ('resolve' === $prefix) { Chris@14: return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($name) { Chris@14: if (!isset($match[1])) { Chris@14: return '%'; Chris@14: } Chris@14: $value = $this->container->getParameter($match[1]); Chris@14: if (!is_scalar($value)) { Chris@17: throw new RuntimeException(sprintf('Parameter "%s" found when resolving env var "%s" must be scalar, "%s" given.', $match[1], $name, \gettype($value))); Chris@14: } Chris@14: Chris@14: return $value; Chris@14: }, $env); Chris@14: } Chris@14: Chris@14: throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix)); Chris@14: } Chris@14: Chris@14: private static function phpize($value) Chris@14: { Chris@14: if (!class_exists(XmlUtils::class)) { Chris@14: throw new RuntimeException('The Symfony Config component is required to cast env vars to "bool", "int" or "float".'); Chris@14: } Chris@14: Chris@14: return XmlUtils::phpize($value); Chris@14: } Chris@14: }