8000 [Console] Add `SignalMap` to map signal value to its name by lyrixx · Pull Request #50663 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Add SignalMap to map signal value to its name #50663

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 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add SignalMap to map signal value to its name
  • Loading branch information
lyrixx committed Jun 14, 2023
commit 32b7dc88aaecc1f23938ae383fda62773d08f7de
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
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
---

Expand Down
36 changes: 36 additions & 0 deletions src/Symfony/Component/Console/SignalRegistry/SignalMap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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 (!\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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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;

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));
}
}
0