Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bd71187

Browse filesBrowse files
committed
[Console] Allow commands to provide a default name for compile time registration
1 parent aef502b commit bd71187
Copy full SHA for bd71187

File tree

4 files changed

+71
-2
lines changed
Filter options

4 files changed

+71
-2
lines changed

‎src/Symfony/Component/Console/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* added `CommandLoaderInterface`, `FactoryCommandLoader` and PSR-11
88
`ContainerCommandLoader` for commands lazy-loading
99
* added a case-insensitive command name matching fallback
10+
* added `DefaultNameProviderInterface`
1011

1112
3.3.0
1213
-----
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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\Command;
13+
14+
/**
15+
* Enables lazy-loading capabilities for a Command by exposing its default name.
16+
*
17+
* @author Nicolas Grekas <p@tchwork.com>
18+
* @author Robin Chalas <robin.chalas@gmail.com>
19+
*/
20+
interface DefaultNameProviderInterface
21+
{
22+
/**
23+
* @return string The name to which register the implementing Command
24+
*/
25+
public static function getDefaultName();
26+
}

‎src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Console\DependencyInjection;
1313

1414
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Command\DefaultNameProviderInterface;
1516
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
1617
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1718
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
@@ -55,7 +56,7 @@ public function process(ContainerBuilder $container)
5556

5657
$commandId = 'console.command.'.strtolower(str_replace('\\', '_', $class));
5758

58-
if (!isset($tags[0]['command'])) {
59+
if (!isset($tags[0]['command']) && !$r->implementsInterface(DefaultNameProviderInterface::class)) {
5960
if (isset($serviceIds[$commandId]) || $container->hasAlias($commandId)) {
6061
$commandId = $commandId.'_'.$id;
6162
}
@@ -69,7 +70,7 @@ public function process(ContainerBuilder $container)
6970
}
7071

7172
$serviceIds[$commandId] = false;
72-
$commandName = $tags[0]['command'];
73+
$commandName = isset($tags[0]['command']) ? $tags[0]['command'] : $class::getDefaultName();
7374
unset($tags[0]);
7475
$lazyCommandMap[$commandName] = $id;
7576
$lazyCommandRefs[$id] = new TypedReference($id, $class);

‎src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Console\Tests\DependencyInjection;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Command\DefaultNameProviderInterface;
1516
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
1617
use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
1718
use Symfony\Component\Console\Command\Command;
@@ -77,6 +78,38 @@ public function testProcessRegistersLazyCommands()
7778
$this->assertSame(array(array('setName', array('my:command')), array('setAliases', array(array('my:alias')))), $command->getMethodCalls());
7879
}
7980

81+
public function testProcessFallsBackToDefaultName()
82+
{
83+
$container = new ContainerBuilder();
84+
$container
85+
->register('with-default-name', NamedCommand::class)
86+
->setPublic(false)
87+
->addTag('console.command')
88+
;
89+
90+
$pass = new AddConsoleCommandPass();
91+
$pass->process($container);
92+
93+
$commandLoader = $container->getDefinition('console.command_loader');
94+
$commandLocator = $container->getDefinition((string) $commandLoader->getArgument(0));
95+
96+
$this->assertSame(ContainerCommandLoader::class, $commandLoader->getClass());
97+
$this->assertSame(array('default' => 'with-default-name'), $commandLoader->getArgument(1));
98+
$this->assertEquals(array(array('with-default-name' => new ServiceClosureArgument(new TypedReference('with-default-name', NamedCommand::class)))), $commandLocator->getArguments());
99+
$this->assertSame(array('console.command.symfony_component_console_tests_dependencyinjection_namedcommand' => false), $container->getParameter('console.command.ids'));
100+
101+
$container = new ContainerBuilder();
102+
$container
103+
->register('with-default-name', NamedCommand::class)
104+
->setPublic(false)
105+
->addTag('console.command', array('command' => 'new-name'))
106+
;
107+
108+
$pass->process($container);
109+
110+
$this->assertSame(array('new-name' => 'with-default-name'), $container->getDefinition('console.command_loader')->getArgument(1));
111+
}
112+
80113
public function visibilityProvider()
81114
{
82115
return array(
@@ -146,3 +179,11 @@ public function testProcessPrivateServicesWithSameCommand()
146179
class MyCommand extends Command
147180
{
148181
}
182+
183+
class NamedCommand extends Command implements DefaultNameProviderInterface
184+
{
185+
public static function getDefaultName()
186+
{
187+
return 'default';
188+
}
189+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.