ParameterBag.php 7.5 KB

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