|
| 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 | +} |
0 commit comments