From 32b7dc88aaecc1f23938ae383fda62773d08f7de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Wed, 14 Jun 2023 18:11:42 +0200 Subject: [PATCH] Add `SignalMap` to map signal value to its name --- src/Symfony/Component/Console/CHANGELOG.md | 5 +++ .../Console/SignalRegistry/SignalMap.php | 36 +++++++++++++++++++ .../Tests/SignalRegistry/SignalMapTest.php | 35 ++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 src/Symfony/Component/Console/SignalRegistry/SignalMap.php create mode 100644 src/Symfony/Component/Console/Tests/SignalRegistry/SignalMapTest.php diff --git a/src/Symfony/Component/Console/CHANGELOG.md b/src/Symfony/Component/Console/CHANGELOG.md index 3428a57de3596..62972ce961b0b 100644 --- a/src/Symfony/Component/Console/CHANGELOG.md +++ b/src/Symfony/Component/Console/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.4 +--- + + * Add `SignalMap` to map signal value to its name + 6.3 --- diff --git a/src/Symfony/Component/Console/SignalRegistry/SignalMap.php b/src/Symfony/Component/Console/SignalRegistry/SignalMap.php new file mode 100644 index 0000000000000..de419bda79821 --- /dev/null +++ b/src/Symfony/Component/Console/SignalRegistry/SignalMap.php @@ -0,0 +1,36 @@ + + * + * 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 + */ +class SignalMap +{ + private static array $map; + + public static function getSignalName(int $signal): ?string + { + if (!\extension_loaded('pcntl')) { + return null; + } + + 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; + } +} diff --git a/src/Symfony/Component/Console/Tests/SignalRegistry/SignalMapTest.php b/src/Symfony/Component/Console/Tests/SignalRegistry/SignalMapTest.php new file mode 100644 index 0000000000000..887c5d7af01c5 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/SignalRegistry/SignalMapTest.php @@ -0,0 +1,35 @@ + + * + * 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; + +class SignalMapTest extends TestCase +{ + /** + * @requires extension pcntl + * + * @testWith [2, "SIGINT"] + * [9, "SIGKILL"] + * [15, "SIGTERM"] + */ + public function testSignalExists(int $signal, string $expected) + { + $this->assertSame($expected, SignalMap::getSignalName($signal)); + } + + public function testSignalDoesNotExist() + { + $this->assertNull(SignalMap::getSignalName(999999)); + } +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy