-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
SignalMap
to map signal value to its name
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
6.4 | ||
--- | ||
|
||
* Add `SignalMap` to map signal value to its name | ||
|
||
6.3 | ||
--- | ||
|
||
|
32 changes: 32 additions & 0 deletions
32
src/Symfony/Component/Console/SignalRegistry/SignalMap.php
This file contains 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,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\SignalRegistry; | ||
|
||
/** | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
*/ | ||
class SignalMap | ||
{ | ||
private static array $map; | ||
|
||
public static function getSignalName(int $signal): ?string | ||
{ | ||
if (!isset(self::$map)) { | ||
$r = new \ReflectionExtension('pcntl'); | ||
$c = $r->getConstants(); | ||
$map = array_filter($c, fn ($k) => str_starts_with($k, 'SIG') && !str_starts_with($k, 'SIG_'), \ARRAY_FILTER_USE_KEY); | ||
self::$map = array_flip($map); | ||
} | ||
|
||
return self::$map[$signal] ?? null; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Symfony/Component/Console/Tests/SignalRegistry/SignalMapTest.php
This file contains 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,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Tests\SignalRegistry; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Console\SignalRegistry\SignalMap; | ||
|
||
/** | ||
* @requires extension pcntl | ||
*/ | ||
class SignalMapTest extends TestCase | ||
{ | ||
/** | ||
* @testWith [2, "SIGINT"] | ||
* [9, "SIGKILL"] | ||
* [15, "SIGTERM"] | ||
* [99999, null] | ||
*/ | ||
public function test(int $signal, ?string $expected) | ||
{ | ||
$this->assertSame($expected, SignalMap::getSignalName($signal)); | ||
} | ||
} |