Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Console] Make attributes accessible on invokable commands #61078

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

Open
wants to merge 9 commits into
base: 7.4
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions src/Symfony/Component/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,14 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
}
}

/**
* Get the code that is executed by the command.
*/
public function getCode(): ?callable
{
return $this->code?->getCode();
}

/**
* Sets the code to execute when running this command.
*
Expand Down
15 changes: 11 additions & 4 deletions src/Symfony/Component/Console/Command/InvokableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,28 @@
*/
class InvokableCommand implements SignalableCommandInterface
{
private readonly \Closure $code;
private readonly \Closure $closure;
private readonly ?SignalableCommandInterface $signalableCommand;
private readonly \ReflectionFunction $reflection;
private bool $triggerDeprecations = false;
private $code;

public function __construct(
private readonly Command $command,
callable $code,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not rename the argument here. Naming it code is clearer about what the callable is about.

Copy link
Contributor Author

@weitzman weitzman Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed some tweaks variable names. This one is now unchanged. I'm open to alternatives.

) {
$this->code = $this->getClosure($code);
$this->code = $code;
$this->closure = $this->getClosure($code);
$this->signalableCommand = $code instanceof SignalableCommandInterface ? $code : null;
$this->reflection = new \ReflectionFunction($this->code);
$this->reflection = new \ReflectionFunction($this->closure);
}

/**
* Invokes a callable with parameters generated from the input interface.
*/
public function __invoke(InputInterface $input, OutputInterface $output): int
{
$statusCode = ($this->code)(...$this->getParameters($input, $output));
$statusCode = ($this->closure)(...$this->getParameters($input, $output));

if (!\is_int($statusCode)) {
if ($this->triggerDeprecations) {
Expand Down Expand Up @@ -81,6 +83,11 @@ public function configure(InputDefinition $definition): void
}
}

public function getCode(): callable
{
return $this->code;
}

private function getClosure(callable $code): \Closure
{
if (!$code instanceof \Closure) {
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Console/Tests/Command/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ public function testCommandAttribute()
$this->assertStringContainsString('usage1', $command->getUsages()[0]);
$this->assertTrue($command->isHidden());
$this->assertSame(['f'], $command->getAliases());
// Standard commands don't have code.
$this->assertNull($command->getCode());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tests\Fixtures\InvokableTestCommand;

class InvokableCommandTest extends TestCase
{
Expand Down Expand Up @@ -292,6 +293,15 @@ public function __invoke()
$command->run(new ArrayInput([]), new NullOutput());
}

public function testGetCode()
{
// Create a command from an invokable class.
$command = new Command(null, new InvokableTestCommand());

// Ensure that the invokable class can be retrieved from the Command.
$this->assertInstanceOf(InvokableTestCommand::class, $command->getCode());
}

/**
* @dataProvider provideInputArguments
*/
Expand Down
Loading