ResolveNoPreloadPass.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\Definition;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. /**
  15. * Propagate the "container.no_preload" tag.
  16. *
  17. * @author Nicolas Grekas <p@tchwork.com>
  18. */
  19. class ResolveNoPreloadPass extends AbstractRecursivePass
  20. {
  21. private const DO_PRELOAD_TAG = '.container.do_preload';
  22. private $tagName;
  23. private $resolvedIds = [];
  24. public function __construct(string $tagName = 'container.no_preload')
  25. {
  26. $this->tagName = $tagName;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function process(ContainerBuilder $container)
  32. {
  33. $this->container = $container;
  34. try {
  35. foreach ($container->getDefinitions() as $id => $definition) {
  36. if ($definition->isPublic() && !$definition->isPrivate() && !isset($this->resolvedIds[$id])) {
  37. $this->resolvedIds[$id] = true;
  38. $this->processValue($definition, true);
  39. }
  40. }
  41. foreach ($container->getAliases() as $alias) {
  42. if ($alias->isPublic() && !$alias->isPrivate() && !isset($this->resolvedIds[$id = (string) $alias]) && $container->hasDefinition($id)) {
  43. $this->resolvedIds[$id] = true;
  44. $this->processValue($container->getDefinition($id), true);
  45. }
  46. }
  47. } finally {
  48. $this->resolvedIds = [];
  49. $this->container = null;
  50. }
  51. foreach ($container->getDefinitions() as $definition) {
  52. if ($definition->hasTag(self::DO_PRELOAD_TAG)) {
  53. $definition->clearTag(self::DO_PRELOAD_TAG);
  54. } elseif (!$definition->isDeprecated() && !$definition->hasErrors()) {
  55. $definition->addTag($this->tagName);
  56. }
  57. }
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. protected function processValue($value, bool $isRoot = false)
  63. {
  64. if ($value instanceof Reference && ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE !== $value->getInvalidBehavior() && $this->container->hasDefinition($id = (string) $value)) {
  65. $definition = $this->container->getDefinition($id);
  66. if (!isset($this->resolvedIds[$id]) && (!$definition->isPublic() || $definition->isPrivate())) {
  67. $this->resolvedIds[$id] = true;
  68. $this->processValue($definition, true);
  69. }
  70. return $value;
  71. }
  72. if (!$value instanceof Definition) {
  73. return parent::processValue($value, $isRoot);
  74. }
  75. if ($value->hasTag($this->tagName) || $value->isDeprecated() || $value->hasErrors()) {
  76. return $value;
  77. }
  78. if ($isRoot) {
  79. $value->addTag(self::DO_PRELOAD_TAG);
  80. }
  81. return parent::processValue($value, $isRoot);
  82. }
  83. }