Skip to content

Commit 16014de

Browse files
committed
feature #50663 [Console] Add SignalMap to map signal value to its name (lyrixx)
This PR was merged into the 6.4 branch. Discussion ---------- [Console] Add `SignalMap` to map signal value to its name | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | I'm migrating some code to use new SF features, but I have a very little regression. Before : ```php private function setUpSignals(): void { pcntl_signal(\SIGTERM, function () { $this->stop('SIGTERM'); }); pcntl_signal(\SIGINT, function () { $this->stop('SIGINT'); }); pcntl_async_signals(true); } ``` Now: ```php public function handleSignal(int $signal): void { // I need the following line $signal = SignalMap::getSignalName($signal); $this->stop("Signal {$signal} received."); } ``` As you can see, now in my log I have an int (`2`, `15`), and not anymore `SIGTERM`, `SIGINT`. Commits ------- 32b7dc8 Add `SignalMap` to map signal value to its name
2 parents 5bc63ad + 32b7dc8 commit 16014de

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 (!\extension_loaded('pcntl')) {
24+
return null;
25+
}
26+
27+
if (!isset(self::$map)) {
28+
$r = new \ReflectionExtension('pcntl');
29+
$c = $r->getConstants();
30+
$map = array_filter($c, fn ($k) => str_starts_with($k, 'SIG') && !str_starts_with($k, 'SIG_'), \ARRAY_FILTER_USE_KEY);
31+
self::$map = array_flip($map);
32+
}
33+
34+
return self::$map[$signal] ?? null;
35+
}
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
class SignalMapTest extends TestCase
18+
{
19+
/**
20+
* @requires extension pcntl
21+
*
22+
* @testWith [2, "SIGINT"]
23+
* [9, "SIGKILL"]
24+
* [15, "SIGTERM"]
25+
*/
26+
public function testSignalExists(int $signal, string $expected)
27+
{
28+
$this->assertSame($expected, SignalMap::getSignalName($signal));
29+
}
30+
31+
public function testSignalDoesNotExist()
32+
{
33+
$this->assertNull(SignalMap::getSignalName(999999));
34+
}
35+
}

0 commit comments

Comments
 (0)
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