|
| 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\Console\Command\Command; |
| 15 | +use Symfony\Component\Console\Input\InputArgument; |
| 16 | +use Symfony\Component\Console\Input\InputInterface; |
| 17 | +use Symfony\Component\Console\Input\InputOption; |
| 18 | +use Symfony\Component\Console\Output\OutputInterface; |
| 19 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 20 | +use Symfony\Component\DependencyInjection\ChildDefinition; |
| 21 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 22 | +use Symfony\Component\DependencyInjection\Dumper\YamlDumper; |
| 23 | +use Symfony\Component\VarDumper\Cloner\VarCloner; |
| 24 | +use Symfony\Component\VarDumper\Dumper\AbstractDumper; |
| 25 | +use Symfony\Component\VarDumper\Dumper\CliDumper; |
| 26 | + |
| 27 | +/** |
| 28 | + * A console command for autoconfiguration information. |
| 29 | + * |
| 30 | + * @internal |
| 31 | + */ |
| 32 | +final class DebugAutoconfigurationCommand extends Command |
| 33 | +{ |
| 34 | + protected static $defaultName = 'debug:autoconfiguration'; |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + */ |
| 39 | + protected function configure() |
| 40 | + { |
| 41 | + $this |
| 42 | + ->setDefinition([ |
| 43 | + new InputArgument('search', InputArgument::OPTIONAL, 'A search filter'), |
| 44 | + new InputOption('tags', null, InputOption::VALUE_NONE, 'Displays autoconfiguration interfaces/class grouped by tags'), |
| 45 | + ]) |
| 46 | + ->setDescription('Displays current autoconfiguration for an application') |
| 47 | + ->setHelp(<<<'EOF' |
| 48 | +The <info>%command.name%</info> command displays all services that |
| 49 | +are autoconfigured: |
| 50 | +
|
| 51 | + <info>php %command.full_name%</info> |
| 52 | +
|
| 53 | +You can also pass a search term to filter the list: |
| 54 | +
|
| 55 | + <info>php %command.full_name% log</info> |
| 56 | + |
| 57 | +EOF |
| 58 | + ) |
| 59 | + ; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * {@inheritdoc} |
| 64 | + */ |
| 65 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 66 | + { |
| 67 | + $io = new SymfonyStyle($input, $output); |
| 68 | + $errorIo = $io->getErrorStyle(); |
| 69 | + |
| 70 | + $autoconfiguredInstanceofItems = $this->getContainerBuilder()->getAutoconfiguredInstanceof(); |
| 71 | + |
| 72 | + if ($search = $input->getArgument('search')) { |
| 73 | + $autoconfiguredInstanceofItems = array_filter($autoconfiguredInstanceofItems, function ($key) use ($search) { |
| 74 | + return false !== stripos(str_replace('\\', '', $key), $search); |
| 75 | + }, ARRAY_FILTER_USE_KEY); |
| 76 | + |
| 77 | + if (!$autoconfiguredInstanceofItems) { |
| 78 | + $errorIo->error(sprintf('No autoconfiguration interface/class found matching "%s"', $search)); |
| 79 | + |
| 80 | + return 1; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + ksort($autoconfiguredInstanceofItems, SORT_NATURAL); |
| 85 | + |
| 86 | + $io->title('Autoconfiguration'); |
| 87 | + if ($search) { |
| 88 | + $io->text(sprintf('(only showing classes/interfaces matching <comment>%s</comment>)', $search)); |
| 89 | + } |
| 90 | + $io->newLine(); |
| 91 | + |
| 92 | + /** @var ChildDefinition $autoconfiguredInstanceofItem */ |
| 93 | + foreach ($autoconfiguredInstanceofItems as $key => $autoconfiguredInstanceofItem) { |
| 94 | + $tableRows = []; |
| 95 | + |
| 96 | + foreach ($autoconfiguredInstanceofItem->getTags() as $tag => $tagAttributes) { |
| 97 | + $tableRows[] = ['Tag', $tag]; |
| 98 | + if ($tagAttributes !== [[]]) { |
| 99 | + $tableRows[] = ['Tag attribute', $this->dumpTagAttribute($tagAttributes)]; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if ($autoconfiguredInstanceofItem->getMethodCalls()) { |
| 104 | + $tableRows[] = ['Method call', $this->dumpMethodCall($autoconfiguredInstanceofItem)]; |
| 105 | + } |
| 106 | + |
| 107 | + if ($autoconfiguredInstanceofItem->getBindings()) { |
| 108 | + $tableRows[] = ['Bindings', $this->dumpBindings($autoconfiguredInstanceofItem)]; |
| 109 | + } |
| 110 | + |
| 111 | + $io->title(sprintf('Autoconfiguration for "%s"', $key)); |
| 112 | + $io->newLine(); |
| 113 | + $io->table(['Option', 'Value'], $tableRows); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private function dumpMethodCall(ChildDefinition $autoconfiguredInstanceofItem) |
| 118 | + { |
| 119 | + $tagContainerBuilder = new ContainerBuilder(); |
| 120 | + foreach ($tagContainerBuilder->getServiceIds() as $serviceId) { |
| 121 | + $tagContainerBuilder->removeDefinition($serviceId); |
| 122 | + $tagContainerBuilder->removeAlias($serviceId); |
| 123 | + } |
| 124 | + $tagContainerBuilder->addDefinitions([$autoconfiguredInstanceofItem]); |
| 125 | + |
| 126 | + $dumper = new YamlDumper($tagContainerBuilder); |
| 127 | + preg_match('/calls\:\n((?: +- .+\n)+)/', $dumper->dump(), $matches); |
| 128 | + |
| 129 | + return preg_replace('/^\s+/m', '', $matches[1]); |
| 130 | + } |
| 131 | + |
| 132 | + private function dumpBindings(ChildDefinition $autoconfiguredInstanceofItem) |
| 133 | + { |
| 134 | + $tagContainerBuilder = new ContainerBuilder(); |
| 135 | + foreach ($tagContainerBuilder->getServiceIds() as $serviceId) { |
| 136 | + $tagContainerBuilder->removeDefinition($serviceId); |
| 137 | + $tagContainerBuilder->removeAlias($serviceId); |
| 138 | + } |
| 139 | + |
| 140 | + $dumper = new YamlDumper($tagContainerBuilder); |
| 141 | + foreach ($autoconfiguredInstanceofItem->getBindings() as $bindingKey => $bindingValue) { |
| 142 | + $tagContainerBuilder->setParameter($bindingKey, $bindingValue->getValues()[0]); |
| 143 | + } |
| 144 | + |
| 145 | + preg_match('/parameters\:\n((?: + .+\n)+)/', $dumper->dump(), $matches); |
| 146 | + |
| 147 | + return preg_replace('/^\s+/m', '', $matches[1]); |
| 148 | + } |
| 149 | + |
| 150 | + private function dumpTagAttribute(array $tagAttribute) |
| 151 | + { |
| 152 | + $cloner = new VarCloner(); |
| 153 | + $cliDumper = new CliDumper(null, null, AbstractDumper::DUMP_LIGHT_ARRAY); |
| 154 | + |
| 155 | + return $cliDumper->dump($cloner->cloneVar(current($tagAttribute)), true); |
| 156 | + } |
| 157 | + |
| 158 | + private function getContainerBuilder(): ContainerBuilder |
| 159 | + { |
| 160 | + $kernel = $this->getApplication()->getKernel(); |
| 161 | + $buildContainer = \Closure::bind(function () { return $this->buildContainer(); }, $kernel, \get_class($kernel)); |
| 162 | + $container = $buildContainer(); |
| 163 | + $container->getCompilerPassConfig()->setRemovingPasses([]); |
| 164 | + $container->getCompilerPassConfig()->setAfterRemovingPasses([]); |
| 165 | + $container->compile(); |
| 166 | + |
| 167 | + return $container; |
| 168 | + } |
| 169 | +} |
0 commit comments