MergeExtensionConfigurationPass.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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\Extension\PrependExtensionInterface;
  13. /**
  14. * Merges extension configs into the container builder
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class MergeExtensionConfigurationPass implements CompilerPassInterface
  19. {
  20. /**
  21. * {@inheritDoc}
  22. */
  23. public function process(ContainerBuilder $container)
  24. {
  25. $parameters = $container->getParameterBag()->all();
  26. $definitions = $container->getDefinitions();
  27. $aliases = $container->getAliases();
  28. foreach ($container->getExtensions() as $extension) {
  29. if ($extension instanceof PrependExtensionInterface) {
  30. $extension->prepend($container);
  31. }
  32. }
  33. foreach ($container->getExtensions() as $name => $extension) {
  34. if (!$config = $container->getExtensionConfig($name)) {
  35. // this extension was not called
  36. continue;
  37. }
  38. $config = $container->getParameterBag()->resolveValue($config);
  39. $tmpContainer = new ContainerBuilder($container->getParameterBag());
  40. $tmpContainer->setResourceTracking($container->isTrackingResources());
  41. $tmpContainer->addObjectResource($extension);
  42. $extension->load($config, $tmpContainer);
  43. $container->merge($tmpContainer);
  44. }
  45. $container->addDefinitions($definitions);
  46. $container->addAliases($aliases);
  47. $container->getParameterBag()->add($parameters);
  48. }
  49. }