ResolveParameterPlaceHoldersPass.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Symfony\Component\DependencyInjection\Compiler;
  3. use Symfony\Component\DependencyInjection\ContainerBuilder;
  4. /*
  5. * This file is part of the Symfony framework.
  6. *
  7. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  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. protected $parameterBag;
  20. public function process(ContainerBuilder $container)
  21. {
  22. $this->parameterBag = $container->getParameterBag();
  23. foreach ($container->getDefinitions() as $id => $definition) {
  24. $definition->setClass($this->resolveValue($definition->getClass()));
  25. $definition->setArguments($this->resolveValue($definition->getArguments()));
  26. $calls = array();
  27. foreach ($definition->getMethodCalls() as $name => $arguments) {
  28. $calls[$this->resolveValue($name)] = $this->resolveValue($arguments);
  29. }
  30. $definition->setMethodCalls($calls);
  31. }
  32. $aliases = array();
  33. foreach ($container->getAliases() as $name => $target) {
  34. $aliases[$this->resolveValue($name)] = $this->resolveValue($target);
  35. }
  36. $container->setAliases($aliases);
  37. $injectors = array();
  38. foreach ($container->getInterfaceInjectors() as $class => $injector) {
  39. $injector->setClass($this->resolveValue($injector->getClass()));
  40. $injectors[$this->resolveValue($class)] = $injector;
  41. }
  42. $container->setInterfaceInjectors($injectors);
  43. }
  44. protected function resolveValue($value)
  45. {
  46. if (is_array($value)) {
  47. $resolved = array();
  48. foreach ($value as $k => $v) {
  49. $resolved[$this->resolveValue($k)] = $this->resolveValue($v);
  50. }
  51. return $resolved;
  52. } else if (is_string($value)) {
  53. return $this->resolveString($value);
  54. } else {
  55. return $value;
  56. }
  57. }
  58. public function resolveString($value)
  59. {
  60. if (preg_match('/^%[^%]+%$/', $value)) {
  61. return $this->resolveValue($this->parameterBag->resolveValue($value));
  62. }
  63. $self = $this;
  64. $parameterBag = $this->parameterBag;
  65. return preg_replace_callback('/(?<!%)%[^%]+%/',
  66. function($parameter) use ($self, $parameterBag) {
  67. $resolved = $parameterBag->resolveValue($parameter[0]);
  68. if (!is_string($resolved)) {
  69. throw new \RuntimeException('You can only embed strings in other parameters.');
  70. }
  71. return $self->resolveString($resolved);
  72. },
  73. $value
  74. );
  75. }
  76. }