8000 Adding a new debug:autowiring command · symfony/symfony@30a7548 · GitHub
[go: up one dir, main page]

Skip to content

Commit 30a7548

Browse files
committed
Adding a new debug:autowiring command
1 parent 78e5858 commit 30a7548

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Command;
13+
14+
use Symfony\Bundle\FrameworkBundle\Console\Helper\DescriptorHelper;
15+
use Symfony\Component\Config\ConfigCache;
16+
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Input\InputOption;
18+
use Symfony\Component\Console\Input\InputInterface;
19+
use Symfony\Component\Console\Output\OutputInterface;
20+
use Symfony\Component\Console\Style\SymfonyStyle;
21+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
22+
use Symfony\Component\DependencyInjection\ContainerBuilder;
23+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
24+
use Symfony\Component\Config\FileLocator;
25+
26+
/**
27+
* A console command for autowiring information.
28+
*
29+
* @author Ryan Weaver <ryan@knpuniversity.com>
30+
*
31+
* @internal
32+
*/
33+
class DebugAutowiringCommand extends ContainerDebugCommand
34+
{
35+
protected static $defaultName = 'debug:autowiring';
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
protected function configure()
41+
{
42+
$this
43+
->setDe 10000 finition(array(
44+
new InputArgument('search', InputArgument::OPTIONAL, 'A search filter'),
45+
))
46+
->setDescription('Lists classes/interfaces you can use for autowiring')
47+
->setHelp(<<<'EOF'
48+
The <info>%command.name%</info> command displays all classes and interfaces that
49+
you can use as type-hints for autowiring:
50+
51+
<info>php %command.full_name%</info>
52+
53+
You can also pass a search term to filter the list:
54+
55+
<info>php %command.full_name% log</info>
56+
57+
EOF
58+
)
59+
;
60+
}
61+
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
protected function execute(InputInterface $input, OutputInterface $output)
67+
{
68+
$io = new SymfonyStyle($input, $output);
69+
$errorIo = $io->getErrorStyle();
70+
71+
$builder = $this->getContainerBuilder();
72+
$serviceIds = $builder->getServiceIds();
73+
$serviceIds = array_filter($serviceIds, array($this, 'filterToServiceTypes'));
74+
75+
if ($search = $input->getArgument('search')) {
76+
$serviceIds = array_filter($serviceIds, function($serviceId) use ($search) {
77+
return stripos($serviceId, $search) !== false;
78+
});
79+
80+
if (empty($serviceIds)) {
81+
$errorIo->error(sprintf('No autowirable classes or interfaces found matching "%s"', $search));
82+
83+
return 1;
84+
}
85+
}
86+
87+
asort($serviceIds);
88+
89+
$io->title('Autowirable Services');
90+
$io->text('The following classes & interfaces can be used as type-hints when autowiring:');
91+
if ($search) {
92+
$io->text(sprintf('(only showing classes/interfaces matching <comment>%s</comment>)', $search));
93+
}
94+
$io->newLine();
95+
$tableRows = array();
96+
foreach ($serviceIds as $serviceId) {
97+
$tableRows[] = array(sprintf('<fg=cyan>%s</fg=cyan>', $serviceId));
98+
if ($builder->hasAlias($serviceId)) {
99+
$tableRows[] = array(sprintf(' alias to %s', $builder->getAlias($serviceId)));
100+
}
101+
}
102+
103+
$io->table(array(), $tableRows);
104+
}
105+
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
<tag name="console.command" command="debug:container" />
5656
</service>
5757

58+
<service id="Symfony\Bundle\FrameworkBundle\Command\DebugAutowiringCommand">
59+
<tag name="console.command" command="debug:autowiring" />
60+
</service>
61+
5862
<service id="Symfony\Bundle\FrameworkBundle\Command\EventDispatcherDebugCommand">
5963
<argument type="service" id="event_dispatcher" />
6064
<tag name="console.command" command="debug:event-dispatcher" />
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
13+
14+
use Symfony\Bundle\FrameworkBundle\Console\Application;
15+
use Symfony\Component\Console\Tester\ApplicationTester;
16+
17+
/**
18+
* @group functional
19+
*/
20+
class DebugAutowiringCommandTest extends WebTestCase
21+
{
22+
public function testBasicFunctionality()
23+
{
24+
static::bootKernel(array('test_case' => 'ContainerDebug', 'root_config' => 'config.yml'));
25+
26+
$application = new Application(static::$kernel);
27+
$application->setAutoExit(false);
28+
29+
$tester = new ApplicationTester($application);
30+
$tester->run(array('command' => 'debug:autowiring'));
31+
32+
$this->assertContains('Symfony\Component\HttpKernel\HttpKernelInterface', $tester->getDisplay());
33+
$this->assertContains('alias to http_kernel', $tester->getDisplay());
34+
}
35+
36+
public function testSearchArgument()
37+
{
38+
static::bootKernel(array('test_case' => 'ContainerDebug', 'root_config' => 'config.yml'));
39+
40+
$application = new Application(static::$kernel);
41+
$application->setAutoExit(false);
42+
43+
$tester = new ApplicationTester($application);
44+
$tester->run(array('command' => 'debug:autowiring', 'search' => 'kern'));
45+
46+
$this->assertContains('Symfony\Component\HttpKernel\HttpKernelInterface', $tester->getDisplay());
47+
$this->assertNotContains('Symfony\Component\Routing\RouterInterface', $tester->getDisplay());
48+
}
49+
50+
public function testSearchNoResults()
51+
{
52+
static::bootKernel(array('test_case' => 'ContainerDebug', 'root_config' => 'config.yml'));
53+
54+
$application = new Application(static::$kernel);
55+
$application->setAutoExit(false);
56+
57+
$tester = new ApplicationTester($application);
58+
$tester->run(array('command' => 'debug:autowiring', 'search' => 'foo_fake'), array('capture_stderr_separately' => true));
59+
60+
$this->assertContains('No autowirable classes or interfaces found matching "foo_fake"', $tester->getErrorOutput());
61+
$this->assertEquals(1, $tester->getStatusCode());
62+
}
63+
}

0 commit comments

Comments
 (0)
0