-
Notifications
You must be signed in to change notification settings - Fork 11.4k
[9.x] Introducing Signal Traps 🚦 #43933
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
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
7c68c8f
Adds signal traps
nunomaduro 6c8e8d7
Apply fixes from StyleCI
StyleCIBot 4b61c28
Improves code logic
nunomaduro 9066928
Improves return type
nunomaduro ad3f304
Don't run traps outside of console or in unit tests
nunomaduro 9080c41
Updates description
nunomaduro c6b356b
Adds tests
nunomaduro 0b4f53c
Removes non needed annotation
nunomaduro dc2ea4d
Improves test
nunomaduro 91e641d
Avoids missing constant on Windows
nunomaduro e735acd
Improves exit "as default" behaviour
nunomaduro 5e1b7e8
Adjusts docs
nunomaduro 53cf0ff
Only use signals if `posix` and `pcntl` are available
nunomaduro 49d1ab7
Fixes missing parentheses
nunomaduro c46b3e5
Apply fixes from StyleCI
StyleCIBot bc99750
No longers kill the process
nunomaduro ecfa022
Unsets null values
nunomaduro 0c49e64
Adjusts PHP annotations
nunomaduro 61326d9
move to service provider
taylorotwell e1a6eea
move method
taylorotwell e88a73b
Apply fixes from StyleCI
StyleCIBot 79ef572
Update InteractsWithSignals.php
taylorotwell f0bed13
Update Signals.php
taylorotwell 69ea261
Update Signals.php
taylorotwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Illuminate\Console\Concerns; | ||
|
||
use Illuminate\Console\Signals; | ||
use Illuminate\Support\Arr; | ||
|
||
trait InteractsWithSignals | ||
{ | ||
/** | ||
* The signal registrar instance. | ||
* | ||
* @var \Illuminate\Console\Signals|null | ||
*/ | ||
protected $signals; | ||
|
||
/** | ||
* Define a callback to be run when the given signal(s) occurs. | ||
* | ||
* @param iterable<array-key, int>|int $signals | ||
* @param callable(int $signal): void $callback | ||
* @return void | ||
*/ | ||
public function trap($signals, $callback) | ||
{ | ||
Signals::whenAvailable(function () use ($signals, $callback) { | ||
$this->signals ??= new Signals( | ||
$this->getApplication()->getSignalRegistry(), | ||
); | ||
|
||
collect(Arr::wrap($signals)) | ||
->each(fn ($signal) => $this->signals->register($signal, $callback)); | ||
}); | ||
} | ||
|
||
/** | ||
* Untrap signal handlers set within the command's handler. | ||
* | ||
* @return void | ||
* | ||
* @internal | ||
*/ | ||
public function untrap() | ||
{ | ||
if (! is_null($this->signals)) { | ||
$this->signals->unregister(); | ||
|
||
$this->signals = null; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
|
||
namespace Illuminate\Console; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class Signals | ||
{ | ||
/** | ||
* The signal registry instance. | ||
* | ||
* @var \Symfony\Component\Console\SignalRegistry\SignalRegistry | ||
*/ | ||
protected $registry; | ||
|
||
/** | ||
* The signal registry's previous list of handlers. | ||
* | ||
* @param array<int, array<int, callable>>|null | ||
*/ | ||
protected $previousHandlers; | ||
|
||
/** | ||
* The current availability resolver, if any. | ||
* | ||
* @param (callable(): bool)|null | ||
*/ | ||
protected static $availabilityResolver; | ||
|
||
/** | ||
* Create a new signal registrar instance. | ||
* | ||
* @param \Symfony\Component\Console\SignalRegistry\SignalRegistry $registry | ||
* @return void | ||
*/ | ||
public function __construct($registry) | ||
{ | ||
$this->registry = $registry; | ||
|
||
$this->previousHandlers = $this->getHandlers(); | ||
} | ||
|
||
/** | ||
* Register a new signal handler. | ||
* | ||
* @param int $signal | ||
* @param callable(int $signal): void $callback | ||
* @return void | ||
*/ | ||
public function register($signal, $callback) | ||
{ | ||
$this->previousHandlers[$signal] ??= $this->initializeSignal($signal); | ||
|
||
with($this->getHandlers(), function ($handlers) use ($signal) { | ||
$handlers[$signal] ??= $this->initializeSignal($signal); | ||
|
||
$this->setHandlers($handlers); | ||
}); | ||
|
||
$this->registry->register($signal, $callback); | ||
|
||
with($this->getHandlers(), function ($handlers) use ($signal) { | ||
$lastHandlerInserted = array_pop($handlers[$signal]); | ||
|
||
array_unshift($handlers[$signal], $lastHandlerInserted); | ||
|
||
$this->setHandlers($handlers); | ||
}); | ||
} | ||
|
||
/** | ||
* Gets the signal's existing handler in array format. | ||
* | ||
* @return array<int, callable(int $signal): void> | ||
*/ | ||
protected function initializeSignal($signal) | ||
{ | ||
return is_callable($existingHandler = pcntl_signal_get_handler($signal)) | ||
? [$existingHandler] | ||
: null; | ||
} | ||
|
||
/** | ||
* Unregister the current signal handlers. | ||
* | ||
* @return array<int, array<int, callable(int $signal): void>> | ||
*/ | ||
public function unregister() | ||
{ | ||
$previousHandlers = $this->previousHandlers; | ||
|
||
foreach ($previousHandlers as $signal => $handler) { | ||
if (is_null($handler)) { | ||
pcntl_signal($signal, SIG_DFL); | ||
|
||
unset($previousHandlers[$signal]); | ||
} | ||
} | ||
|
||
$this->setHandlers($previousHandlers); | ||
} | ||
|
||
/** | ||
* Execute the given callback if "signals" should be used and are available. | ||
* | ||
* @param callable $callback | ||
* @return void | ||
*/ | ||
public static function whenAvailable($callback) | ||
{ | ||
$resolver = static::$availabilityResolver; | ||
|
||
if ($resolver()) { | ||
$callback(); | ||
} | ||
} | ||
|
||
/** | ||
* Get the registry's handlers. | ||
* | ||
* @return array<int, array<int, callable>> | ||
*/ | ||
protected function getHandlers() | ||
{ | ||
return (fn () => $this->signalHandlers) | ||
->call($this->registry); | ||
} | ||
|
||
/** | ||
* Set the registry's handlers. | ||
* | ||
* @param array<int, array<int, callable(int $signal):void>> $handlers | ||
* @return void | ||
nunomaduro marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
protected function setHandlers($handlers) | ||
{ | ||
(fn () => $this->signalHandlers = $handlers) | ||
->call($this->registry); | ||
} | ||
|
||
/** | ||
* Set the availability resolver. | ||
* | ||
* @param callable(): bool | ||
* @return void | ||
*/ | ||
public static function resolveAvailabilityUsing($resolver) | ||
{ | ||
static::$availabilityResolver = $resolver; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Console; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Console\Signals; | ||
use Illuminate\Tests\Console\Fixtures\FakeSignalsRegistry; | ||
use Mockery as m; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CommandTrapTest extends TestCase | ||
{ | ||
protected $registry; | ||
|
||
protected $signals; | ||
|
||
protected $state; | ||
|
||
protected function setUp(): void | ||
{ | ||
Signals::resolveAvailabilityUsing(fn () => true); | ||
|
||
$this->registry = new FakeSignalsRegistry(); | ||
$this->state = null; | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
m::close(); | ||
} | ||
|
||
public function testTrapWhenAvailable() | ||
{ | ||
$command = $this->createCommand(); | ||
|
||
$command->trap('my-signal', function () { | ||
$this->state = 'taylorotwell'; | ||
}); | ||
|
||
$this->registry->handle('my-signal'); | ||
|
||
$this->assertSame('taylorotwell', $this->state); | ||
} | ||
|
||
public function testTrapWhenNotAvailable() | ||
{ | ||
Signals::resolveAvailabilityUsing(fn () => false); | ||
|
||
$command = $this->createCommand(); | ||
|
||
$command->trap('my-signal', function () { | ||
$this->state = 'taylorotwell'; | ||
}); | ||
|
||
$this->registry->handle('my-signal'); | ||
|
||
$this->assertNull($this->state); | ||
} | ||
|
||
public function testUntrap() | ||
{ | ||
$command = $this->createCommand(); | ||
|
||
$command->trap('my-signal', function () { | ||
$this->state = 'taylorotwell'; | ||
}); | ||
|
||
$command->untrap(); | ||
|
||
$this->registry->handle('my-signal'); | ||
|
||
$this->assertNull($this->state); | ||
} | ||
|
||
public function testNestedTraps() | ||
{ | ||
$a = $this->createCommand(); | ||
$a->trap('my-signal', fn () => $this->state .= '1'); | ||
|
||
$b = $this->createCommand(); | ||
$b->trap('my-signal', fn () => $this->state .= '2'); | ||
|
||
$c = $this->createCommand(); | ||
$c->trap('my-signal', fn () => $this->state .= '3'); | ||
|
||
$this->state = ''; | ||
$this->registry->handle('my-signal'); | ||
$this->assertSame('321', $this->state); | ||
|
||
$c->untrap(); | ||
$this->state = ''; | ||
$this->registry->handle('my-signal'); | ||
$this->assertSame('21', $this->state); | ||
|
||
$d = $this->createCommand(); | ||
$d->trap('my-signal', fn () => $this->state .= '3'); | ||
|
||
$this->state = ''; | ||
$this->registry->handle('my-signal'); | ||
$this->assertSame('321', $this->state); | ||
|
||
$d->untrap(); | ||
$this->state = ''; | ||
$this->registry->handle('my-signal'); | ||
$this->assertSame('21', $this->state); | ||
|
||
$b->untrap(); | ||
$this->state = ''; | ||
$this->registry->handle('my-signal'); | ||
$this->assertSame('1', $this->state); | ||
|
||
$a->untrap(); | ||
$this->state = ''; | ||
$this->registry->handle('my-signal'); | ||
$this->assertSame('', $this->state); | ||
} | ||
|
||
protected function createCommand() | ||
{ | ||
$command = new Command; | ||
$registry = $this->registry; | ||
|
||
(fn () => $this->signals = new Signals($registry))->call($command); | ||
|
||
return $command; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.