PosixHandler::stop()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace AsyncPHP\Process;
4
5
class PosixHandler implements Handler
6
{
7
    /**
8
     * @var int
9
     */
10
    private $pid = 0;
11
12
    /**
13
     * @inheritdoc
14
     *
15
     * @param string $id
16
     * @param string $command
17
     * @param bool $background
18
     */
19
    public function start($id, $command, $background = false)
20
    {
21
        $shhh = "";
22
23
        if ($background) {
24
            $shhh = " > /dev/null 2> /dev/null &";
25
        }
26
27
        passthru("ASYNCPHP_PROCESS_ID={$id} {$command} {$shhh}");
28
    }
29
30
    /**
31
     * @inheritdoc
32
     *
33
     * @param string $id
34
     *
35
     * @return bool
36
     */
37
    public function running($id)
38
    {
39
        $switch = "e";
40
41
        if (stristr(PHP_OS, "DAR")) {
42
            $switch = "E";
43
        }
44
45
        exec("ps {$switch} | grep \"[A]SYNCPHP_PROCESS_ID={$id}\"", $output);
46
47
        if (count($output)) {
48
            $line = trim($output[0]);
49
            $values = explode(" ", $line);
50
51
            $this->pid = (int) $values[0];
52
53
            return true;
54
        }
55
56
        return false;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     *
62
     * @param string $id
63
     */
64
    public function stop($id)
65
    {
66
        if ($this->running($id) && $this->pid) {
67
            exec("kill -9 {$this->pid}");
68
        }
69
    }
70
}
71