8000 Allow to register commands privately · symfony/symfony@147eb79 · GitHub
[go: up one dir, main page]

Skip to content

Commit 147eb79

Browse files
committed
Allow to register commands privately
1 parent bb2727a commit 147eb79

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddConsoleCommandPass.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,11 @@ class AddConsoleCommandPass implements CompilerPassInterface
2424
public function process(ContainerBuilder $container)
2525
{
2626
$commandServices = $container->findTaggedServiceIds('console.command');
27+
$serviceIds = array();
2728

2829
foreach ($commandServices as $id => $tags) {
2930
$definition = $container->getDefinition($id);
3031

31-
if (!$definition->isPublic()) {
32-
throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be public.', $id));
33-
}
34-
3532
if ($definition->isAbstract()) {
3633
throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must not be abstract.', $id));
3734
}
@@ -40,9 +37,10 @@ public function process(ContainerBuilder $container)
4037
if (!is_subclass_of($class, 'Symfony\\Component\\Console\\Command\\Command')) {
4138
throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be a subclass of "Symfony\\Component\\Console\\Command\\Command".', $id));
4239
}
43-
$container->setAlias('console.command.'.strtolower(str_replace('\\', '_', $class)), $id);
40+
$container->setAlias($serviceId = 'console.command.'.strtolower(str_replace('\\', '_', $class)), $id);
41+
$serviceIds[] = $serviceId;
4442
}
4543

46-
$container->setParameter('console.command.ids', array_keys($commandServices));
44+
$container->setParameter('console.command.ids', $serviceIds);
4745
}
4846
}

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/AddConsoleCommandPassTest.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,42 @@
1919

2020
class AddConsoleCommandPassTest extends \PHPUnit_Framework_TestCase
2121
{
22-
public function testProcess()
22+
/**
23+
* @dataProvider visibilityProvider
24+
*/
25+
public function testProcess($public)
2326
{
2427
$container = new ContainerBuilder();
2528
$container->addCompilerPass(new AddConsoleCommandPass());
2629
$container->setParameter('my-command.class', 'Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\MyCommand');
2730

2831
$definition = new Definition('%my-command.class%');
32+
$definition->setPublic($public);
2933
$definition->addTag('console.command');
3034
$container->setDefinition('my-command', $definition);
3135

3236
$container->compile();
3337

3438
$alias = 'console.command.symfony_bundle_frameworkbundle_tests_dependencyinjection_compiler_mycommand';
35-
$this->assertTrue($container->hasAlias($alias));
36-
$this->assertSame('my-command', (string) $container->getAlias($alias));
39+
if ($container->hasAlias($alias)) {
40+
$this->assertSame('my-command', (string) $container->getAlias($alias));
41+
} else {
42+
// The alias is replaced by a Definition by the ReplaceAliasByActualDefinitionPass
43+
// in case the original service is private
44+
$this->assertFalse($container->hasDefinition('my-command'));
45+
$this->assertTrue($container->hasDefinition($alias));
46+
}
3747

3848
$this->assertTrue($container->hasParameter('console.command.ids'));
39-
$this->assertSame(array('my-command'), $container->getParameter('console.command.ids'));
49+
$this->assertSame(array('console.command.symfony_bundle_frameworkbundle_tests_dependencyinjection_compiler_mycommand'), $container->getParameter('console.command.ids'));
4050
}
4151

42-
/**
43-
* @expectedException \InvalidArgumentException
44-
* @expectedExceptionMessage The service "my-command" tagged "console.command" must be public.
45-
*/
46-
public function testProcessThrowAnExceptionIfTheServiceIsNotPublic()
52+
public function visibilityProvider()
4753
{
48-
$container = new ContainerBuilder();
49-
$container->addCompilerPass(new AddConsoleCommandPass());
50-
51-
$definition = new Definition('Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\MyCommand');
52-
$definition->addTag('console.command');
53-
$definition->setPublic(false);
54-
$container->setDefinition('my-command', $definition);
55-
56-
$container->compile();
54+
return array(
55+
array(true),
56+
array(false),
57+
);
5758
}
5859

5960
/**

0 commit comments

Comments
 (0)
0