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

Commitf77c403

Browse files
committed
Ensure overriding Command::execute() keep priority over __invoke
1 parent7379bfb commitf77c403

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function __construct(?string $name = null)
134134
$this->setHelp($attribute?->help ??'');
135135
}
136136

137-
if (\is_callable($this)) {
137+
if (\is_callable($this) && (new \ReflectionMethod($this,'execute'))->getDeclaringClass()->name ===self::class) {
138138
$this->code =newInvokableCommand($this,$this(...));
139139
}
140140

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
useSymfony\Component\Console\Exception\InvalidOptionException;
2222
useSymfony\Component\Console\Exception\LogicException;
2323
useSymfony\Component\Console\Input\ArrayInput;
24+
useSymfony\Component\Console\Input\InputInterface;
2425
useSymfony\Component\Console\Output\NullOutput;
26+
useSymfony\Component\Console\Output\OutputInterface;
2527

2628
class InvokableCommandTestextends TestCase
2729
{
@@ -142,6 +144,45 @@ public function testInvalidOptionType()
142144
$command->getDefinition();
143145
}
144146

147+
publicfunctiontestExecuteHasPriorityOverInvokeMethod()
148+
{
149+
$command =newclassextends Command {
150+
publicstring$called;
151+
protectedfunctionexecute(InputInterface$input,OutputInterface$output):int
152+
{
153+
$this->called =__FUNCTION__;
154+
155+
return0;
156+
}
157+
158+
publicfunction__invoke():int
159+
{
160+
$this->called =__FUNCTION__;
161+
162+
return0;
163+
}
164+
};
165+
166+
$command->run(newArrayInput([]),newNullOutput());
167+
$this->assertSame('execute',$command->called);
168+
}
169+
170+
publicfunctiontestCallInvokeMethodWhenExtendingCommandClass()
171+
{
172+
$command =newclassextends Command {
173+
publicstring$called;
174+
publicfunction__invoke():int
175+
{
176+
$this->called =__FUNCTION__;
177+
178+
return0;
179+
}
180+
};
181+
182+
$command->run(newArrayInput([]),newNullOutput());
183+
$this->assertSame('__invoke',$command->called);
184+
}
185+
145186
publicfunctiontestInvalidReturnType()
146187
{
147188
$command =newCommand('foo');

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp