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 05c922a

Browse filesBrowse files
[Console] Add protected static $defaultName to set the default name of a Command
1 parent 5d9ae6b commit 05c922a
Copy full SHA for 05c922a

File tree

4 files changed

+16
-9
lines changed
Filter options

4 files changed

+16
-9
lines changed

‎src/Symfony/Component/Console/Command/Command.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Command/Command.php
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
*
2828
* @author Fabien Potencier <fabien@symfony.com>
2929
*/
30-
class Command
30+
class Command implements DefaultNameProviderInterface
3131
{
32+
protected static $defaultName;
33+
3234
private $application;
3335
private $name;
3436
private $processTitle;
@@ -45,6 +47,14 @@ class Command
4547
private $usages = array();
4648
private $helperSet;
4749

50+
public static function getDefaultName()
51+
{
52+
$class = get_called_class();
53+
$r = new \ReflectionProperty($class, 'defaultName');
54+
55+
return $class === $r->class ? static::$defaultName : null;
56+
}
57+
4858
/**
4959
* Constructor.
5060
*

‎src/Symfony/Component/Console/Command/DefaultNameProviderInterface.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Command/DefaultNameProviderInterface.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
interface DefaultNameProviderInterface
2121
{
2222
/**
23-
* @return string The name to use by default for calling the Command
23+
* @return string|null The name to use by default for calling the Command or null when no default name is set
2424
*/
2525
public static function getDefaultName();
2626
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function process(ContainerBuilder $container)
5656

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

59-
if (!isset($tags[0]['command']) && !$r->implementsInterface(DefaultNameProviderInterface::class)) {
59+
if (!isset($tags[0]['command']) && (!$r->implementsInterface(DefaultNameProviderInterface::class) || !$defaultName = $class::getDefaultName())) {
6060
if (isset($serviceIds[$commandId]) || $container->hasAlias($commandId)) {
6161
$commandId = $commandId.'_'.$id;
6262
}
@@ -70,7 +70,7 @@ public function process(ContainerBuilder $container)
7070
}
7171

7272
$serviceIds[$commandId] = false;
73-
$commandName = isset($tags[0]['command']) ? $tags[0]['command'] : $class::getDefaultName();
73+
$commandName = isset($tags[0]['command']) ? $tags[0]['command'] : $defaultName;
7474
unset($tags[0]);
7575
$lazyCommandMap[$commandName] = $id;
7676
$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
+2-5Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,7 @@ class MyCommand extends Command
180180
{
181181
}
182182

183-
class NamedCommand extends Command implements DefaultNameProviderInterface
183+
class NamedCommand extends Command
184184
{
185-
public static function getDefaultName()
186-
{
187-
return 'default';
188-
}
185+
protected static $defaultName = 'default';
189186
}

0 commit comments

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