ContainerBag.php
4 years ago
ContainerBagInterface.php
4 years ago
EnvPlaceholderParameterBag.php
4 years ago
FrozenParameterBag.php
4 years ago
ParameterBag.php
4 years ago
ParameterBagInterface.php
4 years ago
index.php
3 years ago
ParameterBag.php
168 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\DependencyInjection\ParameterBag; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException; |
| 5 | use MailPoetVendor\Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException; |
| 6 | use MailPoetVendor\Symfony\Component\DependencyInjection\Exception\RuntimeException; |
| 7 | class ParameterBag implements ParameterBagInterface |
| 8 | { |
| 9 | protected $parameters = []; |
| 10 | protected $resolved = \false; |
| 11 | public function __construct(array $parameters = []) |
| 12 | { |
| 13 | $this->add($parameters); |
| 14 | } |
| 15 | public function clear() |
| 16 | { |
| 17 | $this->parameters = []; |
| 18 | } |
| 19 | public function add(array $parameters) |
| 20 | { |
| 21 | foreach ($parameters as $key => $value) { |
| 22 | $this->set($key, $value); |
| 23 | } |
| 24 | } |
| 25 | public function all() |
| 26 | { |
| 27 | return $this->parameters; |
| 28 | } |
| 29 | public function get(string $name) |
| 30 | { |
| 31 | if (!\array_key_exists($name, $this->parameters)) { |
| 32 | if (!$name) { |
| 33 | throw new ParameterNotFoundException($name); |
| 34 | } |
| 35 | $alternatives = []; |
| 36 | foreach ($this->parameters as $key => $parameterValue) { |
| 37 | $lev = \levenshtein($name, $key); |
| 38 | if ($lev <= \strlen($name) / 3 || \str_contains($key, $name)) { |
| 39 | $alternatives[] = $key; |
| 40 | } |
| 41 | } |
| 42 | $nonNestedAlternative = null; |
| 43 | if (!\count($alternatives) && \str_contains($name, '.')) { |
| 44 | $namePartsLength = \array_map('strlen', \explode('.', $name)); |
| 45 | $key = \substr($name, 0, -1 * (1 + \array_pop($namePartsLength))); |
| 46 | while (\count($namePartsLength)) { |
| 47 | if ($this->has($key)) { |
| 48 | if (\is_array($this->get($key))) { |
| 49 | $nonNestedAlternative = $key; |
| 50 | } |
| 51 | break; |
| 52 | } |
| 53 | $key = \substr($key, 0, -1 * (1 + \array_pop($namePartsLength))); |
| 54 | } |
| 55 | } |
| 56 | throw new ParameterNotFoundException($name, null, null, null, $alternatives, $nonNestedAlternative); |
| 57 | } |
| 58 | return $this->parameters[$name]; |
| 59 | } |
| 60 | public function set(string $name, $value) |
| 61 | { |
| 62 | $this->parameters[$name] = $value; |
| 63 | } |
| 64 | public function has(string $name) |
| 65 | { |
| 66 | return \array_key_exists($name, $this->parameters); |
| 67 | } |
| 68 | public function remove(string $name) |
| 69 | { |
| 70 | unset($this->parameters[$name]); |
| 71 | } |
| 72 | public function resolve() |
| 73 | { |
| 74 | if ($this->resolved) { |
| 75 | return; |
| 76 | } |
| 77 | $parameters = []; |
| 78 | foreach ($this->parameters as $key => $value) { |
| 79 | try { |
| 80 | $value = $this->resolveValue($value); |
| 81 | $parameters[$key] = $this->unescapeValue($value); |
| 82 | } catch (ParameterNotFoundException $e) { |
| 83 | $e->setSourceKey($key); |
| 84 | throw $e; |
| 85 | } |
| 86 | } |
| 87 | $this->parameters = $parameters; |
| 88 | $this->resolved = \true; |
| 89 | } |
| 90 | public function resolveValue($value, array $resolving = []) |
| 91 | { |
| 92 | if (\is_array($value)) { |
| 93 | $args = []; |
| 94 | foreach ($value as $k => $v) { |
| 95 | $args[\is_string($k) ? $this->resolveValue($k, $resolving) : $k] = $this->resolveValue($v, $resolving); |
| 96 | } |
| 97 | return $args; |
| 98 | } |
| 99 | if (!\is_string($value) || 2 > \strlen($value)) { |
| 100 | return $value; |
| 101 | } |
| 102 | return $this->resolveString($value, $resolving); |
| 103 | } |
| 104 | public function resolveString(string $value, array $resolving = []) |
| 105 | { |
| 106 | // we do this to deal with non string values (Boolean, integer, ...) |
| 107 | // as the preg_replace_callback throw an exception when trying |
| 108 | // a non-string in a parameter value |
| 109 | if (\preg_match('/^%([^%\\s]+)%$/', $value, $match)) { |
| 110 | $key = $match[1]; |
| 111 | if (isset($resolving[$key])) { |
| 112 | throw new ParameterCircularReferenceException(\array_keys($resolving)); |
| 113 | } |
| 114 | $resolving[$key] = \true; |
| 115 | return $this->resolved ? $this->get($key) : $this->resolveValue($this->get($key), $resolving); |
| 116 | } |
| 117 | return \preg_replace_callback('/%%|%([^%\\s]+)%/', function ($match) use($resolving, $value) { |
| 118 | // skip %% |
| 119 | if (!isset($match[1])) { |
| 120 | return '%%'; |
| 121 | } |
| 122 | $key = $match[1]; |
| 123 | if (isset($resolving[$key])) { |
| 124 | throw new ParameterCircularReferenceException(\array_keys($resolving)); |
| 125 | } |
| 126 | $resolved = $this->get($key); |
| 127 | if (!\is_string($resolved) && !\is_numeric($resolved)) { |
| 128 | throw new RuntimeException(\sprintf('A string value must be composed of strings and/or numbers, but found parameter "%s" of type "%s" inside string value "%s".', $key, \get_debug_type($resolved), $value)); |
| 129 | } |
| 130 | $resolved = (string) $resolved; |
| 131 | $resolving[$key] = \true; |
| 132 | return $this->isResolved() ? $resolved : $this->resolveString($resolved, $resolving); |
| 133 | }, $value); |
| 134 | } |
| 135 | public function isResolved() |
| 136 | { |
| 137 | return $this->resolved; |
| 138 | } |
| 139 | public function escapeValue($value) |
| 140 | { |
| 141 | if (\is_string($value)) { |
| 142 | return \str_replace('%', '%%', $value); |
| 143 | } |
| 144 | if (\is_array($value)) { |
| 145 | $result = []; |
| 146 | foreach ($value as $k => $v) { |
| 147 | $result[$k] = $this->escapeValue($v); |
| 148 | } |
| 149 | return $result; |
| 150 | } |
| 151 | return $value; |
| 152 | } |
| 153 | public function unescapeValue($value) |
| 154 | { |
| 155 | if (\is_string($value)) { |
| 156 | return \str_replace('%%', '%', $value); |
| 157 | } |
| 158 | if (\is_array($value)) { |
| 159 | $result = []; |
| 160 | foreach ($value as $k => $v) { |
| 161 | $result[$k] = $this->unescapeValue($v); |
| 162 | } |
| 163 | return $result; |
| 164 | } |
| 165 | return $value; |
| 166 | } |
| 167 | } |
| 168 |