Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 56aab09

Browse files
[FrameworkBundle] make debug:autowiring list useful services and their description
1 parent 8c24c35 commit 56aab09

File tree

5 files changed

+41
-31
lines changed

5 files changed

+41
-31
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ private function findServiceIdsContaining(ContainerBuilder $builder, string $nam
261261
public function filterToServiceTypes($serviceId)
262262
{
263263
// filter out things that could not be valid class names
264-
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)) {
264+
if (!preg_match('/(?(DEFINE)(?<V>[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+))^(?&V)(?:\\\\(?&V))*+(?: \$(?&V))?$/', $serviceId)) {
265265
return false;
266266
}
267267

@@ -270,13 +270,6 @@ public function filterToServiceTypes($serviceId)
270270
return true;
271271
}
272272

273-
try {
274-
new \ReflectionClass($serviceId);
275-
276-
return true;
277-
} catch (\ReflectionException $e) {
278-
// the service id is not a valid class/interface
279-
return false;
280-
}
273+
return class_exists($serviceId) || interface_exists($serviceId, false);
281274
}
282275
}

src/Symfony/Bundle/FrameworkBundle/Command/DebugAutowiringCommand.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Command;
1313

14+
use Symfony\Bundle\FrameworkBundle\Console\Descriptor\Descriptor;
1415
use Symfony\Component\Console\Input\InputArgument;
1516
use Symfony\Component\Console\Input\InputInterface;
17+
use Symfony\Component\Console\Input\InputOption;
1618
use Symfony\Component\Console\Output\OutputInterface;
1719
use Symfony\Component\Console\Style\SymfonyStyle;
1820

@@ -35,10 +37,11 @@ protected function configure()
3537
$this
3638
->setDefinition(array(
3739
new InputArgument('search', InputArgument::OPTIONAL, 'A search filter'),
40+
new InputOption('all', null, InputOption::VALUE_NONE, 'Show also services that are not aliased'),
3841
))
3942
->setDescription('Lists classes/interfaces you can use for autowiring')
4043
->setHelp(<<<'EOF'
41-
The <info>%command.name%</info> command displays all classes and interfaces that
44+
The <info>%command.name%</info> command displays the classes and interfaces that
4245
you can use as type-hints for autowiring:
4346
4447
<info>php %command.full_name%</info>
@@ -76,26 +79,39 @@ protected function execute(InputInterface $input, OutputInterface $output)
7679
}
7780
}
7881

79-
asort($serviceIds);
82+
uasort($serviceIds, 'strnatcmp');
8083

81-
$io->title('Autowirable Services');
84+
$io->title('Autowirable Types');
8285
$io->text('The following classes & interfaces can be used as type-hints when autowiring:');
8386
if ($search) {
8487
$io->text(sprintf('(only showing classes/interfaces matching <comment>%s</comment>)', $search));
8588
}
86-
$io->newLine();
8789
$tableRows = array();
8890
$hasAlias = array();
91+
$all = $input->getOption('all');
92+
$previousId = '-';
8993
foreach ($serviceIds as $serviceId) {
94+
$text = array();
95+
if (0 !== strpos($serviceId, $previousId)) {
96+
$text[] = '';
97+
if ('' !== $description = Descriptor::getClassDescription($serviceId, $serviceId)) {
98+
if (isset($hasAlias[$serviceId])) {
99+
continue;
100+
}
101+
$text[] = $description;
102+
}
103+
$previousId = $serviceId.' $';
104+
}
105+
$serviceLine = sprintf('<fg=yellow>%s</>', $serviceId);
90106
if ($builder->hasAlias($serviceId)) {
91-
$tableRows[] = array(sprintf('<fg=cyan>%s</fg=cyan>', $serviceId));
92-
$tableRows[] = array(sprintf(' alias to %s', $builder->getAlias($serviceId)));
93-
$hasAlias[(string) $builder->getAlias($serviceId)] = true;
94-
} else {
95-
$tableRows[$serviceId] = array(sprintf('<fg=cyan>%s</fg=cyan>', $serviceId));
107+
$hasAlias[$serviceId] = true;
108+
$serviceLine .= ' <fg=cyan>('.$builder->getAlias($serviceId).')</>';
109+
} elseif (!$all) {
110+
continue;
96111
}
112+
$text[] = $serviceLine;
113+
$io->text($text);
97114
}
98-
99-
$io->table(array(), array_diff_key($tableRows, $hasAlias));
115+
$io->newLine();
100116
}
101117
}

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/Descriptor.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,26 +289,25 @@ protected function sortServiceIds(array $serviceIds)
289289

290290
/**
291291
* Gets class description from a docblock.
292-
*
293-
* @param string $class
294-
*
295-
* @return string
296292
*/
297-
protected function getClassDescription($class)
293+
public static function getClassDescription(string $class, string &$resolvedClass = null): string
298294
{
295+
$resolvedClass = null;
296+
299297
if (!interface_exists(DocBlockFactoryInterface::class)) {
300298
return '';
301299
}
302300

303301
try {
304-
$reflectionProperty = new \ReflectionClass($class);
302+
$r = new \ReflectionClass($class);
303+
$resolvedClass = $r->name;
305304

306-
if ($docComment = $reflectionProperty->getDocComment()) {
305+
if ($docComment = $r->getDocComment()) {
307306
return DocBlockFactory::createInstance()
308307
->create($docComment)
309308
->getSummary();
310309
}
311-
} catch (\ReflectionException $e) {
310+
} catch (\ReflectionException | \InvalidArgumentException $e) {
312311
}
313312

314313
return '';

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,8 +1625,10 @@ private function registerCacheConfiguration(array $config, ContainerBuilder $con
16251625
$pool['adapter'] = '.'.$pool['adapter'].'.inner';
16261626
}
16271627
$definition = new ChildDefinition($pool['adapter']);
1628-
$container->registerAliasForArgument($name, CacheInterface::class);
1629-
$container->registerAliasForArgument($name, CacheItemPoolInterface::class);
1628+
if (!\in_array($name, array('cache.app', 'cache.system'), true)) {
1629+
$container->registerAliasForArgument($name, CacheInterface::class);
1630+
$container->registerAliasForArgument($name, CacheItemPoolInterface::class);
1631+
}
16301632

16311633
if ($pool['tags']) {
16321634
if ($config['pools'][$pool['tags']]['tags'] ?? false) {

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/DebugAutowiringCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testBasicFunctionality()
3030
$tester->run(array('command' => 'debug:autowiring'));
3131

3232
$this->assertContains('Symfony\Component\HttpKernel\HttpKernelInterface', $tester->getDisplay());
33-
$this->assertContains('alias to http_kernel', $tester->getDisplay());
33+
$this->assertContains('(http_kernel)', $tester->getDisplay());
3434
}
3535

3636
public function testSearchArgument()

0 commit comments

Comments
 (0)