10000 lint:container command · symfony/symfony@4f02c27 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4f02c27

Browse files
committed
lint:container command
1 parent cc8c4de commit 4f02c27

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+ 10000
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Command;
13+
14+
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Helper\Helper;
16+
use Symfony\Component\Console\Helper\TableSeparator;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Output\OutputInterface;
19+
use Symfony\Component\Console\Style\SymfonyStyle;
20+
use Symfony\Component\HttpKernel\Kernel;
21+
use Symfony\Component\HttpKernel\KernelInterface;
22+
use Symfony\Component\Config\ConfigCache;
23+
use Symfony\Component\Console\Exception\InvalidArgumentException;
24+
use Symfony\Component\Console\Input\InputArgument;
25+
use Symfony\Component\Console\Input\InputOption;
26+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
27+
use Symfony\Component\DependencyInjection\ContainerBuilder;
28+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
29+
use Symfony\Component\DependencyInjection\Compiler\CheckTypeHintsPass;
30+
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
31+
use Symfony\Component\Config\FileLocator;
32+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension;
33+
34+
class ContainerLintCommand extends Command
35+
{
36+
/**
37+
* @var ContainerBuilder
38+
*/
39+
private $containerBuilder;
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
protected function configure()
45+
{
46+
$this
47+
->setDescription('Lints container for services arguments type hints')
48+
;
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
protected function execute(InputInterface $input, OutputInterface $output)
55+
{
56+
$container = $this->getContainerBuilder();
57+
58+
$container->setParameter('container.build_id', 'lint_container');
59+
60+
$container->addCompilerPass(new CheckTypeHintsPass(), PassConfig::TYPE_AFTER_REMOVING);
61+
62+
$container->compile();
63+
}
64+
65+
/**
66+
* Loads the ContainerBuilder from the cache.
67+
*
68+
* @return ContainerBuilder
69+
*
70+
* @throws \LogicException
71+
*/
72+
protected function getContainerBuilder()
73+
{
74+
if ($this->containerBuilder) {
75+
return $this->containerBuilder;
76+
}
77+
78+
$kernel = $this->getApplication()->getKernel();
79+
80+
if (!$kernel->isDebug() || !(new ConfigCache($kernel->getContainer()->getParameter('debug.container.dump'), true))->isFresh()) {
81+
$buildContainer = \Closure::bind(function () { return $this->buildContainer(); }, $kernel, get_class($kernel));
82+
$container = $buildContainer();
83+
$container->getCompilerPassConfig()->setRemovingPasses(array());
84+
$container->compile();
85+
} else {
86+
(new XmlFileLoader($container = new ContainerBuilder(), new FileLocator()))->load($kernel->getContainer()->getParameter('debug.container.dump'));
87+
}
88+
89+
return $this->containerBuilder = $container;
90+
}
91+
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
<tag name="console.command" command="debug:container" />
6161
</service>
6262

63+
<service id="console.command.container_lint" class="Symfony\Bundle\FrameworkBundle\Command\ContainerLintCommand">
64+
<tag name="console.command" command="lint:container" />
65+
</service>
66+
6367
<service id="console.command.debug_autowiring" class="Symfony\Bundle\FrameworkBundle\Command\DebugAutowiringCommand">
6468
<tag name="console.command" command="debug:autowiring" />
6569
</service>

src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\ContainerInterface;
1616
use Symfony\Component\DependencyInjection\ResettableContainerInterface;
17+
use Symfony\Component\DependencyInjection\Compiler\CheckTypeHintsPass;
1718
use Symfony\Component\HttpKernel\KernelInterface;
1819

1920
/**
@@ -64,7 +65,10 @@ protected static function bootKernel(array $options = array())
6465
static::ensureKernelShutdown();
6566

6667
static::$kernel = static::createKernel($options);
68+
69+
6770
static::$kernel->boot();
71+
static::$kernel->getContainer()->setParameter('test.register_check_type_hints_pass', true);
6872

6973
$container = static::$kernel->getContainer();
7074
static::$container = $container->has('test.service_container') ? $container->get('test.service_container') : $container;
@@ -126,6 +130,14 @@ protected static function ensureKernelShutdown()
126130
static::$container = null;
127131
}
128132

133+
protected static function checkInjectionsTypeHints(array $options = array())
134+
{
135+
static::bootKernel($options);
136+
137+
138+
return static::$kernel;
139+
}
140+
129141
/**
130142
* Clean up Kernel usage in this test.
131143
*/

0 commit comments

Comments
 (0)
0