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\ParameterBag;
|
Chris@0
|
13
|
Chris@17
|
14 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
Chris@0
|
15 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * @author Nicolas Grekas <p@tchwork.com>
|
Chris@0
|
19 */
|
Chris@0
|
20 class EnvPlaceholderParameterBag extends ParameterBag
|
Chris@0
|
21 {
|
Chris@17
|
22 private $envPlaceholders = [];
|
Chris@17
|
23 private $providedTypes = [];
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * {@inheritdoc}
|
Chris@0
|
27 */
|
Chris@0
|
28 public function get($name)
|
Chris@0
|
29 {
|
Chris@0
|
30 if (0 === strpos($name, 'env(') && ')' === substr($name, -1) && 'env()' !== $name) {
|
Chris@0
|
31 $env = substr($name, 4, -1);
|
Chris@0
|
32
|
Chris@0
|
33 if (isset($this->envPlaceholders[$env])) {
|
Chris@0
|
34 foreach ($this->envPlaceholders[$env] as $placeholder) {
|
Chris@0
|
35 return $placeholder; // return first result
|
Chris@0
|
36 }
|
Chris@0
|
37 }
|
Chris@14
|
38 if (!preg_match('/^(?:\w++:)*+\w++$/', $env)) {
|
Chris@0
|
39 throw new InvalidArgumentException(sprintf('Invalid %s name: only "word" characters are allowed.', $name));
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 if ($this->has($name)) {
|
Chris@0
|
43 $defaultValue = parent::get($name);
|
Chris@0
|
44
|
Chris@0
|
45 if (null !== $defaultValue && !is_scalar($defaultValue)) {
|
Chris@17
|
46 throw new RuntimeException(sprintf('The default value of an env() parameter must be scalar or null, but "%s" given to "%s".', \gettype($defaultValue), $name));
|
Chris@0
|
47 }
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 $uniqueName = md5($name.uniqid(mt_rand(), true));
|
Chris@14
|
51 $placeholder = sprintf('env_%s_%s', str_replace(':', '_', $env), $uniqueName);
|
Chris@0
|
52 $this->envPlaceholders[$env][$placeholder] = $placeholder;
|
Chris@0
|
53
|
Chris@0
|
54 return $placeholder;
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 return parent::get($name);
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * Returns the map of env vars used in the resolved parameter values to their placeholders.
|
Chris@0
|
62 *
|
Chris@0
|
63 * @return string[][] A map of env var names to their placeholders
|
Chris@0
|
64 */
|
Chris@0
|
65 public function getEnvPlaceholders()
|
Chris@0
|
66 {
|
Chris@0
|
67 return $this->envPlaceholders;
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * Merges the env placeholders of another EnvPlaceholderParameterBag.
|
Chris@0
|
72 */
|
Chris@0
|
73 public function mergeEnvPlaceholders(self $bag)
|
Chris@0
|
74 {
|
Chris@0
|
75 if ($newPlaceholders = $bag->getEnvPlaceholders()) {
|
Chris@0
|
76 $this->envPlaceholders += $newPlaceholders;
|
Chris@0
|
77
|
Chris@0
|
78 foreach ($newPlaceholders as $env => $placeholders) {
|
Chris@0
|
79 $this->envPlaceholders[$env] += $placeholders;
|
Chris@0
|
80 }
|
Chris@0
|
81 }
|
Chris@0
|
82 }
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@14
|
85 * Maps env prefixes to their corresponding PHP types.
|
Chris@14
|
86 */
|
Chris@14
|
87 public function setProvidedTypes(array $providedTypes)
|
Chris@14
|
88 {
|
Chris@14
|
89 $this->providedTypes = $providedTypes;
|
Chris@14
|
90 }
|
Chris@14
|
91
|
Chris@14
|
92 /**
|
Chris@14
|
93 * Gets the PHP types corresponding to env() parameter prefixes.
|
Chris@14
|
94 *
|
Chris@14
|
95 * @return string[][]
|
Chris@14
|
96 */
|
Chris@14
|
97 public function getProvidedTypes()
|
Chris@14
|
98 {
|
Chris@14
|
99 return $this->providedTypes;
|
Chris@14
|
100 }
|
Chris@14
|
101
|
Chris@14
|
102 /**
|
Chris@0
|
103 * {@inheritdoc}
|
Chris@0
|
104 */
|
Chris@0
|
105 public function resolve()
|
Chris@0
|
106 {
|
Chris@0
|
107 if ($this->resolved) {
|
Chris@0
|
108 return;
|
Chris@0
|
109 }
|
Chris@0
|
110 parent::resolve();
|
Chris@0
|
111
|
Chris@0
|
112 foreach ($this->envPlaceholders as $env => $placeholders) {
|
Chris@14
|
113 if (!$this->has($name = "env($env)")) {
|
Chris@0
|
114 continue;
|
Chris@0
|
115 }
|
Chris@0
|
116 if (is_numeric($default = $this->parameters[$name])) {
|
Chris@0
|
117 $this->parameters[$name] = (string) $default;
|
Chris@0
|
118 } elseif (null !== $default && !is_scalar($default)) {
|
Chris@17
|
119 throw new RuntimeException(sprintf('The default value of env parameter "%s" must be scalar or null, %s given.', $env, \gettype($default)));
|
Chris@0
|
120 }
|
Chris@0
|
121 }
|
Chris@0
|
122 }
|
Chris@0
|
123 }
|