comparison vendor/symfony/dependency-injection/ParameterBag/EnvPlaceholderParameterBag.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\DependencyInjection\ParameterBag;
13
14 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
15 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
16
17 /**
18 * @author Nicolas Grekas <p@tchwork.com>
19 */
20 class EnvPlaceholderParameterBag extends ParameterBag
21 {
22 private $envPlaceholders = array();
23
24 /**
25 * {@inheritdoc}
26 */
27 public function get($name)
28 {
29 if (0 === strpos($name, 'env(') && ')' === substr($name, -1) && 'env()' !== $name) {
30 $env = substr($name, 4, -1);
31
32 if (isset($this->envPlaceholders[$env])) {
33 foreach ($this->envPlaceholders[$env] as $placeholder) {
34 return $placeholder; // return first result
35 }
36 }
37 if (preg_match('/\W/', $env)) {
38 throw new InvalidArgumentException(sprintf('Invalid %s name: only "word" characters are allowed.', $name));
39 }
40
41 if ($this->has($name)) {
42 $defaultValue = parent::get($name);
43
44 if (null !== $defaultValue && !is_scalar($defaultValue)) {
45 throw new RuntimeException(sprintf('The default value of an env() parameter must be scalar or null, but "%s" given to "%s".', gettype($defaultValue), $name));
46 }
47 }
48
49 $uniqueName = md5($name.uniqid(mt_rand(), true));
50 $placeholder = sprintf('env_%s_%s', $env, $uniqueName);
51 $this->envPlaceholders[$env][$placeholder] = $placeholder;
52
53 return $placeholder;
54 }
55
56 return parent::get($name);
57 }
58
59 /**
60 * Returns the map of env vars used in the resolved parameter values to their placeholders.
61 *
62 * @return string[][] A map of env var names to their placeholders
63 */
64 public function getEnvPlaceholders()
65 {
66 return $this->envPlaceholders;
67 }
68
69 /**
70 * Merges the env placeholders of another EnvPlaceholderParameterBag.
71 */
72 public function mergeEnvPlaceholders(self $bag)
73 {
74 if ($newPlaceholders = $bag->getEnvPlaceholders()) {
75 $this->envPlaceholders += $newPlaceholders;
76
77 foreach ($newPlaceholders as $env => $placeholders) {
78 $this->envPlaceholders[$env] += $placeholders;
79 }
80 }
81 }
82
83 /**
84 * {@inheritdoc}
85 */
86 public function resolve()
87 {
88 if ($this->resolved) {
89 return;
90 }
91 parent::resolve();
92
93 foreach ($this->envPlaceholders as $env => $placeholders) {
94 if (!isset($this->parameters[$name = strtolower("env($env)")])) {
95 continue;
96 }
97 if (is_numeric($default = $this->parameters[$name])) {
98 $this->parameters[$name] = (string) $default;
99 } elseif (null !== $default && !is_scalar($default)) {
100 throw new RuntimeException(sprintf('The default value of env parameter "%s" must be scalar or null, %s given.', $env, gettype($default)));
101 }
102 }
103 }
104 }