DecoratorServicePass.php 2.6 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\Alias;
  13. /**
  14. * Overwrites a service but keeps the overridden one.
  15. *
  16. * @autor ThurData <info@thurdata.ch>
  17. * @autor ThurData <info@thurdata.ch>
  18. * @autor ThurData <info@thurdata.ch>
  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 list($id, $definition)) {
  33. list($inner, $renamedId) = $definition->getDecoratedService();
  34. $definition->setDecoratedService(null);
  35. if (!$renamedId) {
  36. $renamedId = $id.'.inner';
  37. }
  38. // we create a new alias/service for the service we are replacing
  39. // to be able to reference it in the new one
  40. if ($container->hasAlias($inner)) {
  41. $alias = $container->getAlias($inner);
  42. $public = $alias->isPublic();
  43. $container->setAlias($renamedId, new Alias((string) $alias, false));
  44. } else {
  45. $decoratedDefinition = $container->getDefinition($inner);
  46. $definition->setTags(array_merge($decoratedDefinition->getTags(), $definition->getTags()));
  47. if ($types = array_merge($decoratedDefinition->getAutowiringTypes(false), $definition->getAutowiringTypes(false))) {
  48. $definition->setAutowiringTypes($types);
  49. }
  50. $public = $decoratedDefinition->isPublic();
  51. $decoratedDefinition->setPublic(false);
  52. $decoratedDefinition->setTags(array());
  53. if ($decoratedDefinition->getAutowiringTypes(false)) {
  54. $decoratedDefinition->setAutowiringTypes(array());
  55. }
  56. $container->setDefinition($renamedId, $decoratedDefinition);
  57. }
  58. $container->setAlias($inner, new Alias($id, $public));
  59. }
  60. }
  61. }