Skip to content

Commit 08efa49

Browse files
committed
Move AddConsoleCommandPass from FrameworkBundle to Console.
1 parent fddd0c5 commit 08efa49

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
3.3.0
5+
-----
6+
7+
* added `AddConsoleCommandPass` (originally in FrameworkBundle)
8+
49
3.2.0
510
------
611

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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\Component\Console\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
17+
/**
18+
* Registers console commands.
19+
*
20+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
21+
*/
22+
class AddConsoleCommandPass implements CompilerPassInterface
23+
{
24+
public function process(ContainerBuilder $container)
25+
{
26+
$commandServices = $container->findTaggedServiceIds('console.command');
27+
$serviceIds = array();
28+
29+
foreach ($commandServices as $id => $tags) {
30+
$definition = $container->getDefinition($id);
31+
32+
if ($definition->isAbstract()) {
33+
throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must not be abstract.', $id));
34+
}
35+
36+
$class = $container->getParameterBag()->resolveValue($definition->getClass());
37+
if (!is_subclass_of($class, 'Symfony\\Component\\Console\\Command\\Command')) {
38+
throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be a subclass of "Symfony\\Component\\Console\\Command\\Command".', $id));
39+
}
40+
$container->setAlias($serviceId = 'console.command.'.strtolower(str_replace('\\', '_', $class)), $id);
41+
$serviceIds[] = $definition->isPublic() ? $id : $serviceId;
42+
}
43+
44+
$container->setParameter('console.command.ids', $serviceIds);
45+
}
46+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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\Component\Console\Tests\DependencyInjection;
13+
14+
use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
15+
use Symfony\Component\Console\Command\Command;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Definition;
18+
use Symfony\Component\HttpKernel\Bundle\Bundle;
19+
20+
class AddConsoleCommandPassTest extends \PHPUnit_Framework_TestCase
21+
{
22+
/**
23+
* @dataProvider visibilityProvider
24+
*/
25+
public function testProcess($public)
26+
{
27+
$container = new ContainerBuilder();
28+
$container->setResourceTracking(false);
29+
$container->addCompilerPass(new AddConsoleCommandPass());
30+
$container->setParameter('my-command.class', 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
31+
32+
$definition = new Definition('%my-command.class%');
33+
$definition->setPublic($public);
34+
$definition->addTag('console.command');
35+
$container->setDefinition('my-command', $definition);
36+
37+
$container->compile();
38+
39+
$id = $public ? 'my-command' : 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
40+
$this->assertTrue($container->hasParameter('console.command.ids'));
41+
$this->assertSame(array($id), $container->getParameter('console.command.ids'));
42+
}
43+
44+
public function visibilityProvider()
45+
{
46+
return array(
47+
array(true),
48+
array(false),
49+
);
50+
}
51+
52+
/**
53+
* @expectedException \InvalidArgumentException
54+
* @expectedExceptionMessage The service "my-command" tagged "console.command" must not be abstract.
55+
*/
56+
public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
57+
{
58+
$container = new ContainerBuilder();
59+
$container->setResourceTracking(false);
60+
$container->addCompilerPass(new AddConsoleCommandPass());
61+
62+
$definition = new Definition('Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
63+
$definition->addTag('console.command');
64+
$definition->setAbstract(true);
65+
$container->setDefinition('my-command', $definition);
66+
67+
$container->compile();
68+
}
69+
70+
/**
71+
* @expectedException \InvalidArgumentException
72+
* @expectedExceptionMessage The service "my-command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command".
73+
*/
74+
public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
75+
{
76+
$container = new ContainerBuilder();
77+
$container->setResourceTracking(false);
78+
$container->addCompilerPass(new AddConsoleCommandPass());
79+
80+
$definition = new Definition('SplObjectStorage');
81+
$definition->addTag('console.command');
82+
$container->setDefinition('my-command', $definition);
83+
84+
$container->compile();
85+
}
86+
}
87+
88+
class MyCommand extends Command
89+
{
90+
}
91+
92+
class ExtensionPresentBundle extends Bundle
93+
{
94+
}

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
"symfony/debug": "~2.8|~3.0"
2222
},
2323
"require-dev": {
24+
"symfony/http-kernel": "~2.8|~3.0",
2425
"symfony/event-dispatcher": "~2.8|~3.0",
26+
"symfony/dependency-injection": "~2.8|~3.0",
2527
"symfony/filesystem": "~2.8|~3.0",
2628
"symfony/process": "~2.8|~3.0",
2729
"psr/log": "~1.0"

0 commit comments

Comments
 (0)