PageRenderTime 56ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/casoetan/ServerGroveLiveChat
PHP | 151 lines | 68 code | 19 blank | 64 comment | 4 complexity | c6bd0430f49352654e18971116ed2803 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, ISC, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. /**
  12. *
  13. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  14. */
  15. class ParameterBag implements ParameterBagInterface
  16. {
  17. protected $parameters;
  18. /**
  19. * Constructor.
  20. *
  21. * @param array $parameters An array of parameters
  22. */
  23. public function __construct(array $parameters = array())
  24. {
  25. $this->parameters = array();
  26. $this->add($parameters);
  27. }
  28. /**
  29. * Clears all parameters.
  30. */
  31. public function clear()
  32. {
  33. $this->parameters = array();
  34. }
  35. /**
  36. * Adds parameters to the service container parameters.
  37. *
  38. * @param array $parameters An array of parameters
  39. */
  40. public function add(array $parameters)
  41. {
  42. foreach ($parameters as $key => $value) {
  43. $this->parameters[strtolower($key)] = $value;
  44. }
  45. }
  46. /**
  47. * Gets the service container parameters.
  48. *
  49. * @return array An array of parameters
  50. */
  51. public function all()
  52. {
  53. return $this->parameters;
  54. }
  55. /**
  56. * Gets a service container parameter.
  57. *
  58. * @param string $name The parameter name
  59. *
  60. * @return mixed The parameter value
  61. *
  62. * @throws \InvalidArgumentException if the parameter is not defined
  63. */
  64. public function get($name)
  65. {
  66. $name = strtolower($name);
  67. if (!array_key_exists($name, $this->parameters)) {
  68. throw new \InvalidArgumentException(sprintf('The parameter "%s" must be defined.', $name));
  69. }
  70. return $this->parameters[$name];
  71. }
  72. /**
  73. * Sets a service container parameter.
  74. *
  75. * @param string $name The parameter name
  76. * @param mixed $parameters The parameter value
  77. */
  78. public function set($name, $value)
  79. {
  80. $this->parameters[strtolower($name)] = $value;
  81. }
  82. /**
  83. * Returns true if a parameter name is defined.
  84. *
  85. * @param string $name The parameter name
  86. *
  87. * @return Boolean true if the parameter name is defined, false otherwise
  88. */
  89. public function has($name)
  90. {
  91. return array_key_exists(strtolower($name), $this->parameters);
  92. }
  93. /**
  94. * Replaces parameter placeholders (%name%) by their values for all parameters.
  95. */
  96. public function resolve()
  97. {
  98. foreach ($this->parameters as $key => $value) {
  99. $this->parameters[$key] = $this->resolveValue($value);
  100. }
  101. }
  102. /**
  103. * Replaces parameter placeholders (%name%) by their values.
  104. *
  105. * @param mixed $value A value
  106. *
  107. * @throws \InvalidArgumentException if a placeholder references a parameter that does not exist
  108. */
  109. public function resolveValue($value)
  110. {
  111. if (is_array($value)) {
  112. $args = array();
  113. foreach ($value as $k => $v) {
  114. $args[$this->resolveValue($k)] = $this->resolveValue($v);
  115. }
  116. return $args;
  117. }
  118. if (!is_string($value)) {
  119. return $value;
  120. }
  121. if (preg_match('/^%([^%]+)%$/', $value, $match)) {
  122. // we do this to deal with non string values (Boolean, integer, ...)
  123. // the preg_replace_callback converts them to strings
  124. return $this->get(strtolower($match[1]));
  125. }
  126. return str_replace('%%', '%', preg_replace_callback(array('/(?<!%)%([^%]+)%/'), array($this, 'resolveValueCallback'), $value));
  127. }
  128. protected function resolveValueCallback($match)
  129. {
  130. return $this->get(strtolower($match[1]));
  131. }
  132. }