diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php index 1d1bb17f8e50..575d109dd666 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php @@ -261,7 +261,7 @@ private function findServiceIdsContaining(ContainerBuilder $builder, string $nam public function filterToServiceTypes($serviceId) { // 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)(?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+))^(?&V)(?:\\\\(?&V))*+(?: \$(?&V))?$/', $serviceId)) { return false; } @@ -270,13 +270,6 @@ public function filterToServiceTypes($serviceId) return true; } - try { - new \ReflectionClass($serviceId); - - return true; - } catch (\ReflectionException $e) { - // the service id is not a valid class/interface - return false; - } + return class_exists($serviceId) || interface_exists($serviceId, false); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/DebugAutowiringCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/DebugAutowiringCommand.php index 0bb09bb3e673..3dd5663eada0 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/DebugAutowiringCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/DebugAutowiringCommand.php @@ -11,8 +11,10 @@ namespace Symfony\Bundle\FrameworkBundle\Command; +use Symfony\Bundle\FrameworkBundle\Console\Descriptor\Descriptor; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; @@ -35,10 +37,11 @@ protected function configure() $this ->setDefinition(array( 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') ->setHelp(<<<'EOF' -The %command.name% command displays all classes and interfaces that +The %command.name% command displays the classes and interfaces that you can use as type-hints for autowiring: php %command.full_name% @@ -76,26 +79,39 @@ protected function execute(InputInterface $input, OutputInterface $output) } } - 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:'); if ($search) { $io->text(sprintf('(only showing classes/interfaces matching %s)', $search)); } - $io->newLine(); $tableRows = array(); $hasAlias = array(); + $all = $input->getOption('all'); + $previousId = '-'; 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('%s', $serviceId); if ($builder->hasAlias($serviceId)) { - $tableRows[] = array(sprintf('%s', $serviceId)); - $tableRows[] = array(sprintf(' alias to %s', $builder->getAlias($serviceId))); - $hasAlias[(string) $builder->getAlias($serviceId)] = true; - } else { - $tableRows[$serviceId] = array(sprintf('%s', $serviceId)); + $hasAlias[$serviceId] = true; + $serviceLine .= ' ('.$builder->getAlias($serviceId).')'; + } elseif (!$all) { + continue; } + $text[] = $serviceLine; + $io->text($text); } - - $io->table(array(), array_diff_key($tableRows, $hasAlias)); + $io->newLine(); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/Descriptor.php b/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/Descriptor.php index 5f28fade95ab..3cfca2498334 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/Descriptor.php +++ b/src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/Descriptor.php @@ -289,26 +289,25 @@ protected function sortServiceIds(array $serviceIds) /** * 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)) { return ''; } try { - $reflectionProperty = new \ReflectionClass($class); + $r = new \ReflectionClass($class); + $resolvedClass = $r->name; - if ($docComment = $reflectionProperty->getDocComment()) { + if ($docComment = $r->getDocComment()) { return DocBlockFactory::createInstance() ->create($docComment) ->getSummary(); } - } catch (\ReflectionException $e) { + } catch (\ReflectionException | \InvalidArgumentException $e) { } return ''; diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 8430c0bbcbe5..c4cf0acb7b54 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -1640,8 +1640,10 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con $pool['adapter'] = '.'.$pool['adapter'].'.inner'; } $definition = new ChildDefinition($pool['adapter']); - $container->registerAliasForArgument($name, CacheInterface::class); - $container->registerAliasForArgument($name, CacheItemPoolInterface::class); + if (!\in_array($name, array('cache.app', 'cache.system'), true)) { + $container->registerAliasForArgument($name, CacheInterface::class); + $container->registerAliasForArgument($name, CacheItemPoolInterface::class); + } if ($pool['tags']) { if ($config['pools'][$pool['tags']]['tags'] ?? false) { diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/DebugAutowiringCommandTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/DebugAutowiringCommandTest.php index 6a962e68c789..2076a183c32a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/DebugAutowiringCommandTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/DebugAutowiringCommandTest.php @@ -30,7 +30,7 @@ public function testBasicFunctionality() $tester->run(array('command' => 'debug:autowiring')); $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()