summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/process/src/ProcessInfo.php
blob: db740d1f54972ba237a9271252e80685ca0f6ae6 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php

namespace gipfl\Process;

use gipfl\Json\JsonSerialization;
use gipfl\LinuxHealth\Memory;
use React\ChildProcess\Process;

class ProcessInfo implements JsonSerialization
{
    /** @var ?int */
    protected $pid;

    /** @var string */
    protected $command;

    /** @var bool */
    protected $running;

    /** @var ?object */
    protected $memory;

    public static function forProcess(Process $process)
    {
        $self = new static();
        $self->pid = $process->getPid();
        $self->command = $process->getCommand();
        $self->running = $process->isRunning();
        if ($memory = Memory::getUsageForPid($self->pid)) {
            $self->memory = $memory;
        }

        return $self;
    }

    public static function fromSerialization($any)
    {
        $self = new static();
        $self->pid = $any->pid;
        $self->command = $any->command;
        $self->running = $any->running;
        $self->memory = $any->memory;

        return $self;
    }

    /**
     * @return int|null
     */
    public function getPid()
    {
        return $this->pid;
    }

    /**
     * @return string
     */
    public function getCommand()
    {
        return $this->command;
    }

    /**
     * @return bool
     */
    public function isRunning()
    {
        return $this->running;
    }

    /**
     * @return object|null
     */
    public function getMemory()
    {
        return $this->memory;
    }

    #[\ReturnTypeWillChange]
    public function jsonSerialize()
    {
        return (object) [
            'pid'     => $this->pid,
            'command' => $this->command,
            'running' => $this->running,
            'memory'  => $this->memory,
        ];
    }
}