-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] Add debug:autoconfigure command #31183
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
<?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 Command | ||
{ | ||
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) | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$errorIo = $io->getErrorStyle(); | ||
|
||
$autoconfiguredInstanceofItems = $this->getContainerBuilder()->getAutoconfiguredInstanceof(); | ||
|
||
if ($search = $input->getArgument('search')) { | ||
$autoconfiguredInstanceofItems = array_filter($autoconfiguredInstanceofItems, function ($key) use ($search) { | ||
return false !== stripos(str_replace('\\', '', $key), $search); | ||
}, ARRAY_FILTER_USE_KEY); | ||
|
||
if (!$autoconfiguredInstanceofItems) { | ||
$errorIo->error(sprintf('No autoconfiguration interface/class found matching "%s"', $search)); | ||
|
||
return 1; | ||
} | ||
} | ||
|
||
ksort($autoconfiguredInstanceofItems, SORT_NATURAL); | ||
|
||
$io->title('Autoconfiguration'); | ||
if ($search) { | ||
$io->text(sprintf('(only showing classes/interfaces matching <comment>%s</comment>)', $search)); | ||
} | ||
$io->newLine(); | ||
|
||
/** @var ChildDefinition $autoconfiguredInstanceofItem */ | ||
foreach ($autoconfiguredInstanceofItems as $key => $autoconfiguredInstanceofItem) { | ||
$tableRows = []; | ||
|
||
foreach ($autoconfiguredInstanceofItem->getTags() as $tag => $tagAttributes) { | ||
$tableRows[] = ['Tag', $tag]; | ||
if ($tagAttributes !== [[]]) { | ||
$tableRows[] = ['Tag attribute', $this->dumpTagAttribute($tagAttributes)]; | ||
} | ||
} | ||
|
||
if ($autoconfiguredInstanceofItem->getMethodCalls()) { | ||
$tableRows[] = ['Method call', $this->dumpMethodCall($autoconfiguredInstanceofItem)]; | ||
} | ||
|
||
if ($autoconfiguredInstanceofItem->getBindings()) { | ||
$tableRows[] = ['Bindings', $this->dumpBindings($autoconfiguredInstanceofItem)]; | ||
} | ||
|
||
$io->title(sprintf('Autoconfiguration for "%s"', $key)); | ||
$io->newLine(); | ||
$io->table(['Option', 'Value'], $tableRows); | ||
} | ||
} | ||
|
||
private function dumpMethodCall(ChildDefinition $autoconfiguredInstanceofItem) | ||
{ | ||
$tagContainerBuilder = new ContainerBuilder(); | ||
foreach ($tagContainerBuilder->getServiceIds() as $serviceId) { | ||
$tagContainerBuilder->removeDefinition($serviceId); | ||
$tagContainerBuilder->removeAlias($serviceId); | ||
} | ||
$tagContainerBuilder->addDefinitions([$autoconfiguredInstanceofItem]); | ||
|
||
$dumper = new YamlDumper($tagContainerBuilder); | ||
preg_match('/calls\:\n((?: +- .+\n)+)/', $dumper->dump(), $matches); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no big fan of this approach, i'd opt for passing e.g. |
||
|
||
return preg_replace('/^\s+/m', '', $matches[1]); | ||
} | ||
|
||
private function dumpBindings(ChildDefinition $autoconfiguredInstanceofItem) | ||
{ | ||
$tagContainerBuilder = new ContainerBuilder(); | ||
foreach ($tagContainerBuilder->getServiceIds() as $serviceId) { | ||
$tagContainerBuilder->removeDefinition($serviceId); | ||
$tagContainerBuilder->removeAlias($serviceId); | ||
} | ||
|
||
$dumper = new YamlDumper($tagContainerBuilder); | ||
foreach ($autoconfiguredInstanceofItem->getBindings() as $bindingKey => $bindingValue) { | ||
$tagContainerBuilder->setParameter($bindingKey, $bindingValue->getValues()[0]); | ||
} | ||
|
||
preg_match('/parameters\:\n((?: + .+\n)+)/', $dumper->dump(), $matches); | ||
|
||
return preg_replace('/^\s+/m', '', $matches[1]); | ||
} | ||
|
||
private function dumpTagAttribute(array $tagAttribute) | ||
{ | ||
$cloner = new VarCloner(); | ||
$cliDumper = new CliDumper(null, null, AbstractDumper::DUMP_LIGHT_ARRAY); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #28898 is meant for this :) |
||
|
||
return $cliDumper->dump($cloner->cloneVar(current($tagAttribute)), true); | ||
} | ||
|
||
private function getContainerBuilder(): ContainerBuilder | ||
{ | ||
$kernel = $this->getApplication()->getKernel(); | ||
$buildContainer = \Closure::bind(function () { return $this->buildContainer(); }, $kernel, \get_class($kernel)); | ||
$container = $buildContainer(); | ||
$container->getCompilerPassConfig()->setRemovingPasses([]); | ||
$container->getCompilerPassConfig()->setAfterRemovingPasses([]); | ||
$container->compile(); | ||
|
||
return $container; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -404,7 +404,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']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would not it be a forgotten ? It seems strange to me |
||
; | ||
|
||
if (!interface_exists(MarshallerInterface::class)) { | ||
$container->registerForAutoconfiguration(ResettableInterface::class) | ||
|
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']) | ||
; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isnt $tagContainerBuilder empty already 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's to make sure. And we could refactor it to be save empty and clone it on each call, no ?