DecoratorServicePass.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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\Alias;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. /**
  14. * Overwrites a service but keeps the overridden one.
  15. *
  16. * @author Christophe Coevoet <stof@notk.org>
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. * @author Diego Saint Esteben <diego@saintesteben.me>
  19. */
  20. class DecoratorServicePass implements CompilerPassInterface
  21. {
  22. public function process(ContainerBuilder $container)
  23. {
  24. $definitions = new \SplPriorityQueue();
  25. $order = PHP_INT_MAX;
  26. foreach ($container->getDefinitions() as $id => $definition) {
  27. if (!$decorated = $definition->getDecoratedService()) {
  28. continue;
  29. }
  30. $definitions->insert(array($id, $definition), array($decorated[2], --$order));
  31. }
  32. foreach ($definitions as $arr) {
  33. list($id, $definition) = $arr;
  34. list($inner, $renamedId) = $definition->getDecoratedService();
  35. $definition->setDecoratedService(null);
  36. if (!$renamedId) {
  37. $renamedId = $id.'.inner';
  38. }
  39. // we create a new alias/service for the service we are replacing
  40. // to be able to reference it in the new one
  41. if ($container->hasAlias($inner)) {
  42. $alias = $container->getAlias($inner);
  43. $public = $alias->isPublic();
  44. $container->setAlias($renamedId, new Alias((string) $alias, false));
  45. } else {
  46. $decoratedDefinition = $container->getDefinition($inner);
  47. $definition->setTags(array_merge($decoratedDefinition->getTags(), $definition->getTags()));
  48. $definition->setAutowiringTypes(array_merge($decoratedDefinition->getAutowiringTypes(), $definition->getAutowiringTypes()));
  49. $public = $decoratedDefinition->isPublic();
  50. $decoratedDefinition->setPublic(false);
  51. $decoratedDefinition->setTags(array());
  52. $decoratedDefinition->setAutowiringTypes(array());
  53. $container->setDefinition($renamedId, $decoratedDefinition);
  54. }
  55. $container->setAlias($inner, new Alias($id, $public));
  56. }
  57. }
  58. }