|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 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\Input\InputArgument; |
| 15 | +use Symfony\Component\Console\Input\InputInterface; |
| 16 | +use Symfony\Component\Console\Output\OutputInterface; |
| 17 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 18 | + |
| 19 | +/** |
| 20 | + * A console command for autowiring information. |
| 21 | + * |
| 22 | + * @author Ryan Weaver <[email protected]> |
| 23 | + * |
| 24 | + * @internal |
| 25 | + */ |
| 26 | +class DebugAutowiringCommand extends ContainerDebugCommand |
| 27 | +{ |
| 28 | + protected static $defaultName = 'debug:autowiring'; |
| 29 | + |
| 30 | + /** |
| 31 | + * {@inheritdoc} |
| 32 | + */ |
| 33 | + protected function configure() |
| 34 | + { |
| 35 | + $this |
| 36 | + ->setDefinition(array( |
| 37 | + new InputArgument('search', InputArgument::OPTIONAL, 'A search filter'), |
| 38 | + )) |
| 39 | + ->setDescription('Lists classes/interfaces you can use for autowiring') |
| 40 | + ->setHelp(<<<'EOF' |
| 41 | +The <info>%command.name%</info> command displays all classes and interfaces that |
| 42 | +you can use as type-hints for autowiring: |
| 43 | +
|
| 44 | + <info>php %command.full_name%</info> |
| 45 | +
|
| 46 | +You can also pass a search term to filter the list: |
| 47 | +
|
| 48 | + <info>php %command.full_name% log</info> |
| 49 | + |
| 50 | +EOF |
| 51 | + ) |
| 52 | + ; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * {@inheritdoc} |
| 57 | + */ |
| 58 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 59 | + { |
| 60 | + $io = new SymfonyStyle($input, $output); |
| 61 | + $errorIo = $io->getErrorStyle(); |
| 62 | + |
| 63 | + $builder = $this->getContainerBuilder(); |
| 64 | + $serviceIds = $builder->getServiceIds(); |
| 65 | + $serviceIds = array_filter($serviceIds, array($this, 'filterToServiceTypes')); |
| 66 | + |
| 67 | + if ($search = $input->getArgument('search')) { |
| 68 | + $serviceIds = array_filter($serviceIds, function ($serviceId) use ($search) { |
| 69 | + return false !== stripos($serviceId, $search); |
| 70 | + }); |
| 71 | + |
| 72 | + if (empty($serviceIds)) { |
| 73 | + $errorIo->error(sprintf('No autowirable classes or interfaces found matching "%s"', $search)); |
| 74 | + |
| 75 | + return 1; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + asort($serviceIds); |
| 80 | + |
| 81 | + $io->title('Autowirable Services'); |
| 82 | + $io->text('The following classes & interfaces can be used as type-hints when autowiring:'); |
| 83 | + if ($search) { |
| 84 | + $io->text(sprintf('(only showing classes/interfaces matching <comment>%s</comment>)', $search)); |
| 85 | + } |
| 86 | + $io->newLine(); |
| 87 | + $tableRows = array(); |
| 88 | + foreach ($serviceIds as $serviceId) { |
| 89 | + $tableRows[] = array(sprintf('<fg=cyan>%s</fg=cyan>', $serviceId)); |
| 90 | + if ($builder->hasAlias($serviceId)) { |
| 91 | + $tableRows[] = array(sprintf(' alias to %s', $builder->getAlias($serviceId))); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + $io->table(array(), $tableRows); |
| 96 | + } |
| 97 | +} |
0 commit comments