wordpress-seo
/
vendor_prefixed
/
symfony
/
dependency-injection
/
ParameterBag
/
EnvPlaceholderParameterBag.php
EnvPlaceholderParameterBag.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at vendor_prefixed/symfony/dependency-injection/ParameterBag/EnvPlaceholderParameterBag.php
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <[email protected]> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace YoastSEO_Vendor\Symfony\Component\DependencyInjection\ParameterBag; |
| 12 | |
| 13 | use YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
| 14 | use YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\RuntimeException; |
| 15 | /** |
| 16 | * @author Nicolas Grekas <[email protected]> |
| 17 | */ |
| 18 | class EnvPlaceholderParameterBag extends \YoastSEO_Vendor\Symfony\Component\DependencyInjection\ParameterBag\ParameterBag |
| 19 | { |
| 20 | private $envPlaceholderUniquePrefix; |
| 21 | private $envPlaceholders = []; |
| 22 | private $unusedEnvPlaceholders = []; |
| 23 | private $providedTypes = []; |
| 24 | private static $counter = 0; |
| 25 | /** |
| 26 | * {@inheritdoc} |
| 27 | */ |
| 28 | public function get(string $name) |
| 29 | { |
| 30 | if (\str_starts_with($name, 'env(') && \str_ends_with($name, ')') && 'env()' !== $name) { |
| 31 | $env = \substr($name, 4, -1); |
| 32 | if (isset($this->envPlaceholders[$env])) { |
| 33 | foreach ($this->envPlaceholders[$env] as $placeholder) { |
| 34 | return $placeholder; |
| 35 | // return first result |
| 36 | } |
| 37 | } |
| 38 | if (isset($this->unusedEnvPlaceholders[$env])) { |
| 39 | foreach ($this->unusedEnvPlaceholders[$env] as $placeholder) { |
| 40 | return $placeholder; |
| 41 | // return first result |
| 42 | } |
| 43 | } |
| 44 | if (!\preg_match('/^(?:[-.\\w]*+:)*+\\w++$/', $env)) { |
| 45 | throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\InvalidArgumentException(\sprintf('Invalid %s name: only "word" characters are allowed.', $name)); |
| 46 | } |
| 47 | if ($this->has($name) && null !== ($defaultValue = parent::get($name)) && !\is_string($defaultValue)) { |
| 48 | throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\RuntimeException(\sprintf('The default value of an env() parameter must be a string or null, but "%s" given to "%s".', \get_debug_type($defaultValue), $name)); |
| 49 | } |
| 50 | $uniqueName = \md5($name . '_' . self::$counter++); |
| 51 | $placeholder = \sprintf('%s_%s_%s', $this->getEnvPlaceholderUniquePrefix(), \strtr($env, ':-.', '___'), $uniqueName); |
| 52 | $this->envPlaceholders[$env][$placeholder] = $placeholder; |
| 53 | return $placeholder; |
| 54 | } |
| 55 | return parent::get($name); |
| 56 | } |
| 57 | /** |
| 58 | * Gets the common env placeholder prefix for env vars created by this bag. |
| 59 | */ |
| 60 | public function getEnvPlaceholderUniquePrefix() : string |
| 61 | { |
| 62 | if (null === $this->envPlaceholderUniquePrefix) { |
| 63 | $reproducibleEntropy = \unserialize(\serialize($this->parameters)); |
| 64 | \array_walk_recursive($reproducibleEntropy, function (&$v) { |
| 65 | $v = null; |
| 66 | }); |
| 67 | $this->envPlaceholderUniquePrefix = 'env_' . \substr(\md5(\serialize($reproducibleEntropy)), -16); |
| 68 | } |
| 69 | return $this->envPlaceholderUniquePrefix; |
| 70 | } |
| 71 | /** |
| 72 | * Returns the map of env vars used in the resolved parameter values to their placeholders. |
| 73 | * |
| 74 | * @return string[][] A map of env var names to their placeholders |
| 75 | */ |
| 76 | public function getEnvPlaceholders() |
| 77 | { |
| 78 | return $this->envPlaceholders; |
| 79 | } |
| 80 | public function getUnusedEnvPlaceholders() : array |
| 81 | { |
| 82 | return $this->unusedEnvPlaceholders; |
| 83 | } |
| 84 | public function clearUnusedEnvPlaceholders() |
| 85 | { |
| 86 | $this->unusedEnvPlaceholders = []; |
| 87 | } |
| 88 | /** |
| 89 | * Merges the env placeholders of another EnvPlaceholderParameterBag. |
| 90 | */ |
| 91 | public function mergeEnvPlaceholders(self $bag) |
| 92 | { |
| 93 | if ($newPlaceholders = $bag->getEnvPlaceholders()) { |
| 94 | $this->envPlaceholders += $newPlaceholders; |
| 95 | foreach ($newPlaceholders as $env => $placeholders) { |
| 96 | $this->envPlaceholders[$env] += $placeholders; |
| 97 | } |
| 98 | } |
| 99 | if ($newUnusedPlaceholders = $bag->getUnusedEnvPlaceholders()) { |
| 100 | $this->unusedEnvPlaceholders += $newUnusedPlaceholders; |
| 101 | foreach ($newUnusedPlaceholders as $env => $placeholders) { |
| 102 | $this->unusedEnvPlaceholders[$env] += $placeholders; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | /** |
| 107 | * Maps env prefixes to their corresponding PHP types. |
| 108 | */ |
| 109 | public function setProvidedTypes(array $providedTypes) |
| 110 | { |
| 111 | $this->providedTypes = $providedTypes; |
| 112 | } |
| 113 | /** |
| 114 | * Gets the PHP types corresponding to env() parameter prefixes. |
| 115 | * |
| 116 | * @return string[][] |
| 117 | */ |
| 118 | public function getProvidedTypes() |
| 119 | { |
| 120 | return $this->providedTypes; |
| 121 | } |
| 122 | /** |
| 123 | * {@inheritdoc} |
| 124 | */ |
| 125 | public function resolve() |
| 126 | { |
| 127 | if ($this->resolved) { |
| 128 | return; |
| 129 | } |
| 130 | parent::resolve(); |
| 131 | foreach ($this->envPlaceholders as $env => $placeholders) { |
| 132 | if ($this->has($name = "env({$env})") && null !== ($default = $this->parameters[$name]) && !\is_string($default)) { |
| 133 | throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\RuntimeException(\sprintf('The default value of env parameter "%s" must be a string or null, "%s" given.', $env, \get_debug_type($default))); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 |