Skip to content

Commit 4087347

Browse files
committed
update unix and windows drivers
1 parent 7d26822 commit 4087347

File tree

2 files changed

+139
-118
lines changed

2 files changed

+139
-118
lines changed

src/ProcessHandler/Drivers/Unix.php

Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,63 @@
66
use Symfony\Component\Process\Process;
77

88
class Unix implements DriversInterface {
9-
/**
10-
* @return \Craftpip\ProcessHandler\Process[]
11-
*/
12-
function getAllProcesses () {
13-
$process = new Process('ps -eo pid,time,size,user,session,cmd');
14-
$process->run();
15-
$op = trim($process->getOutput());
16-
17-
return $this->parse($op);
18-
}
19-
20-
/**
21-
* @param $pid
22-
*
23-
* @return \Craftpip\ProcessHandler\Process[]
24-
*/
25-
function getProcessByPid ($pid) {
26-
$process = new Process("ps -p $pid -o pid,time,size,user,session,cmd");
27-
$process->run();
28-
$op = trim($process->getOutput());
29-
30-
return $this->parse($op);
31-
}
32-
33-
/**
34-
* @param $output
35-
*
36-
* @return \Craftpip\ProcessHandler\Process[]
37-
*/
38-
private function parse ($output) {
39-
$op = explode("\n", $output);
40-
41-
$processes = array();
42-
foreach ($op as $k => $item) {
43-
if ($k < 1)
44-
continue;
45-
46-
$item = explode(" ", $item);
47-
$line = array();
48-
foreach ($item as $i) {
49-
if ($i != '')
50-
$line[] = $i;
9+
/**
10+
* @return \Craftpip\ProcessHandler\Process[]
11+
*/
12+
function getAllProcesses() {
13+
$process = new Process([
14+
'ps',
15+
'-eo',
16+
'pid,time,size,user,session,cmd',
17+
]);
18+
$process->run();
19+
$op = trim($process->getOutput());
20+
21+
return $this->parse($op);
22+
}
23+
24+
/**
25+
* @param $pid
26+
*
27+
* @return \Craftpip\ProcessHandler\Process[]
28+
*/
29+
function getProcessByPid($pid) {
30+
$process = new Process([
31+
"ps",
32+
"-p",
33+
"$pid",
34+
"-o",
35+
"pid,time,size,user,session,cmd",
36+
]);
37+
$process->run();
38+
$op = trim($process->getOutput());
39+
40+
return $this->parse($op);
41+
}
42+
43+
/**
44+
* @param $output
45+
*
46+
* @return \Craftpip\ProcessHandler\Process[]
47+
*/
48+
private function parse($output) {
49+
$op = explode("\n", $output);
50+
51+
$processes = array();
52+
foreach ($op as $k => $item) {
53+
if ($k < 1)
54+
continue;
55+
56+
$item = explode(" ", $item);
57+
$line = array();
58+
foreach ($item as $i) {
59+
if ($i != '')
60+
$line[] = $i;
61+
}
62+
$processName = implode(" ", array_slice($line, 5));
63+
$processes[] = new Process2($processName, $line[0], false, $line[4], $line[2] . ' KB', 'RUNNING', $line[3], $line[1], false);
5164
}
52-
$processName = implode(" ", array_slice($line, 5));
53-
$processes[] = new Process2($processName, $line[0], false, $line[4], $line[2] . ' KB', 'RUNNING', $line[3], $line[1], false);
54-
}
5565

56-
return $processes;
57-
}
58-
}
66+
return $processes;
67+
}
68+
}

src/ProcessHandler/Drivers/Windows.php

Lines changed: 81 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,83 +8,94 @@
88

99
class Windows implements DriversInterface {
1010

11-
/**
12-
* @return \Craftpip\ProcessHandler\Process[]
13-
*/
14-
function getAllProcesses () {
15-
$command = "tasklist /V";
16-
$process = new Process($command);
17-
$process->run();
18-
$op = trim($process->getOutput());
11+
/**
12+
* @return \Craftpip\ProcessHandler\Process[]
13+
*/
14+
function getAllProcesses() {
15+
$process = new Process([
16+
"tasklist",
17+
"/V",
18+
]);
19+
$process->run();
20+
$op = trim($process->getOutput());
1921

20-
return $this->parseOutput($op);
21-
}
22+
return $this->parseOutput($op);
23+
}
2224

23-
/**
24-
* @param $pid
25-
*
26-
* @return \Craftpip\ProcessHandler\Process[]
27-
*/
28-
function getProcessByPid ($pid) {
29-
$command = "tasklist /V /fi \"pid eq $pid\"";
30-
$process = new Process($command);
31-
$process->run();
32-
$op = trim($process->getOutput());
25+
/**
26+
* @param $pid
27+
*
28+
* @return \Craftpip\ProcessHandler\Process[]
29+
*/
30+
function getProcessByPid($pid) {
31+
$command = "tasklist /V /fi \"pid eq $pid\"";
32+
$process = new Process([
33+
"tasklist",
34+
"/V",
35+
"/fi",
36+
"\"pid eq $pid\"",
37+
]);
38+
$process->run();
39+
$op = trim($process->getOutput());
3340

34-
return $this->parseOutput($op);
35-
}
41+
return $this->parseOutput($op);
42+
}
3643

37-
/**
38-
* @param $processName
39-
*
40-
* @return \Craftpip\ProcessHandler\Process[]
41-
*/
42-
function getProcessByProcessName ($processName) {
43-
$command = "tasklist /V /fi \"imagename eq $processName\"";
44-
$process = new Process($command);
45-
$process->run();
46-
$op = trim($process->getOutput());
44+
/**
45+
* @param $processName
46+
*
47+
* @return \Craftpip\ProcessHandler\Process[]
48+
*/
49+
function getProcessByProcessName($processName) {
50+
$command = "tasklist /V /fi \"imagename eq $processName\"";
51+
$process = new Process([
52+
"tasklist",
53+
"/V",
54+
"/fi",
55+
"\"imagename eq $processName\"",
56+
]);
57+
$process->run();
58+
$op = trim($process->getOutput());
4759

48-
return $this->parseOutput($op);
49-
}
60+
return $this->parseOutput($op);
61+
}
5062

51-
/**
52-
* @param $output
53-
*
54-
* @return \Craftpip\ProcessHandler\Process[]
55-
*/
56-
private function parseOutput ($output) {
57-
$op = explode("\n", $output);
58-
if (count($op) == 1) {
59-
return array();
60-
}
61-
$sessions = explode(" ", $op[1]);
62-
$cs = array();
63-
foreach ($sessions as $session) {
64-
$cs[] = strlen($session);
65-
}
66-
$processes = array();
63+
/**
64+
* @param $output
65+
*
66+
* @return \Craftpip\ProcessHandler\Process[]
67+
*/
68+
private function parseOutput($output) {
69+
$op = explode("\n", $output);
70+
if (count($op) == 1) {
71+
return array();
72+
}
73+
$sessions = explode(" ", $op[1]);
74+
$cs = array();
75+
foreach ($sessions as $session) {
76+
$cs[] = strlen($session);
77+
}
78+
$processes = array();
6779

68-
foreach ($op as $k => $o) {
69-
if ($k < 2)
70-
continue;
80+
foreach ($op as $k => $o) {
81+
if ($k < 2)
82+
continue;
7183

72-
$p = array(
73-
'name' => trim(substr($o, 0, $cs[0] + 1)),
74-
'pid' => trim(substr($o, $cs[0] + 1, $cs[1] + 1)),
75-
'session_name' => trim(substr($o, $cs[0] + $cs[1] + 2, $cs[2] + 1)),
76-
'session' => trim(substr($o, $cs[0] + $cs[1] + $cs[2] + 3, $cs[3] + 1)),
77-
'mem_used' => trim(substr($o, $cs[0] + $cs[1] + $cs[2] + $cs[3] + 4, $cs[4] + 1)),
78-
'status' => trim(substr($o, $cs[0] + $cs[1] + $cs[2] + $cs[3] + $cs[4] + 5, $cs[5] + 1)),
79-
'username' => trim(substr($o, $cs[0] + $cs[1] + $cs[2] + $cs[3] + $cs[4] + $cs[5] + 6, $cs[6] + 1)),
80-
'cpu_time' => trim(substr($o, $cs[0] + $cs[1] + $cs[2] + $cs[3] + $cs[4] + $cs[5] + $cs[6] + 7, $cs[7] + 1)),
81-
'window_title' => trim(substr($o, $cs[0] + $cs[1] + $cs[2] + $cs[3] + $cs[4] + $cs[5] + $cs[6] + $cs[7] + 8, $cs[8] + 1)),
82-
);
84+
$p = array(
85+
'name' => trim(substr($o, 0, $cs[0] + 1)),
86+
'pid' => trim(substr($o, $cs[0] + 1, $cs[1] + 1)),
87+
'session_name' => trim(substr($o, $cs[0] + $cs[1] + 2, $cs[2] + 1)),
88+
'session' => trim(substr($o, $cs[0] + $cs[1] + $cs[2] + 3, $cs[3] + 1)),
89+
'mem_used' => trim(substr($o, $cs[0] + $cs[1] + $cs[2] + $cs[3] + 4, $cs[4] + 1)),
90+
'status' => trim(substr($o, $cs[0] + $cs[1] + $cs[2] + $cs[3] + $cs[4] + 5, $cs[5] + 1)),
91+
'username' => trim(substr($o, $cs[0] + $cs[1] + $cs[2] + $cs[3] + $cs[4] + $cs[5] + 6, $cs[6] + 1)),
92+
'cpu_time' => trim(substr($o, $cs[0] + $cs[1] + $cs[2] + $cs[3] + $cs[4] + $cs[5] + $cs[6] + 7, $cs[7] + 1)),
93+
'window_title' => trim(substr($o, $cs[0] + $cs[1] + $cs[2] + $cs[3] + $cs[4] + $cs[5] + $cs[6] + $cs[7] + 8, $cs[8] + 1)),
94+
);
8395

84-
$processes[] = new Process2($p['name'], $p['pid'], $p['session_name'], $p['session'], $p['mem_used'], $p['status'],
85-
$p['username'], $p['cpu_time'], $p['window_title']);
86-
}
96+
$processes[] = new Process2($p['name'], $p['pid'], $p['session_name'], $p['session'], $p['mem_used'], $p['status'], $p['username'], $p['cpu_time'], $p['window_title']);
97+
}
8798

88-
return $processes;
89-
}
90-
}
99+
return $processes;
100+
}
101+
}

0 commit comments

Comments
 (0)