ParameterBag.php 8.1 KB

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