8000 Add `SignalMap` to map signal value to its name · symfony/symfony@dfec409 · GitHub
[go: up one dir, main page]

Skip to content

Commit dfec409

Browse files
committed
Add SignalMap to map signal value to its name
1 parent 52a9292 commit dfec409

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.4
5+
---
6+
7+
* Add `SignalMap` to map signal value to its name
8+
49
6.3
510
---
611

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\SignalRegistry;
13+
14+
/**
15+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
16+
*/
17+
class SignalMap
18+
{
19+
private static array $map;
20+
21+
public static function getSignalName(int $signal): ?string
22+
{
23+
if (!isset(self::$map)) {
24+
$r = new \ReflectionExtension('pcntl');
25+
$c = $r->getConstants();
26+
$map = array_filter($c, fn ($k) => str_starts_with($k, 'SIG') && !str_starts_with($k, 'SIG_'), \ARRAY_FILTER_USE_KEY);
27+
self::$map = array_flip($map);
28+
}
29+
30+
return self::$map[$signal] ?? null;
31+
}
32+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tests\SignalRegistry;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\SignalRegistry\SignalMap;
16+
17+
/**
18+
* @requires extension pcntl
19+
*/
20+
class SignalMapTest extends TestCase
21+
{
22+
/**
23+
* @testWith [2, "SIGINT"]
24+
* [9, "SIGKILL"]
25+
* [15, "SIGTERM"]
26+
* [99999, null]
27+
*/
28+
public function test(int $signal, ?string $expected)
29+
{
30+
$this->assertSame($expected, SignalMap::getSignalName($signal));
31+
}
32+
}

0 commit comments

Comments
 (0)
0