Skip to content

[Console] Fix AsCommand usage without DI #41564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class Command
*/
protected static $defaultDescription;

/**
* @var bool|null The default command visibility (true if hidden or false)
*/
protected static $defaultHidden;

private $application;
private $name;
private $processTitle;
Expand Down Expand Up @@ -92,6 +97,22 @@ public static function getDefaultDescription(): ?string
return $class === $r->class ? static::$defaultDescription : null;
}

/**
* @return bool The default command hidden or false
*/
public static function getDefaultHidden(): ?bool
{
$class = static::class;

if (\PHP_VERSION_ID >= 80000 && $attribute = (new \ReflectionClass($class))->getAttributes(AsCommand::class)) {
return $attribute[0]->getArguments()['hidden'] ?? false;
}

$r = new \ReflectionProperty($class, 'defaultHidden');

return $class === $r->class ? static::$defaultHidden : false;
}

/**
* @param string|null $name The name of the command; passing null means it must be set in configure()
*
Expand All @@ -109,6 +130,10 @@ public function __construct(string $name = null)
$this->setDescription(static::getDefaultDescription() ?? '');
}

if (true !== $this->hidden) {
$this->setHidden(static::getDefaultHidden() ?? false);
}

$this->configure();
}

Expand Down Expand Up @@ -490,6 +515,16 @@ public function setProcessTitle(string $title)
*/
public function getName()
{
if (false !== strpos($this->name, '|')) {
return current(
array_values(
array_filter(
explode('|', $this->name)
)
)
);
}

return $this->name;
}

Expand Down Expand Up @@ -613,6 +648,13 @@ public function setAliases(iterable $aliases)
*/
public function getAliases()
{
if (false !== strpos($this->name, '|')) {
$aliases = array_values(array_filter(explode('|', $this->name)));
array_shift($aliases);

return $aliases;
}

return $this->aliases;
}

Expand Down
83 changes: 72 additions & 11 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Helper\FormatterHelper;
Expand All @@ -33,7 +32,11 @@ class CommandTest extends TestCase
public static function setUpBeforeClass(): void
{
self::$fixturesPath = __DIR__.'/../Fixtures/';
require_once self::$fixturesPath.'/TestCommand.php';
require_once self::$fixturesPath.'/Commands/TestCommand.php';

if (\PHP_VERSION_ID >= 80000) {
require_once self::$fixturesPath.'/Commands/Php8TestCommand.php';
}
}

public function testConstructor()
Expand Down Expand Up @@ -410,10 +413,73 @@ public function testSetCodeWithStaticAnonymousFunction()
/**
* @requires PHP 8
*/
public function testCommandAttribute()
{
$this->assertSame('|foo|f', Php8Command::getDefaultName());
$this->assertSame('desc', Php8Command::getDefaultDescription());
public function provideCommandsWithAttribute(): \Traversable
{
yield '`name` only' => [
[
'defaultName' => 'foo',
'defaultDescription' => null,
'name' => 'foo',
'description' => '',
'aliases' => [],
],
\Php8Command2::class,
];

yield '`name` and `description` set' => [
[
'defaultName' => 'foo',
'defaultDescription' => 'desc',
'name' => 'foo',
'description' => 'desc',
'aliases' => [],
],
\Php8Command3::class,
];

yield '`name`, `description` and `aliases` set' => [
[
'defaultName' => 'foo|f',
'defaultDescription' => 'desc',
'name' => 'foo',
'description' => 'desc',
'aliases' => ['f'],
],
\Php8Command4::class,
];

yield '`name`, `description`, `aliases` and `hidden` = true set' => [
[
'defaultName' => '|foo|f',
'defaultDescription' => 'desc',
'name' => 'foo',
'description' => 'desc',
'aliases' => ['f'],
],
\Php8Command5::class,
];
}

/**
* @requires PHP 8
*
* @dataProvider provideCommandsWithAttribute
*/
public function testCommandAttribute(array $expectedCommandValues, string $commandPath)
{
/** @var Command $command */
$command = new $commandPath();

$this->assertSame(
$expectedCommandValues,
[
'defaultName' => $command::getDefaultName(),
'defaultDescription' => $command::getDefaultDescription(),
'name' => $command->getName(),
'description' => $command->getDescription(),
'aliases' => $command->getAliases(),
]
);
}
}

Expand All @@ -425,8 +491,3 @@ function createClosure()
$output->writeln($this instanceof Command ? 'bound to the command' : 'not bound to the command');
};
}

#[AsCommand(name: 'foo', description: 'desc', hidden: true, aliases: ['f'])]
class Php8Command extends Command
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;

#[AsCommand(name: 'foo', description: 'desc', hidden: true, aliases: ['f'])]
class Php8Command1 extends Command
{
}

#[AsCommand(name: 'foo')]
class Php8Command2 extends Command
{
}

#[AsCommand(name: 'foo', description: 'desc')]
class Php8Command3 extends Command
{
}

#[AsCommand(name: 'foo', description: 'desc', aliases: ['f'])]
class Php8Command4 extends Command
{
}

#[AsCommand(name: 'foo', description: 'desc', aliases: ['f'], hidden: true)]
class Php8Command5 extends Command
{
}