Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit84b2f70

Browse files
committed
[Console] Add support for extension ofAsCommand attribute
1 parentf1e169e commit84b2f70

File tree

7 files changed

+47
-6
lines changed

7 files changed

+47
-6
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
* Deprecate methods`Command::getDefaultName()` and`Command::getDefaultDescription()` in favor of the`#[AsCommand]` attribute
1313
* Add support for Markdown format in`Table`
1414
* Add support for`LockableTrait` in invokable commands
15+
* Add support for extension of`AsCommand` attribute
1516

1617
7.2
1718
---

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static function getDefaultName(): ?string
6161
{
6262
trigger_deprecation('symfony/console','7.3','Method "%s()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.',__METHOD__);
6363

64-
if ($attribute = (new \ReflectionClass(static::class))->getAttributes(AsCommand::class)) {
64+
if ($attribute = (new \ReflectionClass(static::class))->getAttributes(AsCommand::class, \ReflectionAttribute::IS_INSTANCEOF)) {
6565
return$attribute[0]->newInstance()->name;
6666
}
6767

@@ -75,7 +75,7 @@ public static function getDefaultDescription(): ?string
7575
{
7676
trigger_deprecation('symfony/console','7.3','Method "%s()" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.',__METHOD__);
7777

78-
if ($attribute = (new \ReflectionClass(static::class))->getAttributes(AsCommand::class)) {
78+
if ($attribute = (new \ReflectionClass(static::class))->getAttributes(AsCommand::class, \ReflectionAttribute::IS_INSTANCEOF)) {
7979
return$attribute[0]->newInstance()->description;
8080
}
8181

@@ -91,7 +91,7 @@ public function __construct(?string $name = null)
9191
{
9292
$this->definition =newInputDefinition();
9393

94-
$attribute = ((new \ReflectionClass(static::class))->getAttributes(AsCommand::class)[0] ??null)?->newInstance();
94+
$attribute = ((new \ReflectionClass(static::class))->getAttributes(AsCommand::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ??null)?->newInstance();
9595

9696
if (null ===$name) {
9797
if (self::class !== (new \ReflectionMethod($this,'getDefaultName'))->class) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private function lock(?string $name = null, bool $blocking = false): bool
5555
if (!$name) {
5656
if ($thisinstanceof Command) {
5757
$name =$this->getName();
58-
}elseif ($attribute = (new \ReflectionClass($this::class))->getAttributes(AsCommand::class)) {
58+
}elseif ($attribute = (new \ReflectionClass($this::class))->getAttributes(AsCommand::class, \ReflectionAttribute::IS_INSTANCEOF)) {
5959
$name =$attribute[0]->newInstance()->name;
6060
}else {
6161
thrownewLogicException(\sprintf('Lock name missing: provide it via "%s()", #[AsCommand] attribute, or by extending Command class.',__METHOD__));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function process(ContainerBuilder $container): void
6060
$definition->addTag('container.no_preload');
6161

6262
/** @var AsCommand|null $attribute */
63-
$attribute = ($r->getAttributes(AsCommand::class)[0] ??null)?->newInstance();
63+
$attribute = ($r->getAttributes(AsCommand::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ??null)?->newInstance();
6464

6565
if (Command::class !== (new \ReflectionMethod($class,'getDefaultName'))->class) {
6666
trigger_deprecation('symfony/console','7.3','Overriding "Command::getDefaultName()" in "%s" is deprecated and will be removed in Symfony 8.0, use the #[AsCommand] attribute instead.',$class);

‎src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
useSymfony\Component\Console\SignalRegistry\SignalRegistry;
4646
useSymfony\Component\Console\Terminal;
4747
useSymfony\Component\Console\Tester\ApplicationTester;
48+
useSymfony\Component\Console\Tests\Fixtures\AsCommandExtended;
4849
useSymfony\Component\Console\Tests\Fixtures\MockableAppliationWithTerminalWidth;
4950
useSymfony\Component\DependencyInjection\ContainerBuilder;
5051
useSymfony\Component\EventDispatcher\EventDispatcher;
@@ -559,6 +560,16 @@ public function testDontRunAlternativeCommandName()
559560
$this->assertStringContainsString('Do you want to run "foo" instead? (yes/no) [no]:',$display);
560561
}
561562

563+
publicfunctiontestExtendedAsCommandAtribute()
564+
{
565+
$application =newApplication();
566+
$application->add(newWithExtendedAttributeCommand());
567+
$application->setAutoExit(false);
568+
$tester =newApplicationTester($application);
569+
$exitCode =$tester->run(['command' =>'withExtendedAttribute']);
570+
$this->assertSame(Command::FAILURE,$exitCode);
571+
}
572+
562573
publicstaticfunctionprovideInvalidCommandNamesSingle()
563574
{
564575
return [
@@ -2642,3 +2653,11 @@ public static function getSubscribedEvents(): array
26422653
return [ConsoleAlarmEvent::class =>'onAlarm'];
26432654
}
26442655
}
2656+
2657+
#[AsCommandExtended(name:'withExtendedAttribute', addedParam:'addedParam')]
2658+
class WithExtendedAttributeCommandextends Command {
2659+
protectedfunctionexecute(InputInterface$input,OutputInterface$output):int
2660+
{
2661+
return Command::FAILURE;
2662+
}
2663+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespaceSymfony\Component\Console\Tests\Fixtures;
5+
6+
useSymfony\Component\Console\Attribute\AsCommand;
7+
8+
#[\Attribute(\Attribute::TARGET_CLASS)]
9+
finalclass AsCommandExtendedextends AsCommand
10+
{
11+
publicfunction__construct(
12+
publicstring$name,
13+
public ?string$description =null,
14+
array$aliases = [],
15+
bool$hidden =false,
16+
public ?string$help =null,
17+
public ?string$addedParam =null
18+
) {
19+
parent::__construct($this->name,$this->description,$aliases,$hidden,$this->help);
20+
}
21+
}

‎src/Symfony/Component/Scheduler/DependencyInjection/AddScheduleMessengerPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function process(ContainerBuilder $container): void
5757

5858
if ($serviceDefinition->hasTag('console.command')) {
5959
/** @var AsCommand|null $attribute */
60-
$attribute = ($container->getReflectionClass($serviceDefinition->getClass())->getAttributes(AsCommand::class)[0] ??null)?->newInstance();
60+
$attribute = ($container->getReflectionClass($serviceDefinition->getClass())->getAttributes(AsCommand::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ??null)?->newInstance();
6161
$message =newDefinition(RunCommandMessage::class, [$attribute?->name ??$serviceDefinition->getClass()::getDefaultName().(empty($tagAttributes['arguments']) ?'' :"{$tagAttributes['arguments']}")]);
6262
}else {
6363
$message =newDefinition(ServiceCallMessage::class, [$serviceId,$tagAttributes['method'] ??'__invoke', (array) ($tagAttributes['arguments'] ?? [])]);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp