Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 89e3c76

Browse files
committed
[FrameworkBundle][DependencyInjection] Skip removed ids in the lint container command and its associated pass
1 parent 25494fa commit 89e3c76

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

src/Symfony/Bundle/FrameworkBundle/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
}

src/Symfony/Component/DependencyInjection/Compiler/CheckTypeDeclarationsPass.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,30 @@ final class CheckTypeDeclarationsPass extends AbstractRecursivePass
4242
private const SCALAR_TYPES = ['int', 'float', 'bool', 'string'];
4343

4444
private $autoload;
45+
private $skippedIds;
4546

4647
private $expressionLanguage;
4748

4849
/**
4950
* @param bool $autoload Whether services who's class in not loaded should be checked or not.
5051
* Defaults to false to save loading code during compilation.
52+
* @param array $skippedIds An array indexed by the service ids to skip and with truthy values.
5153
*/
52-
public function __construct(bool $autoload = false)
54+
public function __construct(bool $autoload = false, array $skippedIds = [])
5355
{
5456
$this->autoload = $autoload;
57+
$this->skippedIds = $skippedIds;
5558
}
5659

5760
/**
5861
* {@inheritdoc}
5962
*/
6063
protected function processValue($value, $isRoot = false)
6164
{
65+
if (isset($this->skippedIds[$this->currentId])) {
66+
return parent::processValue($value, $isRoot);
67+
}
68+
6269
if (!$value instanceof Definition || $value->hasErrors()) {
6370
return parent::processValue($value, $isRoot);
6471
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/CheckTypeDeclarationsPassTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,4 +669,16 @@ public function testProcessHandleNotFoundEnvPlaceholder()
669669

670670
$this->addToAssertionCount(1);
671671
}
672+
673+
public function testProcessSkipSkippedIds()
674+
{
675+
$container = new ContainerBuilder();
676+
$container
677+
->register('foobar', BarMethodCall::class)
678+
->addMethodCall('setArray', ['a string']);
679+
680+
(new CheckTypeDeclarationsPass(true, ['foobar' => true]))->process($container);
681+
682+
$this->addToAssertionCount(1);
683+
}
672684
}

0 commit comments

Comments
 (0)