/src/Symfony/Component/DependencyInjection/EnvVarProcessor.php

https://github.com/deviantintegral/symfony · PHP · 168 lines · 120 code · 31 blank · 17 comment · 28 complexity · 626a22754dd48981363ea3b0c88bc9f4 MD5 · raw file

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