ResolveParameterPlaceHoldersPass.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\Compiler;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. /**
  13. * Resolves all parameter placeholders "%somevalue%" to their real values.
  14. *
  15. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  16. */
  17. class ResolveParameterPlaceHoldersPass implements CompilerPassInterface
  18. {
  19. private $parameterBag;
  20. /**
  21. * Processes the ContainerBuilder to resolve parameter placeholders.
  22. *
  23. * @param ContainerBuilder $container
  24. */
  25. public function process(ContainerBuilder $container)
  26. {
  27. $this->parameterBag = $container->getParameterBag();
  28. foreach ($container->getDefinitions() as $definition) {
  29. $definition->setClass($this->resolveValue($definition->getClass()));
  30. $definition->setFile($this->resolveValue($definition->getFile()));
  31. $definition->setArguments($this->resolveValue($definition->getArguments()));
  32. $calls = array();
  33. foreach ($definition->getMethodCalls() as $name => $arguments) {
  34. $calls[$this->resolveValue($name)] = $this->resolveValue($arguments);
  35. }
  36. $definition->setMethodCalls($calls);
  37. $definition->setProperties($this->resolveValue($definition->getProperties()));
  38. }
  39. $aliases = array();
  40. foreach ($container->getAliases() as $name => $target) {
  41. $aliases[$this->resolveValue($name)] = $this->resolveValue($target);
  42. }
  43. $container->setAliases($aliases);
  44. $parameterBag = $container->getParameterBag();
  45. foreach ($parameterBag->all() as $key => $value) {
  46. $parameterBag->set($key, $this->resolveValue($value));
  47. }
  48. }
  49. /**
  50. * Expands parameters into their full values
  51. *
  52. * @param mixed $value The value to resolve
  53. * @return mixed The resolved value
  54. */
  55. private function resolveValue($value)
  56. {
  57. if (is_array($value)) {
  58. $resolved = array();
  59. foreach ($value as $k => $v) {
  60. $resolved[$this->resolveValue($k)] = $this->resolveValue($v);
  61. }
  62. return $resolved;
  63. } else if (is_string($value)) {
  64. return $this->resolveString($value);
  65. }
  66. return $value;
  67. }
  68. /**
  69. * Resolves parameters inside a string
  70. *
  71. * @param string $value The string to resolve
  72. * @return string The resolved string
  73. * @throws \RuntimeException when a given parameter has a type problem.
  74. */
  75. public function resolveString($value)
  76. {
  77. if (preg_match('/^%[^%]+%$/', $value)) {
  78. return $this->resolveValue($this->parameterBag->resolveValue($value));
  79. }
  80. $self = $this;
  81. $parameterBag = $this->parameterBag;
  82. return preg_replace_callback('/(?<!%)%[^%]+%/',
  83. function($parameter) use ($self, $parameterBag) {
  84. $resolved = $parameterBag->resolveValue($parameter[0]);
  85. if (!is_string($resolved)) {
  86. throw new \RuntimeException('You can only embed strings in other parameters.');
  87. }
  88. return $self->resolveString($resolved);
  89. },
  90. $value
  91. );
  92. }
  93. }