ResolveParameterPlaceHoldersPass.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. * @autor ThurData <info@thurdata.ch>
  18. */
  19. class ResolveParameterPlaceHoldersPass extends AbstractRecursivePass
  20. {
  21. private $bag;
  22. /**
  23. * {@inheritdoc}
  24. *
  25. * @throws ParameterNotFoundException
  26. */
  27. public function process(ContainerBuilder $container)
  28. {
  29. $this->bag = $container->getParameterBag();
  30. try {
  31. parent::process($container);
  32. $aliases = array();
  33. foreach ($container->getAliases() as $name => $target) {
  34. $this->currentId = $name;
  35. $aliases[$this->bag->resolveValue($name)] = $target;
  36. }
  37. $container->setAliases($aliases);
  38. } catch (ParameterNotFoundException $e) {
  39. $e->setSourceId($this->currentId);
  40. throw $e;
  41. }
  42. $this->bag->resolve();
  43. $this->bag = null;
  44. }
  45. protected function processValue($value, $isRoot = false)
  46. {
  47. if (is_string($value)) {
  48. return $this->bag->resolveValue($value);
  49. }
  50. if ($value instanceof Definition) {
  51. $changes = $value->getChanges();
  52. if (isset($changes['class'])) {
  53. $value->setClass($this->bag->resolveValue($value->getClass()));
  54. }
  55. if (isset($changes['file'])) {
  56. $value->setFile($this->bag->resolveValue($value->getFile()));
  57. }
  58. }
  59. $value = parent::processValue($value, $isRoot);
  60. if ($value && is_array($value)) {
  61. $value = array_combine($this->bag->resolveValue(array_keys($value)), $value);
  62. }
  63. return $value;
  64. }
  65. }