Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
EnvVarProcessor.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\DependencyInjection;
013
014 use Symfony\Component\Config\Util\XmlUtils;
015 use Symfony\Component\DependencyInjection\Exception\EnvNotFoundException;
016 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
017
018 /**
019 * @author Nicolas Grekas <p@tchwork.com>
020 */
021 class EnvVarProcessor implements EnvVarProcessorInterface
022 {
023 private $container;
024
025 public function __construct(ContainerInterface $container)
026 {
027 $this->container = $container;
028 }
029
030 /**
031 * {@inheritdoc}
032 */
033 public static function getProvidedTypes()
034 {
035 return [
036 'base64' => 'string',
037 'bool' => 'bool',
038 'const' => 'bool|int|float|string|array',
039 'file' => 'string',
040 'float' => 'float',
041 'int' => 'int',
042 'json' => 'array',
043 'resolve' => 'string',
044 'string' => 'string',
045 ];
046 }
047
048 /**
049 * {@inheritdoc}
050 */
051 public function getEnv($prefix, $name, \Closure $getEnv)
052 {
053 $i = strpos($name, ':');
054
055 if ('file' === $prefix) {
056 if (!is_scalar($file = $getEnv($name))) {
057 throw new RuntimeException(sprintf('Invalid file name: env var "%s" is non-scalar.', $name));
058 }
059 if (!file_exists($file)) {
060 throw new RuntimeException(sprintf('Env "file:%s" not found: "%s" does not exist.', $name, $file));
061 }
062
063 return file_get_contents($file);
064 }
065
066 if (false !== $i || 'string' !== $prefix) {
067 if (null === $env = $getEnv($name)) {
068 return null;
069 }
070 } elseif (isset($_ENV[$name])) {
071 $env = $_ENV[$name];
072 } elseif (isset($_SERVER[$name]) && 0 !== strpos($name, 'HTTP_')) {
073 $env = $_SERVER[$name];
074 } elseif (false === ($env = getenv($name)) || null === $env) { // null is a possible value because of thread safety issues
075 if (!$this->container->hasParameter("env($name)")) {
076 throw new EnvNotFoundException($name);
077 }
078
079 if (null === $env = $this->container->getParameter("env($name)")) {
080 return null;
081 }
082 }
083
084 if (!is_scalar($env)) {
085 throw new RuntimeException(sprintf('Non-scalar env var "%s" cannot be cast to "%s".', $name, $prefix));
086 }
087
088 if ('string' === $prefix) {
089 return (string) $env;
090 }
091
092 if ('bool' === $prefix) {
093 return (bool) self::phpize($env);
094 }
095
096 if ('int' === $prefix) {
097 if (!is_numeric($env = self::phpize($env))) {
098 throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to int.', $name));
099 }
100
101 return (int) $env;
102 }
103
104 if ('float' === $prefix) {
105 if (!is_numeric($env = self::phpize($env))) {
106 throw new RuntimeException(sprintf('Non-numeric env var "%s" cannot be cast to float.', $name));
107 }
108
109 return (float) $env;
110 }
111
112 if ('const' === $prefix) {
113 if (!\defined($env)) {
114 throw new RuntimeException(sprintf('Env var "%s" maps to undefined constant "%s".', $name, $env));
115 }
116
117 return \constant($env);
118 }
119
120 if ('base64' === $prefix) {
121 return base64_decode($env);
122 }
123
124 if ('json' === $prefix) {
125 $env = json_decode($env, true);
126
127 if (\JSON_ERROR_NONE !== json_last_error()) {
128 throw new RuntimeException(sprintf('Invalid JSON in env var "%s": ', $name).json_last_error_msg());
129 }
130
131 if (!\is_array($env)) {
132 throw new RuntimeException(sprintf('Invalid JSON env var "%s": array expected, "%s" given.', $name, \gettype($env)));
133 }
134
135 return $env;
136 }
137
138 if ('resolve' === $prefix) {
139 return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($name) {
140 if (!isset($match[1])) {
141 return '%';
142 }
143 $value = $this->container->getParameter($match[1]);
144 if (!is_scalar($value)) {
145 throw new RuntimeException(sprintf('Parameter "%s" found when resolving env var "%s" must be scalar, "%s" given.', $match[1], $name, \gettype($value)));
146 }
147
148 return $value;
149 }, $env);
150 }
151
152 throw new RuntimeException(sprintf('Unsupported env var prefix "%s".', $prefix));
153 }
154
155 private static function phpize($value)
156 {
157 if (!class_exists(XmlUtils::class)) {
158 throw new RuntimeException('The Symfony Config component is required to cast env vars to "bool", "int" or "float".');
159 }
160
161 return XmlUtils::phpize($value);
162 }
163 }
164