[FrameworkBundle] make debug:autowiring list useful services and their description

This commit is contained in:
Nicolas Grekas 2018-10-24 15:59:47 +02:00
parent 8c24c35fe8
commit 56aab09b01
5 changed files with 42 additions and 32 deletions

View File

@ -261,7 +261,7 @@ EOF
public function filterToServiceTypes($serviceId) public function filterToServiceTypes($serviceId)
{ {
// filter out things that could not be valid class names // filter out things that could not be valid class names
if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $serviceId)) { if (!preg_match('/(?(DEFINE)(?<V>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+))^(?&V)(?:\\\\(?&V))*+(?: \$(?&V))?$/', $serviceId)) {
return false; return false;
} }
@ -270,13 +270,6 @@ EOF
return true; return true;
} }
try { return class_exists($serviceId) || interface_exists($serviceId, false);
new \ReflectionClass($serviceId);
return true;
} catch (\ReflectionException $e) {
// the service id is not a valid class/interface
return false;
}
} }
} }

View File

@ -11,8 +11,10 @@
namespace Symfony\Bundle\FrameworkBundle\Command; namespace Symfony\Bundle\FrameworkBundle\Command;
use Symfony\Bundle\FrameworkBundle\Console\Descriptor\Descriptor;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Style\SymfonyStyle;
@ -35,10 +37,11 @@ class DebugAutowiringCommand extends ContainerDebugCommand
$this $this
->setDefinition(array( ->setDefinition(array(
new InputArgument('search', InputArgument::OPTIONAL, 'A search filter'), new InputArgument('search', InputArgument::OPTIONAL, 'A search filter'),
new InputOption('all', null, InputOption::VALUE_NONE, 'Show also services that are not aliased'),
)) ))
->setDescription('Lists classes/interfaces you can use for autowiring') ->setDescription('Lists classes/interfaces you can use for autowiring')
->setHelp(<<<'EOF' ->setHelp(<<<'EOF'
The <info>%command.name%</info> command displays all classes and interfaces that The <info>%command.name%</info> command displays the classes and interfaces that
you can use as type-hints for autowiring: you can use as type-hints for autowiring:
<info>php %command.full_name%</info> <info>php %command.full_name%</info>
@ -76,26 +79,39 @@ EOF
} }
} }
asort($serviceIds); uasort($serviceIds, 'strnatcmp');
$io->title('Autowirable Services'); $io->title('Autowirable Types');
$io->text('The following classes & interfaces can be used as type-hints when autowiring:'); $io->text('The following classes & interfaces can be used as type-hints when autowiring:');
if ($search) { if ($search) {
$io->text(sprintf('(only showing classes/interfaces matching <comment>%s</comment>)', $search)); $io->text(sprintf('(only showing classes/interfaces matching <comment>%s</comment>)', $search));
} }
$io->newLine();
$tableRows = array(); $tableRows = array();
$hasAlias = array(); $hasAlias = array();
$all = $input->getOption('all');
$previousId = '-';
foreach ($serviceIds as $serviceId) { foreach ($serviceIds as $serviceId) {
$text = array();
if (0 !== strpos($serviceId, $previousId)) {
$text[] = '';
if ('' !== $description = Descriptor::getClassDescription($serviceId, $serviceId)) {
if (isset($hasAlias[$serviceId])) {
continue;
}
$text[] = $description;
}
$previousId = $serviceId.' $';
}
$serviceLine = sprintf('<fg=yellow>%s</>', $serviceId);
if ($builder->hasAlias($serviceId)) { if ($builder->hasAlias($serviceId)) {
$tableRows[] = array(sprintf('<fg=cyan>%s</fg=cyan>', $serviceId)); $hasAlias[$serviceId] = true;
$tableRows[] = array(sprintf(' alias to %s', $builder->getAlias($serviceId))); $serviceLine .= ' <fg=cyan>('.$builder->getAlias($serviceId).')</>';
$hasAlias[(string) $builder->getAlias($serviceId)] = true; } elseif (!$all) {
} else { continue;
$tableRows[$serviceId] = array(sprintf('<fg=cyan>%s</fg=cyan>', $serviceId));
} }
$text[] = $serviceLine;
$io->text($text);
} }
$io->newLine();
$io->table(array(), array_diff_key($tableRows, $hasAlias));
} }
} }

View File

@ -289,26 +289,25 @@ abstract class Descriptor implements DescriptorInterface
/** /**
* Gets class description from a docblock. * Gets class description from a docblock.
*
* @param string $class
*
* @return string
*/ */
protected function getClassDescription($class) public static function getClassDescription(string $class, string &$resolvedClass = null): string
{ {
$resolvedClass = null;
if (!interface_exists(DocBlockFactoryInterface::class)) { if (!interface_exists(DocBlockFactoryInterface::class)) {
return ''; return '';
} }
try { try {
$reflectionProperty = new \ReflectionClass($class); $r = new \ReflectionClass($class);
$resolvedClass = $r->name;
if ($docComment = $reflectionProperty->getDocComment()) { if ($docComment = $r->getDocComment()) {
return DocBlockFactory::createInstance() return DocBlockFactory::createInstance()
->create($docComment) ->create($docComment)
->getSummary(); ->getSummary();
} }
} catch (\ReflectionException $e) { } catch (\ReflectionException | \InvalidArgumentException $e) {
} }
return ''; return '';

View File

@ -1625,8 +1625,10 @@ class FrameworkExtension extends Extension
$pool['adapter'] = '.'.$pool['adapter'].'.inner'; $pool['adapter'] = '.'.$pool['adapter'].'.inner';
} }
$definition = new ChildDefinition($pool['adapter']); $definition = new ChildDefinition($pool['adapter']);
if (!\in_array($name, array('cache.app', 'cache.system'), true)) {
$container->registerAliasForArgument($name, CacheInterface::class); $container->registerAliasForArgument($name, CacheInterface::class);
$container->registerAliasForArgument($name, CacheItemPoolInterface::class); $container->registerAliasForArgument($name, CacheItemPoolInterface::class);
}
if ($pool['tags']) { if ($pool['tags']) {
if ($config['pools'][$pool['tags']]['tags'] ?? false) { if ($config['pools'][$pool['tags']]['tags'] ?? false) {

View File

@ -30,7 +30,7 @@ class DebugAutowiringCommandTest extends WebTestCase
$tester->run(array('command' => 'debug:autowiring')); $tester->run(array('command' => 'debug:autowiring'));
$this->assertContains('Symfony\Component\HttpKernel\HttpKernelInterface', $tester->getDisplay()); $this->assertContains('Symfony\Component\HttpKernel\HttpKernelInterface', $tester->getDisplay());
$this->assertContains('alias to http_kernel', $tester->getDisplay()); $this->assertContains('(http_kernel)', $tester->getDisplay());
} }
public function testSearchArgument() public function testSearchArgument()