ResolveParameterPlaceHoldersPass.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\Exception\ParameterNotFoundException;
  13. /**
  14. * Resolves all parameter placeholders "%somevalue%" to their real values.
  15. *
  16. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  17. */
  18. class ResolveParameterPlaceHoldersPass implements CompilerPassInterface
  19. {
  20. /**
  21. * Processes the ContainerBuilder to resolve parameter placeholders.
  22. *
  23. * @throws ParameterNotFoundException
  24. */
  25. public function process(ContainerBuilder $container)
  26. {
  27. $parameterBag = $container->getParameterBag();
  28. foreach ($container->getDefinitions() as $id => $definition) {
  29. try {
  30. $definition->setClass($parameterBag->resolveValue($definition->getClass()));
  31. $definition->setFile($parameterBag->resolveValue($definition->getFile()));
  32. $definition->setArguments($parameterBag->resolveValue($definition->getArguments()));
  33. if ($definition->getFactoryClass(false)) {
  34. $definition->setFactoryClass($parameterBag->resolveValue($definition->getFactoryClass(false)));
  35. }
  36. $factory = $definition->getFactory();
  37. if (\is_array($factory) && isset($factory[0])) {
  38. $factory[0] = $parameterBag->resolveValue($factory[0]);
  39. $definition->setFactory($factory);
  40. }
  41. $calls = array();
  42. foreach ($definition->getMethodCalls() as $name => $arguments) {
  43. $calls[$parameterBag->resolveValue($name)] = $parameterBag->resolveValue($arguments);
  44. }
  45. $definition->setMethodCalls($calls);
  46. $definition->setProperties($parameterBag->resolveValue($definition->getProperties()));
  47. } catch (ParameterNotFoundException $e) {
  48. $e->setSourceId($id);
  49. throw $e;
  50. }
  51. }
  52. $aliases = array();
  53. foreach ($container->getAliases() as $name => $target) {
  54. $aliases[$parameterBag->resolveValue($name)] = $target;
  55. }
  56. $container->setAliases($aliases);
  57. $parameterBag->resolve();
  58. }
  59. }