Skip to content

[Console] Add ConsoleCommand attribute for declaring commands on PHP 8 #40234

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

Merged
merged 1 commit into from
Feb 19, 2021
Merged
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
36 changes: 36 additions & 0 deletions src/Symfony/Component/Console/Attribute/ConsoleCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Attribute;

#[\Attribute(\Attribute::TARGET_CLASS)]
class ConsoleCommand
{
public function __construct(
public string $name,
public ?string $description = null,
array $aliases = [],
bool $hidden = false,
) {
if (!$hidden && !$aliases) {
return;
}

$name = explode('|', $name);
$name = array_merge($name, $aliases);

if ($hidden && '' !== $name[0]) {
array_unshift($name, '');
}

$this->name = implode('|', $name);
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
on the `console.command` tag to allow the `list` command to instantiate commands lazily
* Add option `--short` to the `list` command
* Add support for bright colors
* Add `ConsoleCommand` attribute for declaring commands on PHP 8

5.2.0
-----
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Console\Command;

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Attribute\ConsoleCommand;
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\LogicException;
Expand Down Expand Up @@ -65,6 +66,11 @@ class Command
public static function getDefaultName()
{
$class = static::class;

if (\PHP_VERSION_ID >= 80000 && $attribute = (new \ReflectionClass($class))->getAttributes(ConsoleCommand::class)) {
return $attribute[0]->newInstance()->name;
}

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

return $class === $r->class ? static::$defaultName : null;
Expand All @@ -76,6 +82,11 @@ public static function getDefaultName()
public static function getDefaultDescription(): ?string
{
$class = static::class;

if (\PHP_VERSION_ID >= 80000 && $attribute = (new \ReflectionClass($class))->getAttributes(ConsoleCommand::class)) {
return $attribute[0]->newInstance()->description;
}

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

return $class === $r->class ? static::$defaultDescription : null;
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Attribute\ConsoleCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Helper\FormatterHelper;
Expand Down Expand Up @@ -404,6 +405,15 @@ public function testSetCodeWithStaticAnonymousFunction()

$this->assertEquals('interact called'.\PHP_EOL.'not bound'.\PHP_EOL, $tester->getDisplay());
}

/**
* @requires PHP 8
*/
public function testConsoleCommandAttribute()
{
$this->assertSame('|foo|f', Php8Command::getDefaultName());
$this->assertSame('desc', Php8Command::getDefaultDescription());
}
}

// In order to get an unbound closure, we should create it outside a class
Expand All @@ -414,3 +424,8 @@ function createClosure()
$output->writeln($this instanceof Command ? 'bound to the command' : 'not bound to the command');
};
}

#[ConsoleCommand(name: 'foo', description: 'desc', hidden: true, aliases: ['f'])]
class Php8Command extends Command
{
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/EventDispatcher/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CHANGELOG
5.3
---

* Add `EventListener` attribute for declaring listeners on PHP 8.
* Add `EventListener` attribute for declaring listeners on PHP 8

5.1.0
-----
Expand Down