-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[WIP][FrameworkBundle] Add debug autoconfigure command #28730
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
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f9fe63e
Add debug autoconfigure command
aaa2000 c0525ca
Fix coding standard
aaa2000 30d9c15
Fixes
aaa2000 9c00441
Fix coding standard
aaa2000 058c306
Remove unnecessary config
aaa2000 00fb8ae
Fix windows tests
aaa2000 b2941a0
Fix display tag attributes
aaa2000 e1cee7e
Modify expected output of tests
aaa2000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
src/Symfony/Bundle/FrameworkBundle/Command/DebugAutoconfigurationCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
<?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 | ||
*/ | ||
class DebugAutoconfigurationCommand extends Command | ||
{ | ||
protected static $defaultName = 'debug:autoconfiguration'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setDefinition(array( | ||
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 (empty($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 = array(); | ||
|
||
foreach ($autoconfiguredInstanceofItem->getTags() as $tag => $tagAttributes) { | ||
$tableRows[] = array('Tag', $tag); | ||
if ($tagAttributes !== array(array())) { | ||
$tableRows[] = array('Tag attribute', $this->dumpTagAttribute($tagAttributes)); | ||
} | ||
} | ||
|
||
if ($autoconfiguredInstanceofItem->getMethodCalls()) { | ||
$tableRows[] = array('Method call', $this->dumpMethodCall($autoconfiguredInstanceofItem)); | ||
} | ||
|
||
if ($autoconfiguredInstanceofItem->getBindings()) { | ||
$tableRows[] = array('Bindings', $this->dumpBindings($autoconfiguredInstanceofItem)); | ||
} | ||
|
||
$io->writeln(sprintf("Autoconfiguration for \"%s\"\n==============================================", $key)); | ||
$io->newLine(); | ||
$io->table(array('Option', 'Value'), $tableRows); | ||
} | ||
} | ||
|
||
private function dumpMethodCall(ChildDefinition $autoconfiguredInstanceofItem) | ||
{ | ||
$tagContainerBuilder = new ContainerBuilder(); | ||
foreach ($tagContainerBuilder->getServiceIds() as $serviceId) { | ||
$tagContainerBuilder->removeDefinition($serviceId); | ||
$tagContainerBuilder->removeAlias($serviceId); | ||
} | ||
$tagContainerBuilder->addDefinitions(array($autoconfiguredInstanceofItem)); | ||
|
||
$dumper = new YamlDumper($tagContainerBuilder); | ||
preg_match('/calls\:\n((?: +- .+\n)+)/', $dumper->dump(), $matches); | ||
|
||
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); | ||
|
||
return $cliDumper->dump($cloner->cloneVar(current($tagAttribute)), true); | ||
} | ||
|
||
/** | ||
* @return ContainerBuilder | ||
*/ | ||
private function getContainerBuilder() | ||
{ | ||
$kernel = $this->getApplication()->getKernel(); | ||
$buildContainer = \Closure::bind(function () { return $this->buildContainer(); }, $kernel, \get_class($kernel)); | ||
$container = $buildContainer(); | ||
$container->getCompilerPassConfig()->setRemovingPasses(array()); | ||
$container->compile(); | ||
|
||
return $container; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...undle/Tests/Functional/Bundle/DebugAutoconfigurationBundle/Autoconfiguration/Bindings.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...le/Tests/Functional/Bundle/DebugAutoconfigurationBundle/Autoconfiguration/MethodCalls.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...Tests/Functional/Bundle/DebugAutoconfigurationBundle/Autoconfiguration/TagsAttributes.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
9 changes: 9 additions & 0 deletions
9
...dle/Tests/Functional/Bundle/DebugAutoconfigurationBundle/DebugAutoconfigurationBundle.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\DebugAutoconfigurationBundle; | ||
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. Maybe, I can use the TestBundle instead of create a new bundle ? |
||
|
||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
class DebugAutoconfigurationBundle extends Bundle | ||
{ | ||
} |
31 changes: 31 additions & 0 deletions
31
...ndle/DebugAutoconfigurationBundle/DependencyInjection/DebugAutoconfigurationExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', array(new Reference('logger'))) | ||
->addMethodCall('setMethodTwo', array(array('paramOne', 'paramOne'))); | ||
|
||
$container->registerForAutoconfiguration(Bindings::class) | ||
->setBindings(array( | ||
'$paramOne' => new Reference('logger'), | ||
'$paramTwo' => 'binding test', | ||
)); | ||
|
||
$container->registerForAutoconfiguration(TagsAttributes::class) | ||
->addTag('debugautoconfiguration.tag1', array('method' => 'debug')) | ||
->addTag('debugautoconfiguration.tag2', array('test')) | ||
; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
maybe we can get rid of the
$tableRows
array by usingaddRow()
andrender()
, as this is formaster
. IMO its more readable afterwardsdocs: https://symfony.com/doc/current/components/console/helpers/table.html#modifying-rendered-tables
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'm not sure it's more readable. To have the same style that the others commands, it is necessary to configure the table.