From bb97a2a2399341f9e47884287f754cc49d991934 Mon Sep 17 00:00:00 2001 From: HypeMC Date: Fri, 9 May 2025 13:44:27 +0200 Subject: [PATCH] [Console] Add support for `SignalableCommandInterface` with invokable commands --- src/Symfony/Component/Console/Application.php | 4 +- src/Symfony/Component/Console/CHANGELOG.md | 1 + .../Component/Console/Command/Command.php | 12 ++- .../Console/Command/InvokableCommand.php | 14 +++- .../Console/Command/TraceableCommand.php | 8 +- .../Console/Tests/ApplicationTest.php | 74 ++++++++++++++++++- .../AddConsoleCommandPassTest.php | 40 ++++++++++ .../Tests/Fixtures/application_signalable.php | 3 +- .../Tests/phpt/alarm/command_exit.phpt | 3 +- .../Tests/phpt/signal/command_exit.phpt | 3 +- 10 files changed, 141 insertions(+), 21 deletions(-) diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 78d885d2597a9..b4539fa1eeb50 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -17,7 +17,6 @@ use Symfony\Component\Console\Command\HelpCommand; use Symfony\Component\Console\Command\LazyCommand; use Symfony\Component\Console\Command\ListCommand; -use Symfony\Component\Console\Command\SignalableCommandInterface; use Symfony\Component\Console\CommandLoader\CommandLoaderInterface; use Symfony\Component\Console\Completion\CompletionInput; use Symfony\Component\Console\Completion\CompletionSuggestions; @@ -1005,8 +1004,7 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI } } - $commandSignals = $command instanceof SignalableCommandInterface ? $command->getSubscribedSignals() : []; - if ($commandSignals || $this->dispatcher && $this->signalsToDispatchEvent) { + if (($commandSignals = $command->getSubscribedSignals()) || $this->dispatcher && $this->signalsToDispatchEvent) { $signalRegistry = $this->getSignalRegistry(); if (Terminal::hasSttyAvailable()) { diff --git a/src/Symfony/Component/Console/CHANGELOG.md b/src/Symfony/Component/Console/CHANGELOG.md index b84099a1d0e10..9f3ae3d7d2326 100644 --- a/src/Symfony/Component/Console/CHANGELOG.md +++ b/src/Symfony/Component/Console/CHANGELOG.md @@ -14,6 +14,7 @@ CHANGELOG * Add support for `LockableTrait` in invokable commands * Deprecate returning a non-integer value from a `\Closure` function set via `Command::setCode()` * Mark `#[AsCommand]` attribute as `@final` + * Add support for `SignalableCommandInterface` with invokable commands 7.2 --- diff --git a/src/Symfony/Component/Console/Command/Command.php b/src/Symfony/Component/Console/Command/Command.php index c93340a77ad95..f6cd8499791f1 100644 --- a/src/Symfony/Component/Console/Command/Command.php +++ b/src/Symfony/Component/Console/Command/Command.php @@ -32,7 +32,7 @@ * * @author Fabien Potencier */ -class Command +class Command implements SignalableCommandInterface { // see https://tldp.org/LDP/abs/html/exitcodes.html public const SUCCESS = 0; @@ -674,6 +674,16 @@ public function getHelper(string $name): HelperInterface return $this->helperSet->get($name); } + public function getSubscribedSignals(): array + { + return $this->code?->getSubscribedSignals() ?? []; + } + + public function handleSignal(int $signal, int|false $previousExitCode = 0): int|false + { + return $this->code?->handleSignal($signal, $previousExitCode) ?? false; + } + /** * Validates a command name. * diff --git a/src/Symfony/Component/Console/Command/InvokableCommand.php b/src/Symfony/Component/Console/Command/InvokableCommand.php index 329d8b253cfb8..72ff407c81fdf 100644 --- a/src/Symfony/Component/Console/Command/InvokableCommand.php +++ b/src/Symfony/Component/Console/Command/InvokableCommand.php @@ -28,9 +28,10 @@ * * @internal */ -class InvokableCommand +class InvokableCommand implements SignalableCommandInterface { private readonly \Closure $code; + private readonly ?SignalableCommandInterface $signalableCommand; private readonly \ReflectionFunction $reflection; private bool $triggerDeprecations = false; @@ -39,6 +40,7 @@ public function __construct( callable $code, ) { $this->code = $this->getClosure($code); + $this->signalableCommand = $code instanceof SignalableCommandInterface ? $code : null; $this->reflection = new \ReflectionFunction($this->code); } @@ -142,4 +144,14 @@ private function getParameters(InputInterface $input, OutputInterface $output): return $parameters ?: [$input, $output]; } + + public function getSubscribedSignals(): array + { + return $this->signalableCommand?->getSubscribedSignals() ?? []; + } + + public function handleSignal(int $signal, int|false $previousExitCode = 0): int|false + { + return $this->signalableCommand?->handleSignal($signal, $previousExitCode) ?? false; + } } diff --git a/src/Symfony/Component/Console/Command/TraceableCommand.php b/src/Symfony/Component/Console/Command/TraceableCommand.php index 659798e651c46..315f385de9aa2 100644 --- a/src/Symfony/Component/Console/Command/TraceableCommand.php +++ b/src/Symfony/Component/Console/Command/TraceableCommand.php @@ -27,7 +27,7 @@ * * @author Jules Pietri */ -final class TraceableCommand extends Command implements SignalableCommandInterface +final class TraceableCommand extends Command { public readonly Command $command; public int $exitCode; @@ -89,15 +89,11 @@ public function __call(string $name, array $arguments): mixed public function getSubscribedSignals(): array { - return $this->command instanceof SignalableCommandInterface ? $this->command->getSubscribedSignals() : []; + return $this->command->getSubscribedSignals(); } public function handleSignal(int $signal, int|false $previousExitCode = 0): int|false { - if (!$this->command instanceof SignalableCommandInterface) { - return false; - } - $event = $this->stopwatch->start($this->getName().'.handle_signal'); $exit = $this->command->handleSignal($signal, $previousExitCode); diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index c5c796517c17a..268f8ba501a9e 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -2255,6 +2255,41 @@ public function testSignalableRestoresStty() $this->assertSame($previousSttyMode, $sttyMode); } + /** + * @requires extension pcntl + */ + public function testSignalableInvokableCommand() + { + $command = new Command(); + $command->setName('signal-invokable'); + $command->setCode($invokable = new class implements SignalableCommandInterface { + use SignalableInvokableCommandTrait; + }); + + $application = $this->createSignalableApplication($command, null); + $application->setSignalsToDispatchEvent(\SIGUSR1); + + $this->assertSame(1, $application->run(new ArrayInput(['signal-invokable']))); + $this->assertTrue($invokable->signaled); + } + + /** + * @requires extension pcntl + */ + public function testSignalableInvokableCommandThatExtendsBaseCommand() + { + $command = new class extends Command implements SignalableCommandInterface { + use SignalableInvokableCommandTrait; + }; + $command->setName('signal-invokable'); + + $application = $this->createSignalableApplication($command, null); + $application->setSignalsToDispatchEvent(\SIGUSR1); + + $this->assertSame(1, $application->run(new ArrayInput(['signal-invokable']))); + $this->assertTrue($command->signaled); + } + /** * @requires extension pcntl */ @@ -2514,7 +2549,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } #[AsCommand(name: 'signal')] -class SignableCommand extends BaseSignableCommand implements SignalableCommandInterface +class SignableCommand extends BaseSignableCommand { public function getSubscribedSignals(): array { @@ -2531,7 +2566,7 @@ public function handleSignal(int $signal, int|false $previousExitCode = 0): int| } #[AsCommand(name: 'signal')] -class TerminatableCommand extends BaseSignableCommand implements SignalableCommandInterface +class TerminatableCommand extends BaseSignableCommand { public function getSubscribedSignals(): array { @@ -2548,7 +2583,7 @@ public function handleSignal(int $signal, int|false $previousExitCode = 0): int| } #[AsCommand(name: 'signal')] -class TerminatableWithEventCommand extends Command implements SignalableCommandInterface, EventSubscriberInterface +class TerminatableWithEventCommand extends Command implements EventSubscriberInterface { private bool $shouldContinue = true; private OutputInterface $output; @@ -2615,8 +2650,39 @@ public static function getSubscribedEvents(): array } } +trait SignalableInvokableCommandTrait +{ + public bool $signaled = false; + + public function __invoke(): int + { + posix_kill(posix_getpid(), \SIGUSR1); + + for ($i = 0; $i < 1000; ++$i) { + usleep(100); + if ($this->signaled) { + return 1; + } + } + + return 0; + } + + public function getSubscribedSignals(): array + { + return SignalRegistry::isSupported() ? [\SIGUSR1] : []; + } + + public function handleSignal(int $signal, int|false $previousExitCode = 0): int|false + { + $this->signaled = true; + + return false; + } +} + #[AsCommand(name: 'alarm')] -class AlarmableCommand extends BaseSignableCommand implements SignalableCommandInterface +class AlarmableCommand extends BaseSignableCommand { public function __construct(private int $alarmInterval) { diff --git a/src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php b/src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php index 8a0c1e6b2bbf5..9ac660100ea0d 100644 --- a/src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php +++ b/src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php @@ -15,6 +15,7 @@ use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\LazyCommand; +use Symfony\Component\Console\Command\SignalableCommandInterface; use Symfony\Component\Console\CommandLoader\ContainerCommandLoader; use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass; use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; @@ -325,6 +326,27 @@ public function testProcessInvokableCommand() self::assertSame('The command description', $command->getDescription()); self::assertSame('The %command.name% command help content.', $command->getHelp()); } + + public function testProcessInvokableSignalableCommand() + { + $container = new ContainerBuilder(); + $container->addCompilerPass(new AddConsoleCommandPass(), PassConfig::TYPE_BEFORE_REMOVING); + + $definition = new Definition(InvokableSignalableCommand::class); + $definition->addTag('console.command', [ + 'command' => 'invokable-signalable', + 'description' => 'The command description', + 'help' => 'The %command.name% command help content.', + ]); + $container->setDefinition('invokable_signalable_command', $definition); + + $container->compile(); + $command = $container->get('console.command_loader')->get('invokable-signalable'); + + self::assertTrue($container->has('invokable_signalable_command.command')); + self::assertSame('The command description', $command->getDescription()); + self::assertSame('The %command.name% command help content.', $command->getHelp()); + } } class MyCommand extends Command @@ -361,3 +383,21 @@ public function __invoke(): void { } } + +#[AsCommand(name: 'invokable-signalable', description: 'Just testing', help: 'The %command.name% help content.')] +class InvokableSignalableCommand implements SignalableCommandInterface +{ + public function __invoke(): void + { + } + + public function getSubscribedSignals(): array + { + return []; + } + + public function handleSignal(int $signal, false|int $previousExitCode = 0): int|false + { + return false; + } +} diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_signalable.php b/src/Symfony/Component/Console/Tests/Fixtures/application_signalable.php index c737ba1bf79c7..cc1bae6acdf7f 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_signalable.php +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_signalable.php @@ -1,6 +1,5 @@