ResolveParameterPlaceHoldersPass.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. use Symfony\Component\DependencyInjection\Definition;
  13. use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
  14. /**
  15. * Resolves all parameter placeholders "%somevalue%" to their real values.
  16. *
  17. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  18. */
  19. class ResolveParameterPlaceHoldersPass extends AbstractRecursivePass
  20. {
  21. private $bag;
  22. private $resolveArrays;
  23. private $throwOnResolveException;
  24. public function __construct($resolveArrays = true, $throwOnResolveException = true)
  25. {
  26. $this->resolveArrays = $resolveArrays;
  27. $this->throwOnResolveException = $throwOnResolveException;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. *
  32. * @throws ParameterNotFoundException
  33. */
  34. public function process(ContainerBuilder $container)
  35. {
  36. $this->bag = $container->getParameterBag();
  37. try {
  38. parent::process($container);
  39. $aliases = [];
  40. foreach ($container->getAliases() as $name => $target) {
  41. $this->currentId = $name;
  42. $aliases[$this->bag->resolveValue($name)] = $target;
  43. }
  44. $container->setAliases($aliases);
  45. } catch (ParameterNotFoundException $e) {
  46. $e->setSourceId($this->currentId);
  47. throw $e;
  48. }
  49. $this->bag->resolve();
  50. $this->bag = null;
  51. }
  52. protected function processValue($value, bool $isRoot = false)
  53. {
  54. if (\is_string($value)) {
  55. try {
  56. $v = $this->bag->resolveValue($value);
  57. } catch (ParameterNotFoundException $e) {
  58. if ($this->throwOnResolveException) {
  59. throw $e;
  60. }
  61. $v = null;
  62. $this->container->getDefinition($this->currentId)->addError($e->getMessage());
  63. }
  64. return $this->resolveArrays || !$v || !\is_array($v) ? $v : $value;
  65. }
  66. if ($value instanceof Definition) {
  67. $value->setBindings($this->processValue($value->getBindings()));
  68. $changes = $value->getChanges();
  69. if (isset($changes['class'])) {
  70. $value->setClass($this->bag->resolveValue($value->getClass()));
  71. }
  72. if (isset($changes['file'])) {
  73. $value->setFile($this->bag->resolveValue($value->getFile()));
  74. }
  75. }
  76. $value = parent::processValue($value, $isRoot);
  77. if ($value && \is_array($value)) {
  78. $value = array_combine($this->bag->resolveValue(array_keys($value)), $value);
  79. }
  80. return $value;
  81. }
  82. }