Skip to content

Commit 95498f2

Browse files
bug #34935 [FrameworkBundle][DependencyInjection] Skip removed ids in the lint container command and its associated pass (fancyweb)
This PR was merged into the 4.4 branch. Discussion ---------- [FrameworkBundle][DependencyInjection] Skip removed ids in the lint container command and its associated pass | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | symfony/symfony#34858 | License | MIT | Doc PR | - We remove the "removing" passes again and to avoid what symfony/symfony#34502 fixed, we skip validating the "live" container removed ids in the pass (the "live" container is supposed to have the same definitions than the "debug container" one). Logically, an errored service cannot pass the "live" container compilation without being removed. Consequently, it also skips the errored services that ended up being removed in the "live" container. Commits ------- a0f581ba9d [FrameworkBundle][DependencyInjection] Skip removed ids in the lint container command and its associated pass
2 parents 78929e9 + 46398b4 commit 95498f2

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

Command/ContainerLintCommand.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
use Symfony\Component\Config\ConfigCache;
1515
use Symfony\Component\Config\FileLocator;
1616
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Exception\RuntimeException;
1718
use Symfony\Component\Console\Input\InputInterface;
1819
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Component\Console\Style\SymfonyStyle;
1921
use Symfony\Component\DependencyInjection\Compiler\CheckTypeDeclarationsPass;
2022
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
23+
use Symfony\Component\DependencyInjection\Container;
2124
use Symfony\Component\DependencyInjection\ContainerBuilder;
2225
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2326
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
27+
use Symfony\Component\HttpKernel\Kernel;
2428

2529
final class ContainerLintCommand extends Command
2630
{
@@ -47,13 +51,18 @@ protected function configure()
4751
*/
4852
protected function execute(InputInterface $input, OutputInterface $output): int
4953
{
50-
$container = $this->getContainerBuilder();
54+
$io = new SymfonyStyle($input, $output);
55+
$errorIo = $io->getErrorStyle();
5156

52-
$container->setParameter('container.build_hash', 'lint_container');
53-
$container->setParameter('container.build_time', time());
54-
$container->setParameter('container.build_id', 'lint_container');
57+
try {
58+
$container = $this->getContainerBuilder();
59+
} catch (RuntimeException $e) {
60+
$errorIo->error($e->getMessage());
61+
62+
return 2;
63+
}
5564

56-
$container->addCompilerPass(new CheckTypeDeclarationsPass(true), PassConfig::TYPE_AFTER_REMOVING, -100);
65+
$container->setParameter('container.build_time', time());
5766

5867
$container->compile();
5968

@@ -67,22 +76,44 @@ private function getContainerBuilder(): ContainerBuilder
6776
}
6877

6978
$kernel = $this->getApplication()->getKernel();
79+
$kernelContainer = $kernel->getContainer();
80+
81+
if (!$kernel->isDebug() || !(new ConfigCache($kernelContainer->getParameter('debug.container.dump'), true))->isFresh()) {
82+
if (!$kernel instanceof Kernel) {
83+
throw new RuntimeException("This command does not support the console application's kernel.");
84+
}
7085

71-
if (!$kernel->isDebug() || !(new ConfigCache($kernel->getContainer()->getParameter('debug.container.dump'), true))->isFresh()) {
7286
$buildContainer = \Closure::bind(function (): ContainerBuilder {
7387
$this->initializeBundles();
7488

7589
return $this->buildContainer();
7690
}, $kernel, \get_class($kernel));
7791
$container = $buildContainer();
92+
93+
$skippedIds = [];
7894
} else {
79-
(new XmlFileLoader($container = new ContainerBuilder($parameterBag = new EnvPlaceholderParameterBag()), new FileLocator()))->load($kernel->getContainer()->getParameter('debug.container.dump'));
95+
if (!$kernelContainer instanceof Container) {
96+
throw new RuntimeException("This command does not support the console application kernel's container.");
97+
}
98+
99+
(new XmlFileLoader($container = new ContainerBuilder($parameterBag = new EnvPlaceholderParameterBag()), new FileLocator()))->load($kernelContainer->getParameter('debug.container.dump'));
80100

81101
$refl = new \ReflectionProperty($parameterBag, 'resolved');
82102
$refl->setAccessible(true);
83103
$refl->setValue($parameterBag, true);
104+
105+
$passConfig = $container->getCompilerPassConfig();
106+
$passConfig->setRemovingPasses([]);
107+
$passConfig->setAfterRemovingPasses([]);
108+
109+
$skippedIds = $kernelContainer->getRemovedIds();
84110
}
85111

112+
$container->setParameter('container.build_hash', 'lint_container');
113+
$container->setParameter('container.build_id', 'lint_container');
114+
115+
$container->addCompilerPass(new CheckTypeDeclarationsPass(true, $skippedIds), PassConfig::TYPE_AFTER_REMOVING, -100);
116+
86117
return $this->containerBuilder = $container;
87118
}
88119
}

0 commit comments

Comments
 (0)