-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurrentProcess.php
More file actions
69 lines (59 loc) · 1.52 KB
/
CurrentProcess.php
File metadata and controls
69 lines (59 loc) · 1.52 KB
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
64
65
66
67
68
69
<?php
declare(strict_types = 1);
namespace Innmind\OperatingSystem;
use Innmind\OperatingSystem\CurrentProcess\Signals;
use Innmind\Server\Control\Server\Process\Pid;
use Innmind\Server\Status\Server\Memory\Bytes;
use Innmind\TimeContinuum\Period;
use Innmind\TimeWarp\Halt;
use Innmind\Signals\Handler;
use Innmind\Immutable\{
Attempt,
SideEffect,
};
final class CurrentProcess
{
private Halt $halt;
private Handler $handler;
private ?Signals $signals = null;
private function __construct(Halt $halt, Handler $handler)
{
$this->halt = $halt;
$this->handler = $handler;
}
/**
* @internal
*/
public static function of(Halt $halt, Handler $handler): self
{
return new self($halt, $handler);
}
/**
* @return Attempt<Pid>
*/
public function id(): Attempt
{
$pid = \getmypid();
/** @psalm-suppress ArgumentTypeCoercion */
return match ($pid) {
false => Attempt::error(new \RuntimeException('Failed to retrieve process id')),
default => Attempt::result(new Pid($pid)),
};
}
public function signals(): Signals
{
return $this->signals ??= Signals::of($this->handler);
}
/**
* @return Attempt<SideEffect>
*/
public function halt(Period $period): Attempt
{
return ($this->halt)($period);
}
public function memory(): Bytes
{
/** @psalm-suppress ArgumentTypeCoercion */
return Bytes::of(\memory_get_usage());
}
}