-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Adding a new debug:autowiring command #24583
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,97 @@ | ||
<?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\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
/** | ||
* A console command for autowiring information. | ||
* | ||
* @author Ryan Weaver <ryan@knpuniversity.com> | ||
* | ||
* @internal | ||
*/ | ||
class DebugAutowiringCommand extends ContainerDebugCommand | ||
{ | ||
protected static $defaultName = 'debug:autowiring'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setDefinition(array( | ||
new InputArgument('search', InputArgument::OPTIONAL, 'A search filter'), | ||
)) | ||
->setDescription('Lists classes/interfaces you can use for autowiring') | ||
->setHelp(<<<'EOF' | ||
The <info>%command.name%</info> command displays all classes and interfaces that | ||
you can use as type-hints for autowiring: | ||
|
||
<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(); | ||
|
||
$builder = $this->getContainerBuilder(); | ||
$serviceIds = $builder->getServiceIds(); | ||
$serviceIds = array_filter($serviceIds, array($this, 'filterToServiceTypes')); | ||
|
||
if ($search = $input->getArgument('search')) { | ||
$serviceIds = array_filter($serviceIds, function ($serviceId) use ($search) { | ||
return false !== stripos($serviceId, $search); | ||
}); | ||
|
||
if (empty($serviceIds)) { | ||
$errorIo->error(sprintf('No autowirable classes or interfaces found matching "%s"', $search)); | ||
|
||
return 1; | ||
} | ||
} | ||
|
||
asort($serviceIds); | ||
|
||
$io->title('Autowirable Services'); | ||
$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(); | ||
foreach ($serviceIds as $serviceId) { | ||
$tableRows[] = array(sprintf('<fg=cyan>%s</fg=cyan>', $serviceId)); | ||
if ($builder->hasAlias($serviceId)) { | ||
$tableRows[] = array(sprintf(' alias to %s', $builder->getAlias($serviceId))); | ||
} | ||
} | ||
|
||
$io->table(array(), $tableRows); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?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\Tests\Functional; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Console\Application; | ||
use Symfony\Component\Console\Tester\ApplicationTester; | ||
|
||
/** | ||
* @group functional | ||
*/ | ||
class DebugAutowiringCommandTest extends WebTestCase | ||
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. you should use KernelTestCase only, as you don't care about the web part (you won't use a BrowserKit client). 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. I think it's correct actually: this 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. ah, I missed that this is the special WebTestCase from the FrameworkBundle testsuite, not the one exposed to users of Symfony |
||
{ | ||
public function testBasicFunctionality() | ||
{ | ||
static::bootKernel(array('test_case' => 'ContainerDebug', 'root_config' => 'config.yml')); | ||
|
||
$application = new Application(static::$kernel); | ||
$application->setAutoExit(false); | ||
|
||
$tester = new ApplicationTester($application); | ||
$tester->run(array('command' => 'debug:autowiring')); | ||
|
||
$this->assertContains('Symfony\Component\HttpKernel\HttpKernelInterface', $tester->getDisplay()); | ||
$this->assertContains('alias to http_kernel', $tester->getDisplay()); | ||
} | ||
|
||
public function testSearchArgument() | ||
{ | ||
static::bootKernel(array('test_case' => 'ContainerDebug', 'root_config' => 'config.yml')); | ||
|
||
$application = new Application(static::$kernel); | ||
$application->setAutoExit(false); | ||
|
||
$tester = new ApplicationTester($application); | ||
$tester->run(array('command' => 'debug:autowiring', 'search' => 'kern')); | ||
|
||
$this->assertContains('Symfony\Component\HttpKernel\HttpKernelInterface', $tester->getDisplay()); | ||
$this->assertNotContains('Symfony\Component\Routing\RouterInterface', $tester->getDisplay()); | ||
} | ||
|
||
public function testSearchNoResults() | ||
{ | ||
static::bootKernel(array('test_case' => 'ContainerDebug', 'root_config' => 'config.yml')); | ||
|
||
$application = new Application(static::$kernel); | ||
$application->setAutoExit(false); | ||
|
||
$tester = new ApplicationTester($application); | ||
$tester->run(array('command' => 'debug:autowiring', 'search' => 'foo_fake'), array('capture_stderr_separately' => true)); | ||
|
||
$this->assertContains('No autowirable classes or interfaces found matching "foo_fake"', $tester->getErrorOutput()); | ||
$this->assertEquals(1, $tester->getStatusCode()); | ||
} | ||
} |
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.
this command should have a format option, for people wanting to do some scripting (we generally don't guarantee stability on the text format for usage with |grep`, as this would forbid us to improve DX, but then it requires having other formats)
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 did this on purpose. I really want this command. But I also realize that it's after feature freeze, and things might change with
debug:container
ordebug:services
in the future. I don't want to encourage (right now) that people use this for scripting. Also, they can usedebug:container --types
and pass a format.