PageRenderTime 43ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/baldurrensch/symfony
PHP | 294 lines | 142 code | 42 blank | 110 comment | 14 complexity | 59af26f895175a6abffa0d86b72d2e6a 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. * 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. throw new ParameterNotFoundException($name);
  87. }
  88. return $this->parameters[$name];
  89. }
  90. /**
  91. * Sets a service container parameter.
  92. *
  93. * @param string $name The parameter name
  94. * @param mixed $value The parameter value
  95. *
  96. * @api
  97. */
  98. public function set($name, $value)
  99. {
  100. $this->parameters[strtolower($name)] = $value;
  101. }
  102. /**
  103. * Returns true if a parameter name is defined.
  104. *
  105. * @param string $name The parameter name
  106. *
  107. * @return Boolean true if the parameter name is defined, false otherwise
  108. *
  109. * @api
  110. */
  111. public function has($name)
  112. {
  113. return array_key_exists(strtolower($name), $this->parameters);
  114. }
  115. /**
  116. * Removes a parameter.
  117. *
  118. * @param string $key The key
  119. *
  120. * @api
  121. */
  122. public function remove($key)
  123. {
  124. unset($this->parameters[strtolower($key)]);
  125. }
  126. /**
  127. * Replaces parameter placeholders (%name%) by their values for all parameters.
  128. */
  129. public function resolve()
  130. {
  131. if ($this->resolved) {
  132. return;
  133. }
  134. $parameters = array();
  135. foreach ($this->parameters as $key => $value) {
  136. try {
  137. $value = $this->resolveValue($value);
  138. $parameters[$key] = $this->unescapeValue($value);
  139. } catch (ParameterNotFoundException $e) {
  140. $e->setSourceKey($key);
  141. throw $e;
  142. }
  143. }
  144. $this->parameters = $parameters;
  145. $this->resolved = true;
  146. }
  147. /**
  148. * Replaces parameter placeholders (%name%) by their values.
  149. *
  150. * @param mixed $value A value
  151. * @param array $resolving An array of keys that are being resolved (used internally to detect circular references)
  152. *
  153. * @return mixed The resolved value
  154. *
  155. * @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
  156. * @throws ParameterCircularReferenceException if a circular reference if detected
  157. * @throws RuntimeException when a given parameter has a type problem.
  158. */
  159. public function resolveValue($value, array $resolving = array())
  160. {
  161. if (is_array($value)) {
  162. $args = array();
  163. foreach ($value as $k => $v) {
  164. $args[$this->resolveValue($k, $resolving)] = $this->resolveValue($v, $resolving);
  165. }
  166. return $args;
  167. }
  168. if (!is_string($value)) {
  169. return $value;
  170. }
  171. return $this->resolveString($value, $resolving);
  172. }
  173. /**
  174. * Resolves parameters inside a string
  175. *
  176. * @param string $value The string to resolve
  177. * @param array $resolving An array of keys that are being resolved (used internally to detect circular references)
  178. *
  179. * @return string The resolved string
  180. *
  181. * @throws ParameterNotFoundException if a placeholder references a parameter that does not exist
  182. * @throws ParameterCircularReferenceException if a circular reference if detected
  183. * @throws RuntimeException when a given parameter has a type problem.
  184. */
  185. public function resolveString($value, array $resolving = array())
  186. {
  187. // we do this to deal with non string values (Boolean, integer, ...)
  188. // as the preg_replace_callback throw an exception when trying
  189. // a non-string in a parameter value
  190. if (preg_match('/^%([^%\s]+)%$/', $value, $match)) {
  191. $key = strtolower($match[1]);
  192. if (isset($resolving[$key])) {
  193. throw new ParameterCircularReferenceException(array_keys($resolving));
  194. }
  195. $resolving[$key] = true;
  196. return $this->resolved ? $this->get($key) : $this->resolveValue($this->get($key), $resolving);
  197. }
  198. $self = $this;
  199. return preg_replace_callback('/%%|%([^%\s]+)%/', function ($match) use ($self, $resolving, $value) {
  200. // skip %%
  201. if (!isset($match[1])) {
  202. return '%%';
  203. }
  204. $key = strtolower($match[1]);
  205. if (isset($resolving[$key])) {
  206. throw new ParameterCircularReferenceException(array_keys($resolving));
  207. }
  208. $resolved = $self->get($key);
  209. if (!is_string($resolved) && !is_numeric($resolved)) {
  210. 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));
  211. }
  212. $resolved = (string) $resolved;
  213. $resolving[$key] = true;
  214. return $self->isResolved() ? $resolved : $self->resolveString($resolved, $resolving);
  215. }, $value);
  216. }
  217. public function isResolved()
  218. {
  219. return $this->resolved;
  220. }
  221. /**
  222. * {@inheritDoc}
  223. */
  224. public function escapeValue($value)
  225. {
  226. if (is_string($value)) {
  227. return str_replace('%', '%%', $value);
  228. }
  229. if (is_array($value)) {
  230. $result = array();
  231. foreach ($value as $k => $v) {
  232. $result[$k] = $this->escapeValue($v);
  233. }
  234. return $result;
  235. }
  236. return $value;
  237. }
  238. public function unescapeValue($value)
  239. {
  240. if (is_string($value)) {
  241. return str_replace('%%', '%', $value);
  242. }
  243. if (is_array($value)) {
  244. $result = array();
  245. foreach ($value as $k => $v) {
  246. $result[$k] = $this->unescapeValue($v);
  247. }
  248. return $result;
  249. }
  250. return $value;
  251. }
  252. }