annotate vendor/symfony/dependency-injection/EnvVarProcessor.php @ 5:12f9dff5fda9 tip

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