ResolveHotPathPass.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\Argument\ArgumentInterface;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Definition;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. /**
  16. * Propagate "container.hot_path" tags to referenced services.
  17. *
  18. * @author Nicolas Grekas <p@tchwork.com>
  19. */
  20. class ResolveHotPathPass extends AbstractRecursivePass
  21. {
  22. private $tagName;
  23. private $resolvedIds = [];
  24. public function __construct(string $tagName = 'container.hot_path')
  25. {
  26. $this->tagName = $tagName;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function process(ContainerBuilder $container)
  32. {
  33. try {
  34. parent::process($container);
  35. $container->getDefinition('service_container')->clearTag($this->tagName);
  36. } finally {
  37. $this->resolvedIds = [];
  38. }
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. protected function processValue($value, bool $isRoot = false)
  44. {
  45. if ($value instanceof ArgumentInterface) {
  46. return $value;
  47. }
  48. if ($value instanceof Definition && $isRoot) {
  49. if ($value->isDeprecated()) {
  50. return $value->clearTag($this->tagName);
  51. }
  52. $this->resolvedIds[$this->currentId] = true;
  53. if (!$value->hasTag($this->tagName)) {
  54. return $value;
  55. }
  56. }
  57. if ($value instanceof Reference && ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE !== $value->getInvalidBehavior() && $this->container->hasDefinition($id = (string) $value)) {
  58. $definition = $this->container->getDefinition($id);
  59. if ($definition->isDeprecated() || $definition->hasTag($this->tagName)) {
  60. return $value;
  61. }
  62. $definition->addTag($this->tagName);
  63. if (isset($this->resolvedIds[$id])) {
  64. parent::processValue($definition, false);
  65. }
  66. return $value;
  67. }
  68. return parent::processValue($value, $isRoot);
  69. }
  70. }