Skip to content

Commit b57d275

Browse files
weitzmanGromNaN
authored andcommitted
[Console] Add getter for the original command "code" object
1 parent 883a1b4 commit b57d275

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
7.4
55
---
66

7+
* Add `Command::getCode()` to get the code set via `setCode()`.
78
* Allow setting aliases and the hidden flag via the command name passed to the constructor
89
* Introduce `Symfony\Component\Console\Application::addCommand()` to simplify using invokable commands when the component is used standalone
910
* Deprecate `Symfony\Component\Console\Application::add()` in favor of `Symfony\Component\Console\Application::addCommand()`

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,16 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
356356
}
357357
}
358358

359+
/**
360+
* Gets the code that is executed by the command.
361+
*
362+
* @return ?callable null if the code has not been set with setCode()
363+
*/
364+
public function getCode(): ?callable
365+
{
366+
return $this->code?->getCode();
367+
}
368+
359369
/**
360370
* Sets the code to execute when running this command.
361371
*

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,28 @@
3030
*/
3131
class InvokableCommand implements SignalableCommandInterface
3232
{
33-
private readonly \Closure $code;
33+
private readonly \Closure $closure;
3434
private readonly ?SignalableCommandInterface $signalableCommand;
3535
private readonly \ReflectionFunction $reflection;
3636
private bool $triggerDeprecations = false;
37+
private $code;
3738

3839
public function __construct(
3940
private readonly Command $command,
4041
callable $code,
4142
) {
42-
$this->code = $this->getClosure($code);
43+
$this->code = $code;
44+
$this->closure = $this->getClosure($code);
4345
$this->signalableCommand = $code instanceof SignalableCommandInterface ? $code : null;
44-
$this->reflection = new \ReflectionFunction($this->code);
46+
$this->reflection = new \ReflectionFunction($this->closure);
4547
}
4648

4749
/**
4850
* Invokes a callable with parameters generated from the input interface.
4951
*/
5052
public function __invoke(InputInterface $input, OutputInterface $output): int
5153
{
52-
$statusCode = ($this->code)(...$this->getParameters($input, $output));
54+
$statusCode = ($this->closure)(...$this->getParameters($input, $output));
5355

5456
if (!\is_int($statusCode)) {
5557
if ($this->triggerDeprecations) {
@@ -81,6 +83,11 @@ public function configure(InputDefinition $definition): void
8183
}
8284
}
8385

86+
public function getCode(): callable
87+
{
88+
return $this->code;
89+
}
90+
8491
private function getClosure(callable $code): \Closure
8592
{
8693
if (!$code instanceof \Closure) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@ public function testCommandAttribute()
464464
$this->assertStringContainsString('usage1', $command->getUsages()[0]);
465465
$this->assertTrue($command->isHidden());
466466
$this->assertSame(['f'], $command->getAliases());
467+
// Standard commands don't have code.
468+
$this->assertNull($command->getCode());
467469
}
468470

469471
#[IgnoreDeprecations]

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Symfony\Component\Console\Input\InputInterface;
2828
use Symfony\Component\Console\Output\NullOutput;
2929
use Symfony\Component\Console\Output\OutputInterface;
30+
use Symfony\Component\Console\Tests\Fixtures\InvokableTestCommand;
3031

3132
class InvokableCommandTest extends TestCase
3233
{
@@ -293,6 +294,14 @@ public function __invoke()
293294
$command->run(new ArrayInput([]), new NullOutput());
294295
}
295296

297+
public function testGetCode()
298+
{
299+
$invokableTestCommand = new InvokableTestCommand();
300+
$command = new Command(null, $invokableTestCommand);
301+
302+
$this->assertSame($invokableTestCommand, $command->getCode());
303+
}
304+
296305
#[DataProvider('provideInputArguments')]
297306
public function testInputArguments(array $parameters, array $expected)
298307
{

0 commit comments

Comments
 (0)