Skip to content

[WIP] [FrameworkBundle] Add debug:autoconfigure command #35033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ CHANGELOG
* Added a `InMemoryTransport` to Messenger. Use it with a DSN starting with `in-memory://`.
* Added `framework.property_access.throw_exception_on_invalid_property_path` config option.
* Added `cache:pool:list` command to list all available cache pools.
* Added `debug:autoconfiguration` command to display the autoconfiguration of interfaces/classes

4.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Console\Command\Command;
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;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\YamlDumper;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\AbstractDumper;
use Symfony\Component\VarDumper\Dumper\CliDumper;

/**
* A console command for autoconfiguration information.
*
* @internal
*/
final class DebugAutoconfigurationCommand extends ContainerDebugCommand
{
protected static $defaultName = 'debug:autoconfiguration';

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setDefinition([
new InputArgument('search', InputArgument::OPTIONAL, 'A search filter'),
new InputOption('tags', null, InputOption::VALUE_NONE, 'Displays autoconfiguration interfaces/class grouped by tags'),
])
->setDescription('Displays current autoconfiguration for an application')
->setHelp(<<<'EOF'
The <info>%command.name%</info> command displays all services that are autoconfigured:

<info>php %command.full_name%</info>

You can also pass a search term to filter the list:

<info>php %command.full_name% log</info>

EOF
)
;
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$errorIo = $io->getErrorStyle();

$definitions = $this->getContainerBuilder()->getAutoconfiguredInstanceof();
ksort($definitions, SORT_NATURAL);

if ($search = $input->getArgument('search')) {
$definitions = array_filter($definitions, function ($key) use ($search) {
return false !== stripos(str_replace('\\', '', $key), $search);
}, ARRAY_FILTER_USE_KEY);

if (0 === \count($definitions)) {
$errorIo->error(sprintf('No autoconfiguration interface/class found matching "%s"', $search));

return 1;
}

$name = $this->findProperInterfaceName(array_keys($definitions), $input, $io, $search);
/** @var ChildDefinition $definition */
$definition = $definitions[$name];

$io->title(sprintf('Information for Interface/Class "<info>%s</info>"', $name));
$tableHeaders = ['Option', 'Value'];
$tableRows = [];

$tagInformation = [];
foreach ($definition->getTags() as $tagName => $tagData) {
foreach ($tagData as $tagParameters) {
$parameters = array_map(function ($key, $value) {
return sprintf('<info>%s</info>: %s', $key, $value);
}, array_keys($tagParameters), array_values($tagParameters));
$parameters = implode(', ', $parameters);

if ('' === $parameters) {
$tagInformation[] = sprintf('%s', $tagName);
} else {
$tagInformation[] = sprintf('%s (%s)', $tagName, $parameters);
}
}
}
$tableRows[] = ['Tags', implode("\n", $tagInformation)];

$calls = $definition->getMethodCalls();
if (\count($calls) > 0) {
$callInformation = [];
foreach ($calls as $call) {
$callInformation[] = $call[0];
}
$tableRows[] = ['Calls', implode(', ', $callInformation)];
}

$io->table($tableHeaders, $tableRows);
} else {
$io->table(['Interface/Class'], array_map(static function ($interface) {
return [$interface];
}, array_keys($definitions)));
}

$io->newLine();

return 0;
}

private function findProperInterfaceName(array $list, InputInterface $input, SymfonyStyle $io, string $name): string
{
$name = ltrim($name, '\\');

if (\in_array($name, $list, true)) {
return $name;
}

if (1 === \count($list)) {
return $list[0];
}

return $io->choice('Select one of the following interfaces to display its information', $list);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,9 @@ public function load(array $configs, ContainerBuilder $container)
$container->registerForAutoconfiguration(LocaleAwareInterface::class)
->addTag('kernel.locale_aware');
$container->registerForAutoconfiguration(ResetInterface::class)
->addTag('kernel.reset', ['method' => 'reset']);
->addTag('kernel.reset', ['method' => 'reset'])
->addTag('kernel.reset2', ['method' => 'reset2'])
;

if (!interface_exists(MarshallerInterface::class)) {
$container->registerForAutoconfiguration(ResettableInterface::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
<tag name="console.command" command="lint:container" />
</service>

<service id="console.command.debug_autoconfiguration" class="Symfony\Bundle\FrameworkBundle\Command\DebugAutoconfigurationCommand">
<tag name="console.command" command="debug:autoconfiguration" />
</service>

<service id="console.command.debug_autowiring" class="Symfony\Bundle\FrameworkBundle\Command\DebugAutowiringCommand">
<argument>null</argument>
<argument type="service" id="debug.file_link_formatter" on-invalid="null"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\DebugAutoconfigurationBundle\Autoconfiguration;

class Bindings
{
private $paramOne;
private $paramTwo;

public function __construct($paramOne, $paramTwo)
{
$this->paramOne = $paramOne;
$this->paramTwo = $paramTwo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\DebugAutoconfigurationBundle\Autoconfiguration;

class MethodCalls
{
public function setMethodCallOne()
{
}

public function setMethodCallTwo()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\DebugAutoconfigurationBundle\Autoconfiguration;

class TagsAttributes
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\DebugAutoconfigurationBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class DebugAutoconfigurationBundle extends Bundle
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\DebugAutoconfigurationBundle\DependencyInjection;

use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\DebugAutoconfigurationBundle\Autoconfiguration\Bindings;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\DebugAutoconfigurationBundle\Autoconfiguration\MethodCalls;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\DebugAutoconfigurationBundle\Autoconfiguration\TagsAttributes;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Reference;

class DebugAutoconfigurationExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$container->registerForAutoconfiguration(MethodCalls::class)
->addMethodCall('setMethodOne', [new Reference('logger')])
->addMethodCall('setMethodTwo', [['paramOne', 'paramOne']]);

$container->registerForAutoconfiguration(Bindings::class)
->setBindings([
'$paramOne' => new Reference('logger'),
'$paramTwo' => 'binding test',
]);

$container->registerForAutoconfiguration(TagsAttributes::class)
->addTag('debugautoconfiguration.tag1', ['method' => 'debug'])
->addTag('debugautoconfiguration.tag2', ['test'])
;
}
}
Loading