Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)(?<V>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+))^(?&V)(?:\\\\(?&V))*+(?: \$(?&V))?$/', $serviceId)) {
return false;
}

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 <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:

<info>php %command.full_name%</info>
Expand Down Expand Up @@ -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 <comment>%s</comment>)', $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('<fg=yellow>%s</>', $serviceId);
if ($builder->hasAlias($serviceId)) {
$tableRows[] = array(sprintf('<fg=cyan>%s</fg=cyan>', $serviceId));
$tableRows[] = array(sprintf(' alias to %s', $builder->getAlias($serviceId)));
$hasAlias[(string) $builder->getAlias($serviceId)] = true;
} else {
$tableRows[$serviceId] = array(sprintf('<fg=cyan>%s</fg=cyan>', $serviceId));
$hasAlias[$serviceId] = true;
$serviceLine .= ' <fg=cyan>('.$builder->getAlias($serviceId).')</>';
} elseif (!$all) {
continue;
}
$text[] = $serviceLine;
$io->text($text);
}

$io->table(array(), array_diff_key($tableRows, $hasAlias));
$io->newLine();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1625,8 +1625,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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down