Skip to content

Commit

Permalink
bug #42091 [Console] Run commands when implements SignalableCommandIn…
Browse files Browse the repository at this point in the history
…terface without pcntl and they have'nt signals (PaolaRuby)

This PR was squashed before being merged into the 5.2 branch.

Discussion
----------

[Console] Run commands when implements SignalableCommandInterface without pcntl and they have'nt signals

| Q             | A
| ------------- | ---
| Branch?       | 5.2
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix [#42076](#42076)
| License       | MIT

When a command extends a class wich implements SignalableCommandInterface but the command has empty signals to dispatch, it still can be executed, It can be useful when the extension is not available and the command is necessary.
Also, it can be used like a workaround for support other environments, for example windows servers, they don't have pcntl extension

Commits
-------

ad63d0b [Console] Run commands when implements SignalableCommandInterface without pcntl and they have'nt signals
  • Loading branch information
nicolas-grekas committed Jul 19, 2021
2 parents 5514f15 + ad63d0b commit e737bd2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Console/Application.php
Expand Up @@ -940,7 +940,7 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
}
}

if ($command instanceof SignalableCommandInterface) {
if ($command instanceof SignalableCommandInterface && ($this->signalsToDispatchEvent || $command->getSubscribedSignals())) {
if (!$this->signalRegistry) {
throw new RuntimeException('Unable to subscribe to signal events. Make sure that the `pcntl` extension is installed and that "pcntl_*" functions are not disabled by your php.ini\'s "disable_functions" directive.');
}
Expand Down
15 changes: 14 additions & 1 deletion src/Symfony/Component/Console/Tests/ApplicationTest.php
Expand Up @@ -36,6 +36,7 @@
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\SignalRegistry\SignalRegistry;
use Symfony\Component\Console\Tester\ApplicationTester;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\EventDispatcher;
Expand Down Expand Up @@ -1861,6 +1862,18 @@ public function testSignal()
$this->assertTrue($command->signaled);
$this->assertTrue($dispatcherCalled);
}

public function testSignalableCommandInterfaceWithoutSignals()
{
$command = new SignableCommand();

$dispatcher = new EventDispatcher();
$application = new Application();
$application->setAutoExit(false);
$application->setDispatcher($dispatcher);
$application->add($command);
$this->assertSame(0, $application->run(new ArrayInput(['signal'])));
}
}

class CustomApplication extends Application
Expand Down Expand Up @@ -1928,7 +1941,7 @@ class SignableCommand extends Command implements SignalableCommandInterface

public function getSubscribedSignals(): array
{
return [\SIGALRM];
return SignalRegistry::isSupported() ? [\SIGALRM] : [];
}

public function handleSignal(int $signal): void
Expand Down

0 comments on commit e737bd2

Please sign in to comment.