/src/Symfony/Component/DependencyInjection/ParameterBag/ParameterBag.php

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