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 b166ba0

Browse filesBrowse files
[Console] Add ConsoleCommand attribute for declaring commands on PHP 8
1 parent 22c2f1a commit b166ba0
Copy full SHA for b166ba0

File tree

5 files changed

+64
-1
lines changed
Filter options

5 files changed

+64
-1
lines changed
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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\Attribute;
13+
14+
#[\Attribute(\Attribute::TARGET_CLASS)]
15+
class ConsoleCommand
16+
{
17+
public function __construct(
18+
public ?string $name,
19+
public ?string $description = null,
20+
array $aliases = [],
21+
bool $hidden = false,
22+
) {
23+
if (!$hidden && !$aliases) {
24+
return;
25+
}
26+
27+
$name = explode('|', $name);
28+
$name = array_merge($name, $aliases);
29+
30+
if ($hidden && '' !== $name[0]) {
31+
array_unshift($name, '');
32+
}
33+
34+
$this->name = implode('|', $name);
35+
}
36+
}

‎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
@@ -10,6 +10,7 @@ CHANGELOG
1010
on the `console.command` tag to allow the `list` command to instantiate commands lazily
1111
* Add option `--short` to the `list` command
1212
* Add support for bright colors
13+
* Add `Command` attribute for declaring commands on PHP 8
1314

1415
5.2.0
1516
-----

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

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

1414
use Symfony\Component\Console\Application;
15+
use Symfony\Component\Console\Attribute\ConsoleCommand;
1516
use Symfony\Component\Console\Exception\ExceptionInterface;
1617
use Symfony\Component\Console\Exception\InvalidArgumentException;
1718
use Symfony\Component\Console\Exception\LogicException;
@@ -65,6 +66,11 @@ class Command
6566
public static function getDefaultName()
6667
{
6768
$class = static::class;
69+
70+
if (\PHP_VERSION_ID >= 80000 && $attribute = (new \ReflectionClass($class))->getAttributes(ConsoleCommand::class)) {
71+
return $attribute[0]->newInstance()->name;
72+
}
73+
6874
$r = new \ReflectionProperty($class, 'defaultName');
6975

7076
return $class === $r->class ? static::$defaultName : null;
@@ -76,6 +82,11 @@ public static function getDefaultName()
7682
public static function getDefaultDescription(): ?string
7783
{
7884
$class = static::class;
85+
86+
if (\PHP_VERSION_ID >= 80000 && $attribute = (new \ReflectionClass($class))->getAttributes(ConsoleCommand::class)) {
87+
return $attribute[0]->newInstance()->description;
88+
}
89+
7990
$r = new \ReflectionProperty($class, 'defaultDescription');
8091

8192
return $class === $r->class ? static::$defaultDescription : null;

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/Command/CommandTest.php
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Application;
16+
use Symfony\Component\Console\Attribute\ConsoleCommand;
1617
use Symfony\Component\Console\Command\Command;
1718
use Symfony\Component\Console\Exception\InvalidOptionException;
1819
use Symfony\Component\Console\Helper\FormatterHelper;
@@ -404,6 +405,15 @@ public function testSetCodeWithStaticAnonymousFunction()
404405

405406
$this->assertEquals('interact called'.\PHP_EOL.'not bound'.\PHP_EOL, $tester->getDisplay());
406407
}
408+
409+
/**
410+
* @requires PHP 8
411+
*/
412+
public function testConsoleCommandAttribute()
413+
{
414+
$this->assertSame('|foo|f', Php8Command::getDefaultName());
415+
$this->assertSame('desc', Php8Command::getDefaultDescription());
416+
}
407417
}
408418

409419
// In order to get an unbound closure, we should create it outside a class
@@ -414,3 +424,8 @@ function createClosure()
414424
$output->writeln($this instanceof Command ? 'bound to the command' : 'not bound to the command');
415425
};
416426
}
427+
428+
#[ConsoleCommand(name: 'foo', description: 'desc', hidden: true, aliases: ['f'])]
429+
class Php8Command extends Command
430+
{
431+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/EventDispatcher/CHANGELOG.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CHANGELOG
44
5.3
55
---
66

7-
* Add `EventListener` attribute for declaring listeners on PHP 8.
7+
* Add `EventListener` attribute for declaring listeners on PHP 8
88

99
5.1.0
1010
-----

0 commit comments

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