|
| 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\Bundle\FrameworkBundle\Command; |
| 13 | + |
| 14 | +use Symfony\Component\Config\ConfigCache; |
| 15 | +use Symfony\Component\Config\FileLocator; |
| 16 | +use Symfony\Component\Console\Command\Command; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Output\OutputInterface; |
| 19 | +use Symfony\Component\DependencyInjection\Compiler\CheckTypeDeclarationsPass; |
| 20 | +use Symfony\Component\DependencyInjection\Compiler\PassConfig; |
| 21 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 22 | +use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
| 23 | + |
| 24 | +final class ContainerLintCommand extends Command |
| 25 | +{ |
| 26 | + protected static $defaultName = 'lint:container'; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var ContainerBuilder |
| 30 | + */ |
| 31 | + private $containerBuilder; |
| 32 | + |
| 33 | + /** |
| 34 | + * {@inheritdoc} |
| 35 | + */ |
| 36 | + protected function configure() |
| 37 | + { |
| 38 | + $this |
| 39 | + ->setDescription('Ensures that arguments injected into services match type declarations') |
| 40 | + ->setHelp('This command parses service definitions and ensures that injected values match the type declarations of each services\' class.') |
| 41 | + ; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * {@inheritdoc} |
| 46 | + */ |
| 47 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 48 | + { |
| 49 | + $container = $this->getContainerBuilder(); |
| 50 | + |
| 51 | + $container->setParameter('container.build_hash', 'lint_container'); |
| 52 | + $container->setParameter('container.build_time', time()); |
| 53 | + $container->setParameter('container.build_id', 'lint_container'); |
| 54 | + |
| 55 | + $container->addCompilerPass(new CheckTypeDeclarationsPass(true), PassConfig::TYPE_AFTER_REMOVING, -100); |
| 56 | + |
| 57 | + $container->compile(); |
| 58 | + |
| 59 | + return 0; |
| 60 | + } |
| 61 | + |
| 62 | + private function getContainerBuilder(): ContainerBuilder |
| 63 | + { |
| 64 | + if ($this->containerBuilder) { |
| 65 | + return $this->containerBuilder; |
| 66 | + } |
| 67 | + |
| 68 | + $kernel = $this->getApplication()->getKernel(); |
| 69 | + |
| 70 | + if (!$kernel->isDebug() || !(new ConfigCache($kernel->getContainer()->getParameter('debug.container.dump'), true))->isFresh()) { |
| 71 | + $buildContainer = \Closure::bind(function () { return $this->buildContainer(); }, $kernel, \get_class($kernel)); |
| 72 | + $container = $buildContainer(); |
| 73 | + $container->getCompilerPassConfig()->setRemovingPasses([]); |
| 74 | + } else { |
| 75 | + (new XmlFileLoader($container = new ContainerBuilder(), new FileLocator()))->load($kernel->getContainer()->getParameter('debug.container.dump')); |
| 76 | + } |
| 77 | + |
| 78 | + return $this->containerBuilder = $container; |
| 79 | + } |
| 80 | +} |
0 commit comments