Total Complexity | 10 |
Total Lines | 138 |
Duplicated Lines | 0 % |
Coverage | 92.11% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
5 | class Process |
||
6 | { |
||
7 | |||
8 | /** |
||
9 | * error |
||
10 | * |
||
11 | * @var Boolean |
||
12 | */ |
||
13 | private $error; |
||
14 | |||
15 | /** |
||
16 | * error message |
||
17 | * |
||
18 | * @var String |
||
19 | */ |
||
20 | private $errorMesage; |
||
21 | |||
22 | /** |
||
23 | * command to execute |
||
24 | * |
||
25 | * @var String |
||
26 | */ |
||
27 | private $command; |
||
28 | |||
29 | /** |
||
30 | * execute ouput |
||
31 | * |
||
32 | * @var String |
||
33 | */ |
||
34 | private $result; |
||
35 | |||
36 | |||
37 | /** |
||
38 | * instanciate |
||
39 | */ |
||
40 | 8 | public function __construct() |
|
41 | { |
||
42 | 8 | $this->reset(); |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * set command to be executed |
||
47 | * |
||
48 | * @param string $command |
||
49 | * @return Process |
||
50 | */ |
||
51 | 2 | public function setCommand(string $command): Process |
|
52 | { |
||
53 | 2 | $this->reset(); |
|
54 | 2 | $this->command = $command; |
|
55 | 2 | return $this; |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * return string result for a given system command |
||
60 | * |
||
61 | * @param string $command |
||
62 | * @return string |
||
63 | */ |
||
64 | 1 | public function run(): Process |
|
65 | { |
||
66 | 1 | if (!\function_exists('proc_open')) { |
|
67 | $this->error = true; |
||
68 | $this->errorMesage = 'undefined function proc_open'; |
||
69 | return null; |
||
70 | } |
||
71 | 1 | $this->error = true; |
|
72 | 1 | $this->errorMesage = 'process is not a resource'; |
|
73 | 1 | $process = proc_open( |
|
74 | 1 | $this->command, |
|
75 | 1 | $this->getDescriptors(), |
|
76 | 1 | $pipes |
|
77 | ); |
||
78 | 1 | if (is_resource($process)) { |
|
79 | 1 | $this->result = stream_get_contents($pipes[1]); |
|
80 | 1 | $this->errorMesage = stream_get_contents($pipes[2]); |
|
81 | 1 | $this->error = !empty($this->errorMesage); |
|
82 | 1 | fclose($pipes[1]); |
|
83 | 1 | fclose($pipes[2]); |
|
84 | 1 | proc_close($process); |
|
85 | } |
||
86 | 1 | return $this; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * return true if error |
||
91 | * |
||
92 | * @return boolean |
||
93 | */ |
||
94 | 2 | public function isError(): bool |
|
95 | { |
||
96 | 2 | return $this->error === true; |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * returns error message |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | 1 | public function getErrorMessage(): string |
|
105 | { |
||
106 | 1 | return $this->errorMesage; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * return execute result as string |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | 2 | public function __toString() |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * reset errors |
||
121 | * |
||
122 | * @return Process |
||
123 | */ |
||
124 | 1 | protected function reset(): Process |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * return pipes descriptors |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | 1 | protected function getDescriptors(): array |
|
143 | ]; |
||
144 | } |
||
145 | } |
||
146 |