|
| 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\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper; |
| 15 | +use Symfony\Component\Config\ConfigCache; |
| 16 | +use Symfony\Component\Console\Input\InputArgument; |
| 17 | +use Symfony\Component\Console\Input\InputOption; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 21 | +use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
| 22 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 23 | +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
| 24 | +use Symfony\Component\Config\FileLocator; |
| 25 | + |
| 26 | +/** |
| 27 | + * A console command for autowiring information. |
| 28 | + * |
| 29 | + * @author Ryan Weaver <ryan@knpuniversity.com> |
| 30 | + * |
| 31 | + * @internal |
| 32 | + */ |
| 33 | +class DebugAutowiringCommand extends ContainerDebugCommand |
| 34 | +{ |
| 35 | + protected static $defaultName = 'debug:autowiring'; |
| 36 | + |
| 37 | + /** |
| 38 | + * {@inheritdoc} |
| 39 | + */ |
| 40 | + protected function configure() |
| 41 | + { |
| 42 | + $this |
| 43 | + ->setDe
10000
finition(array( |
| 44 | + new InputArgument('search', InputArgument::OPTIONAL, 'A search filter'), |
| 45 | + )) |
| 46 | + ->setDescription('Lists classes/interfaces you can use for autowiring') |
| 47 | + ->setHelp(<<<'EOF' |
| 48 | +The <info>%command.name%</info> command displays all classes and interfaces that |
| 49 | +you can use as type-hints for autowiring: |
| 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 | + /** |
| 64 | + * {@inheritdoc} |
| 65 | + */ |
| 66 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 67 | + { |
| 68 | + $io = new SymfonyStyle($input, $output); |
| 69 | + $errorIo = $io->getErrorStyle(); |
| 70 | + |
| 71 | + $builder = $this->getContainerBuilder(); |
| 72 | + $serviceIds = $builder->getServiceIds(); |
| 73 | + $serviceIds = array_filter($serviceIds, array($this, 'filterToServiceTypes')); |
| 74 | + |
| 75 | + if ($search = $input->getArgument('search')) { |
| 76 | + $serviceIds = array_filter($serviceIds, function($serviceId) use ($search) { |
| 77 | + return stripos($serviceId, $search) !== false; |
| 78 | + }); |
| 79 | + |
| 80 | + if (empty($serviceIds)) { |
| 81 | + $errorIo->error(sprintf('No autowirable classes or interfaces found matching "%s"', $search)); |
| 82 | + |
| 83 | + return 1; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + asort($serviceIds); |
| 88 | + |
| 89 | + $io->title('Autowirable Services'); |
| 90 | + $io->text('The following classes & interfaces can be used as type-hints when autowiring:'); |
| 91 | + if ($search) { |
| 92 | + $io->text(sprintf('(only showing classes/interfaces matching <comment>%s</comment>)', $search)); |
| 93 | + } |
| 94 | + $io->newLine(); |
| 95 | + $tableRows = array(); |
| 96 | + foreach ($serviceIds as $serviceId) { |
| 97 | + $tableRows[] = array(sprintf('<fg=cyan>%s</fg=cyan>', $serviceId)); |
| 98 | + if ($builder->hasAlias($serviceId)) { |
| 99 | + $tableRows[] = array(sprintf(' alias to %s', $builder->getAlias($serviceId))); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + $io->table(array(), $tableRows); |
| 104 | + } |
| 105 | +} |
0 commit comments