annotate vendor/symfony/dependency-injection/EnvVarProcessor.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@14 1 <?php
Chris@14 2
Chris@14 3 /*
Chris@14 4 * This file is part of the Symfony package.
Chris@14 5 *
Chris@14 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@14 7 *
Chris@14 8 * For the full copyright and license information, please view the LICENSE
Chris@14 9 * file that was distributed with this source code.
Chris@14 10 */
Chris@14 11
Chris@14 12 namespace Symfony\Component\DependencyInjection;
Chris@14 13
Chris@14 14 use Symfony\Component\Config\Util\XmlUtils;
Chris@14 15 use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
Chris@14 16 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
Chris@14 17
Chris@14 18 /**
Chris@14 19 * @author Nicolas Grekas <p@tchwork.com>
Chris@14 20 */
Chris@14 21 class EnvVarProcessor implements EnvVarProcessorInterface
Chris@14 22 {
Chris@14 23 private $container;
Chris@14 24
Chris@14 25 public function __construct(ContainerInterface $container)
Chris@14 26 {
Chris@14 27 $this->container = $container;
Chris@14 28 }
Chris@14 29
Chris@14 30 /**
Chris@14 31 * {@inheritdoc}
Chris@14 32 */
Chris@14 33 public static function getProvidedTypes()
Chris@14 34 {
Chris@17 35 return [
Chris@14 36 'base64' => 'string',
Chris@14 37 'bool' => 'bool',
Chris@14 38 'const' => 'bool|int|float|string|array',
Chris@14 39 'file' => 'string',
Chris@14 40 'float' => 'float',
Chris@14 41 'int' => 'int',
Chris@14 42 'json' => 'array',
Chris@14 43 'resolve' => 'string',
Chris@14 44 'string' => 'string',
Chris@17 45 ];
Chris@14 46 }
Chris@14 47
Chris@14 48 /**
Chris@14 49 * {@inheritdoc}
Chris@14 50 */
Chris@14 51 public function getEnv($prefix, $name, \Closure $getEnv)
Chris@14 52 {
Chris@14 53 $i = strpos($name, ':');
Chris@14 54
Chris@14 55 if ('file' === $prefix) {
Chris@14 56 if (!is_scalar($file = $getEnv($name))) {
Chris@14 57 throw new RuntimeException(sprintf('Invalid file name: env var "%s" is non-scalar.', $name));
Chris@14 58 }
Chris@14 59 if (!file_exists($file)) {
Chris@14 60 throw new RuntimeException(sprintf('Env "file:%s" not found: %s does not exist.', $name, $file));
Chris@14 61 }
Chris@14 62
Chris@14 63 return file_get_contents($file);
Chris@14 64 }
Chris@14 65
Chris@14 66 if (false !== $i || 'string' !== $prefix) {
Chris@14 67 if (null === $env = $getEnv($name)) {
Chris@14 68 return;
Chris@14 69 }
Chris@14 70 } elseif (isset($_ENV[$name])) {
Chris@14 71 $env = $_ENV[$name];
Chris@14 72 } elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
Chris@14 73 $env = $_SERVER[$name];
Chris@14 74 } elseif (false === ($env = getenv($name)) || null === $env) { // null is a possible value because of thread safety issues
Chris@14 75 if (!$this->container->hasParameter("env($name)")) {
Chris@14 76 throw new EnvNotFoundException($name);
Chris@14 77 }
Chris@14 78
Chris@14 79 if (null === $env = $this->container->getParameter("env($name)")) {
Chris@14 80 return;
Chris@14 81 }
Chris@14 82 }
Chris@14 83
Chris@14 84 if (!is_scalar($env)) {
Chris@14 85 throw new RuntimeException(sprintf('Non-scalar env var "%s" cannot be cast to %s.', $name, $prefix));
Chris@14 86 }
Chris@14 87
Chris@14 88 if ('string' === $prefix) {
Chris@14 89 return (string) $env;
Chris@14 90 }
Chris@14 91
Chris@14 92 if ('bool' === $prefix) {
Chris@14 93 return (bool) self::phpize($env);
Chris@14 94 }
Chris@14 95
Chris@14 96 if ('int' === $prefix) {
Chris@14 97 if (!is_numeric($env = self::phpize($env))) {
Chris@14 98 throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to int.', $name));
Chris@14 99 }
Chris@14 100
Chris@14 101 return (int) $env;
Chris@14 102 }
Chris@14 103
Chris@14 104 if ('float' === $prefix) {
Chris@14 105 if (!is_numeric($env = self::phpize($env))) {
Chris@14 106 throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to float.', $name));
Chris@14 107 }
Chris@14 108
Chris@14 109 return (float) $env;
Chris@14 110 }
Chris@14 111
Chris@14 112 if ('const' === $prefix) {
Chris@17 113 if (!\defined($env)) {
Chris@14 114 throw new RuntimeException(sprintf('Env var "%s" maps to undefined constant "%s".', $name, $env));
Chris@14 115 }
Chris@14 116
Chris@17 117 return \constant($env);
Chris@14 118 }
Chris@14 119
Chris@14 120 if ('base64' === $prefix) {
Chris@14 121 return base64_decode($env);
Chris@14 122 }
Chris@14 123
Chris@14 124 if ('json' === $prefix) {
Chris@14 125 $env = json_decode($env, true);
Chris@14 126
Chris@14 127 if (JSON_ERROR_NONE !== json_last_error()) {
Chris@14 128 throw new RuntimeException(sprintf('Invalid JSON in env var "%s": '.json_last_error_msg(), $name));
Chris@14 129 }
Chris@14 130
Chris@17 131 if (!\is_array($env)) {
Chris@17 132 throw new RuntimeException(sprintf('Invalid JSON env var "%s": array expected, %s given.', $name, \gettype($env)));
Chris@14 133 }
Chris@14 134
Chris@14 135 return $env;
Chris@14 136 }
Chris@14 137
Chris@14 138 if ('resolve' === $prefix) {
Chris@14 139 return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($name) {
Chris@14 140 if (!isset($match[1])) {
Chris@14 141 return '%';
Chris@14 142 }
Chris@14 143 $value = $this->container->getParameter($match[1]);
Chris@14 144 if (!is_scalar($value)) {
Chris@17 145 throw new RuntimeException(sprintf('Parameter "%s" found when resolving env var "%s" must be scalar, "%s" given.', $match[1], $name, \gettype($value)));
Chris@14 146 }
Chris@14 147
Chris@14 148 return $value;
Chris@14 149 }, $env);
Chris@14 150 }
Chris@14 151
Chris@14 152 throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
Chris@14 153 }
Chris@14 154
Chris@14 155 private static function phpize($value)
Chris@14 156 {
Chris@14 157 if (!class_exists(XmlUtils::class)) {
Chris@14 158 throw new RuntimeException('The Symfony Config component is required to cast env vars to "bool", "int" or "float".');
Chris@14 159 }
Chris@14 160
Chris@14 161 return XmlUtils::phpize($value);
Chris@14 162 }
Chris@14 163 }