-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCurrentProcess.php
63 lines (56 loc) · 1.34 KB
/
CurrentProcess.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
/*
* This file is part of the slince/process package.
*
* (c) Slince <taosikai@yeah.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Slince\Process;
final class CurrentProcess
{
/**
* @var int|null
*/
protected ?int $pid;
public function __construct()
{
pcntl_async_signals(true);
}
/**
* Returns the curren process id.
*
* @return int
*/
public function pid(): int
{
if (null === $this->pid) {
$this->pid = posix_getpid();
}
return $this->pid;
}
/**
* Registers a callback for some signals.
*
* @param array|int $signals a signal or an array of signals
* @param callable|int $handler
* @param bool $restartSysCalls
*/
public function signal(array|int $signals, callable|int $handler, bool $restartSysCalls = true): void
{
foreach ((array)$signals as $signal) {
pcntl_signal($signal, $handler, $restartSysCalls);
}
}
/**
* Gets the handler for a signal
* @param int $signal
* @return callable|int
*/
public function getSignalHandler(int $signal): callable|int
{
return pcntl_signal_get_handler($signal);
}
}