🌐 AI搜索 & 代理 主页
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[Console] Add getter for the original command "code" object
  • Loading branch information
weitzman authored and GromNaN committed Aug 22, 2025
commit b57d27544e6324a440b0a1a7f784fb6321b3a009
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
7.4
---

* Add `Command::getCode()` to get the code set via `setCode()`.
* Allow setting aliases and the hidden flag via the command name passed to the constructor
* Introduce `Symfony\Component\Console\Application::addCommand()` to simplify using invokable commands when the component is used standalone
* Deprecate `Symfony\Component\Console\Application::add()` in favor of `Symfony\Component\Console\Application::addCommand()`
Expand Down
10 changes: 10 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,16 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti
}
}

/**
* Gets the code that is executed by the command.
*
* @return ?callable null if the code has not been set with setCode()
*/
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,
) {
$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 @@ -464,6 +464,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());
}

#[IgnoreDeprecations]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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 @@ -293,6 +294,14 @@ public function __invoke()
$command->run(new ArrayInput([]), new NullOutput());
}

public function testGetCode()
{
$invokableTestCommand = new InvokableTestCommand();
$command = new Command(null, $invokableTestCommand);

$this->assertSame($invokableTestCommand, $command->getCode());
}

#[DataProvider('provideInputArguments')]
public function testInputArguments(array $parameters, array $expected)
{
Expand Down
Loading