/server/wordpress/wp-content/plugins/wordpress-seo/vendor_prefixed/symfony/dependency-injection/ParameterBag/ParameterBag.php

https://gitlab.com/suporte.spturis/carnaval2015.spturis.com.br · PHP · 263 lines · 178 code · 2 blank · 83 comment · 26 complexity · 0da7e45f1e55c0c454ccddff460628f5 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace YoastSEO_Vendor\Symfony\Component\DependencyInjection\ParameterBag;
  11. use YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException;
  12. use YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
  13. use YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\RuntimeException;
  14. /**
  15. * Holds parameters.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class ParameterBag implements \YoastSEO_Vendor\Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface
  20. {
  21. protected $parameters = [];
  22. protected $resolved = \false;
  23. private $normalizedNames = [];
  24. /**
  25. * @param array $parameters An array of parameters
  26. */
  27. public function __construct(array $parameters = [])
  28. {
  29. $this->add($parameters);
  30. }
  31. /**
  32. * Clears all parameters.
  33. */
  34. public function clear()
  35. {
  36. $this->parameters = [];
  37. }
  38. /**
  39. * Adds parameters to the service container parameters.
  40. *
  41. * @param array $parameters An array of parameters
  42. */
  43. public function add(array $parameters)
  44. {
  45. foreach ($parameters as $key => $value) {
  46. $this->set($key, $value);
  47. }
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function all()
  53. {
  54. return $this->parameters;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function get($name)
  60. {
  61. $name = $this->normalizeName($name);
  62. if (!\array_key_exists($name, $this->parameters)) {
  63. if (!$name) {
  64. throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException($name);
  65. }
  66. $alternatives = [];
  67. foreach ($this->parameters as $key => $parameterValue) {
  68. $lev = \levenshtein($name, $key);
  69. if ($lev <= \strlen($name) / 3 || \false !== \strpos($key, $name)) {
  70. $alternatives[] = $key;
  71. }
  72. }
  73. $nonNestedAlternative = null;
  74. if (!\count($alternatives) && \false !== \strpos($name, '.')) {
  75. $namePartsLength = \array_map('strlen', \explode('.', $name));
  76. $key = \substr($name, 0, -1 * (1 + \array_pop($namePartsLength)));
  77. while (\count($namePartsLength)) {
  78. if ($this->has($key)) {
  79. if (\is_array($this->get($key))) {
  80. $nonNestedAlternative = $key;
  81. }
  82. break;
  83. }
  84. $key = \substr($key, 0, -1 * (1 + \array_pop($namePartsLength)));
  85. }
  86. }
  87. throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException($name, null, null, null, $alternatives, $nonNestedAlternative);
  88. }
  89. return $this->parameters[$name];
  90. }
  91. /**
  92. * Sets a service container parameter.
  93. *
  94. * @param string $name The parameter name
  95. * @param mixed $value The parameter value
  96. */
  97. public function set($name, $value)
  98. {
  99. $this->parameters[$this->normalizeName($name)] = $value;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function has($name)
  105. {
  106. return \array_key_exists($this->normalizeName($name), $this->parameters);
  107. }
  108. /**
  109. * Removes a parameter.
  110. *
  111. * @param string $name The parameter name
  112. */
  113. public function remove($name)
  114. {
  115. unset($this->parameters[$this->normalizeName($name)]);
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function resolve()
  121. {
  122. if ($this->resolved) {
  123. return;
  124. }
  125. $parameters = [];
  126. foreach ($this->parameters as $key => $value) {
  127. try {
  128. $value = $this->resolveValue($value);
  129. $parameters[$key] = $this->unescapeValue($value);
  130. } catch (\YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException $e) {
  131. $e->setSourceKey($key);
  132. throw $e;
  133. }
  134. }
  135. $this->parameters = $parameters;
  136. $this->resolved = \true;
  137. }
  138. /**
  139. * Replaces parameter placeholders (%name%) by their values.
  140. *
  141. * @param mixed $value A value
  142. * @param array $resolving An array of keys that are being resolved (used internally to detect circular references)
  143. *
  144. * @return mixed The resolved value
  145. *
  146. * @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
  147. * @throws ParameterCircularReferenceException if a circular reference if detected
  148. * @throws RuntimeException when a given parameter has a type problem
  149. */
  150. public function resolveValue($value, array $resolving = [])
  151. {
  152. if (\is_array($value)) {
  153. $args = [];
  154. foreach ($value as $k => $v) {
  155. $args[\is_string($k) ? $this->resolveValue($k, $resolving) : $k] = $this->resolveValue($v, $resolving);
  156. }
  157. return $args;
  158. }
  159. if (!\is_string($value) || 2 > \strlen($value)) {
  160. return $value;
  161. }
  162. return $this->resolveString($value, $resolving);
  163. }
  164. /**
  165. * Resolves parameters inside a string.
  166. *
  167. * @param string $value The string to resolve
  168. * @param array $resolving An array of keys that are being resolved (used internally to detect circular references)
  169. *
  170. * @return mixed The resolved string
  171. *
  172. * @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
  173. * @throws ParameterCircularReferenceException if a circular reference if detected
  174. * @throws RuntimeException when a given parameter has a type problem
  175. */
  176. public function resolveString($value, array $resolving = [])
  177. {
  178. // we do this to deal with non string values (Boolean, integer, ...)
  179. // as the preg_replace_callback throw an exception when trying
  180. // a non-string in a parameter value
  181. if (\preg_match('/^%([^%\\s]+)%$/', $value, $match)) {
  182. $key = $match[1];
  183. $lcKey = \strtolower($key);
  184. // strtolower() to be removed in 4.0
  185. if (isset($resolving[$lcKey])) {
  186. throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException(\array_keys($resolving));
  187. }
  188. $resolving[$lcKey] = \true;
  189. return $this->resolved ? $this->get($key) : $this->resolveValue($this->get($key), $resolving);
  190. }
  191. return \preg_replace_callback('/%%|%([^%\\s]+)%/', function ($match) use($resolving, $value) {
  192. // skip %%
  193. if (!isset($match[1])) {
  194. return '%%';
  195. }
  196. $key = $match[1];
  197. $lcKey = \strtolower($key);
  198. // strtolower() to be removed in 4.0
  199. if (isset($resolving[$lcKey])) {
  200. throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\ParameterCircularReferenceException(\array_keys($resolving));
  201. }
  202. $resolved = $this->get($key);
  203. if (!\is_string($resolved) && !\is_numeric($resolved)) {
  204. throw new \YoastSEO_Vendor\Symfony\Component\DependencyInjection\Exception\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, \gettype($resolved), $value));
  205. }
  206. $resolved = (string) $resolved;
  207. $resolving[$lcKey] = \true;
  208. return $this->isResolved() ? $resolved : $this->resolveString($resolved, $resolving);
  209. }, $value);
  210. }
  211. public function isResolved()
  212. {
  213. return $this->resolved;
  214. }
  215. /**
  216. * {@inheritdoc}
  217. */
  218. public function escapeValue($value)
  219. {
  220. if (\is_string($value)) {
  221. return \str_replace('%', '%%', $value);
  222. }
  223. if (\is_array($value)) {
  224. $result = [];
  225. foreach ($value as $k => $v) {
  226. $result[$k] = $this->escapeValue($v);
  227. }
  228. return $result;
  229. }
  230. return $value;
  231. }
  232. /**
  233. * {@inheritdoc}
  234. */
  235. public function unescapeValue($value)
  236. {
  237. if (\is_string($value)) {
  238. return \str_replace('%%', '%', $value);
  239. }
  240. if (\is_array($value)) {
  241. $result = [];
  242. foreach ($value as $k => $v) {
  243. $result[$k] = $this->unescapeValue($v);
  244. }
  245. return $result;
  246. }
  247. return $value;
  248. }
  249. private function normalizeName($name)
  250. {
  251. if (isset($this->normalizedNames[$normalizedName = \strtolower($name)])) {
  252. $normalizedName = $this->normalizedNames[$normalizedName];
  253. if ((string) $name !== $normalizedName) {
  254. @\trigger_error(\sprintf('Parameter names will be made case sensitive in Symfony 4.0. Using "%s" instead of "%s" is deprecated since Symfony 3.4.', $name, $normalizedName), \E_USER_DEPRECATED);
  255. }
  256. } else {
  257. $normalizedName = $this->normalizedNames[$normalizedName] = (string) $name;
  258. }
  259. return $normalizedName;
  260. }
  261. }