|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\DependencyInjection\Compiler; |
| 13 | + |
| 14 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 15 | +use Symfony\Component\DependencyInjection\Definition; |
| 16 | +use Symfony\Component\DependencyInjection\Reference; |
| 17 | + |
| 18 | +/** |
| 19 | + * Propagate the "container.no_preload" tag. |
| 20 | + * |
| 21 | + * @author Nicolas Grekas <p@tchwork.com> |
| 22 | + */ |
| 23 | +class ResolveNoPreloadPass extends AbstractRecursivePass |
| 24 | +{ |
| 25 | + private const DO_PRELOAD_TAG = '.container.do_preload'; |
| 26 | + |
| 27 | + private $tagName; |
| 28 | + private $resolvedIds = []; |
| 29 | + |
| 30 | + public function __construct(string $tagName = 'container.no_preload') |
| 31 | + { |
| 32 | + $this->tagName = $tagName; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * {@inheritdoc} |
| 37 | + */ |
| 38 | + public function process(ContainerBuilder $container) |
| 39 | + { |
| 40 | + $this->container = $container; |
| 41 | + |
| 42 | + try { |
| 43 | + foreach ($container->getDefinitions() as $id => $definition) { |
| 44 | + if ($definition->isPublic() && !$definition->isPrivate() && !isset($this->resolvedIds[$id])) { |
| 45 | + $this->resolvedIds[$id] = true; |
| 46 | + $this->processValue($definition, true); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + foreach ($container->getAliases() as $alias) { |
| 51 | + if ($alias->isPublic() && !$alias->isPrivate() && !isset($this->resolvedIds[$id = (string) $alias]) && $container->has($id)) { |
| 52 | + $this->resolvedIds[$id] = true; |
| 53 | + $this->processValue($container->getDefinition($id), true); |
| 54 | + } |
| 55 | + } |
| 56 | + } finally { |
| 57 | + $this->resolvedIds = []; |
| 58 | + $this->container = null; |
| 59 | + } |
| 60 | + |
| 61 | + foreach ($container->getDefinitions() as $definition) { |
| 62 | + if ($definition->hasTag(self::DO_PRELOAD_TAG)) { |
| 63 | + $definition->clearTag(self::DO_PRELOAD_TAG); |
| 64 | + } elseif (!$definition->isDeprecated() && !$definition->hasErrors()) { |
| 65 | + $definition->addTag($this->tagName); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * {@inheritdoc} |
| 72 | + */ |
| 73 | + protected function processValue($value, bool $isRoot = false) |
| 74 | + { |
| 75 | + if ($value instanceof Reference && ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE !== $value->getInvalidBehavior() && $this->container->has($id = (string) $value)) { |
| 76 | + $definition = $this->container->findDefinition($id); |
| 77 | + |
| 78 | + if (!isset($this->resolvedIds[$id])) { |
| 79 | + $this->resolvedIds[$id] = true; |
| 80 | + $this->processValue($definition, true); |
| 81 | + } |
| 82 | + |
| 83 | + return $value; |
| 84 | + } |
| 85 | + |
| 86 | + if (!$value instanceof Definition) { |
| 87 | + return parent::processValue($value, $isRoot); |
| 88 | + } |
| 89 | + |
| 90 | + if ($value->hasTag($this->tagName) || $value->isDeprecated() || $value->hasErrors()) { |
| 91 | + return $value; |
| 92 | + } |
| 93 | + |
| 94 | + if ($isRoot) { |
| 95 | + $value->addTag(self::DO_PRELOAD_TAG); |
| 96 | + } |
| 97 | + |
| 98 | + return parent::processValue($value, $isRoot); |
| 99 | + } |
| 100 | +} |
0 commit comments