Skip to content

Commit c2e697b

Browse files
committed
Process object is returned for each process
1 parent 9aa8fc2 commit c2e697b

File tree

6 files changed

+243
-21
lines changed

6 files changed

+243
-21
lines changed

src/ProcessHandler/Drivers/DriversInterface.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44

55
interface DriversInterface {
66
/**
7-
* @return array
7+
* @return \Craftpip\ProcessHandler\Process[]
88
*/
99
function getAllProcesses ();
1010

11+
/**
12+
* @param $pid
13+
*
14+
* @return \Craftpip\ProcessHandler\Process[]
15+
*/
1116
function getProcessByPid ($pid);
1217
}

src/ProcessHandler/Drivers/Unix.php

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
namespace Craftpip\ProcessHandler\Drivers;
44

5+
use Craftpip\ProcessHandler\Process as Process2;
56
use Symfony\Component\Process\Process;
67

78
class Unix implements DriversInterface {
9+
/**
10+
* @return \Craftpip\ProcessHandler\Process[]
11+
*/
812
function getAllProcesses () {
913
$process = new Process('ps -eo pid,time,size,user,session,cmd');
1014
$process->run();
@@ -13,6 +17,11 @@ function getAllProcesses () {
1317
return $this->parse($op);
1418
}
1519

20+
/**
21+
* @param $pid
22+
*
23+
* @return \Craftpip\ProcessHandler\Process[]
24+
*/
1625
function getProcessByPid ($pid) {
1726
$process = new Process("ps -p $pid -o pid,time,size,user,session,cmd");
1827
$process->run();
@@ -21,6 +30,11 @@ function getProcessByPid ($pid) {
2130
return $this->parse($op);
2231
}
2332

33+
/**
34+
* @param $output
35+
*
36+
* @return \Craftpip\ProcessHandler\Process[]
37+
*/
2438
private function parse ($output) {
2539
$op = explode("\n", $output);
2640

@@ -36,18 +50,9 @@ private function parse ($output) {
3650
$line[] = $i;
3751
}
3852
$processName = implode(" ", array_slice($line, 5));
39-
$processes[] = [
40-
'name' => $processName,
41-
'pid' => $line[0],
42-
'session_name' => false,
43-
'session' => $line[4],
44-
'mem_used' => $line[2] . " KB",
45-
'status' => 'RUNNING',
46-
'username' => $line[3],
47-
'cpu_time' => $line[1],
48-
'window_title' => false,
49-
];
53+
$processes[] = new Process2($processName, $line[0], false, $line[4], $line[2] . ' KB', 'RUNNING', $line[3], $line[1], false);
5054
}
51-
print_r($processes);
55+
56+
return $processes;
5257
}
5358
}

src/ProcessHandler/Drivers/Windows.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
namespace Craftpip\ProcessHandler\Drivers;
44

55
use Craftpip\ProcessHandler\Exception\ProcessHandlerException;
6+
use Craftpip\ProcessHandler\Process as Process2;
67
use Symfony\Component\Process\Process;
78

89
class Windows implements DriversInterface {
10+
11+
/**
12+
* @return \Craftpip\ProcessHandler\Process[]
13+
*/
914
function getAllProcesses () {
1015
$command = "tasklist /V";
1116
$process = new Process($command);
@@ -15,6 +20,11 @@ function getAllProcesses () {
1520
return $this->parseOutput($op);
1621
}
1722

23+
/**
24+
* @param $pid
25+
*
26+
* @return \Craftpip\ProcessHandler\Process[]
27+
*/
1828
function getProcessByPid ($pid) {
1929
$command = "tasklist /V /fi \"pid eq $pid\"";
2030
$process = new Process($command);
@@ -24,6 +34,11 @@ function getProcessByPid ($pid) {
2434
return $this->parseOutput($op);
2535
}
2636

37+
/**
38+
* @param $processName
39+
*
40+
* @return \Craftpip\ProcessHandler\Process[]
41+
*/
2742
function getProcessByProcessName ($processName) {
2843
$command = "tasklist /V /fi \"imagename eq $processName\"";
2944
$process = new Process($command);
@@ -33,6 +48,11 @@ function getProcessByProcessName ($processName) {
3348
return $this->parseOutput($op);
3449
}
3550

51+
/**
52+
* @param $output
53+
*
54+
* @return \Craftpip\ProcessHandler\Process[]
55+
*/
3656
private function parseOutput ($output) {
3757
$op = explode("\n", $output);
3858
if (count($op) == 1) {
@@ -49,7 +69,7 @@ private function parseOutput ($output) {
4969
if ($k < 2)
5070
continue;
5171

52-
$processes[] = [
72+
$p = [
5373
'name' => trim(substr($o, 0, $cs[0] + 1)),
5474
'pid' => trim(substr($o, $cs[0] + 1, $cs[1] + 1)),
5575
'session_name' => trim(substr($o, $cs[0] + $cs[1] + 2, $cs[2] + 1)),
@@ -60,6 +80,9 @@ private function parseOutput ($output) {
6080
'cpu_time' => trim(substr($o, $cs[0] + $cs[1] + $cs[2] + $cs[3] + $cs[4] + $cs[5] + $cs[6] + 7, $cs[7] + 1)),
6181
'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)),
6282
];
83+
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']);
6386
}
6487

6588
return $processes;

src/ProcessHandler/Process.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
namespace Craftpip\ProcessHandler;
4+
5+
use Craftpip\ProcessHandler\Drivers\DriversInterface;
6+
use Craftpip\ProcessHandler\Drivers\Unix;
7+
use Craftpip\ProcessHandler\Drivers\Windows;
8+
use Craftpip\ProcessHandler\Exception\ProcessHandlerException;
9+
10+
class Process {
11+
12+
private $name;
13+
14+
private $pid;
15+
16+
private $session_name;
17+
18+
private $session;
19+
20+
private $mem_used;
21+
22+
private $status;
23+
24+
private $username;
25+
26+
private $cpu_time;
27+
28+
private $window_title;
29+
30+
/**
31+
* Process constructor.
32+
*
33+
* @param $name
34+
* @param $pid
35+
* @param $session_name
36+
* @param $session
37+
* @param $mem_used
38+
* @param $status
39+
* @param $username
40+
* @param $cpu_time
41+
* @param $window_title
42+
*/
43+
public function __construct ($name, $pid, $session_name, $session, $mem_used, $status, $username, $cpu_time, $window_title) {
44+
$this->name = $name;
45+
$this->pid = $pid;
46+
$this->session_name = $session_name;
47+
$this->session = $session;
48+
$this->mem_used = $mem_used;
49+
$this->status = $status;
50+
$this->username = $username;
51+
$this->cpu_time = $cpu_time;
52+
$this->window_title = $window_title;
53+
}
54+
55+
/**
56+
* @return bool
57+
*/
58+
public function isRunning () {
59+
return (new ProcessHandler())->isRunning($this->getPid());
60+
}
61+
62+
/**
63+
* @return mixed
64+
*/
65+
public function getName () {
66+
return $this->name;
67+
}
68+
69+
/**
70+
* @return mixed
71+
*/
72+
public function getPid () {
73+
return $this->pid;
74+
}
75+
76+
/**
77+
* @return mixed
78+
*/
79+
public function getSessionName () {
80+
return $this->session_name;
81+
}
82+
83+
/**
84+
* @return mixed
85+
*/
86+
public function getSession () {
87+
return $this->session;
88+
}
89+
90+
/**
91+
* @return mixed
92+
*/
93+
public function getMemUsed () {
94+
return $this->mem_used;
95+
}
96+
97+
/**
98+
* @return mixed
99+
*/
100+
public function getStatus () {
101+
return $this->status;
102+
}
103+
104+
/**
105+
* @return mixed
106+
*/
107+
public function getUsername () {
108+
return $this->username;
109+
}
110+
111+
/**
112+
* @return mixed
113+
*/
114+
public function getCpuTime () {
115+
return $this->cpu_time;
116+
}
117+
118+
/**
119+
* @return mixed
120+
*/
121+
public function getWindowTitle () {
122+
return $this->window_title;
123+
}
124+
125+
}

src/ProcessHandler/ProcessHandler.php

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Craftpip\ProcessHandler\Drivers\DriversInterface;
66
use Craftpip\ProcessHandler\Drivers\Unix;
77
use Craftpip\ProcessHandler\Drivers\Windows;
8-
use Symfony\Component\Process\Process;
8+
use Craftpip\ProcessHandler\Exception\ProcessHandlerException;
99

1010
class ProcessHandler {
1111
/**
@@ -21,17 +21,80 @@ class ProcessHandler {
2121
const systemWindows = "Win";
2222
const systemUnix = "Unix";
2323

24+
/**
25+
* @var int
26+
*/
27+
private $pid;
28+
2429
/**
2530
* ProcessHandler constructor.
31+
*
32+
* @param $pid
2633
*/
27-
public function __construct () {
34+
public function __construct ($pid = null) {
35+
$this->pid = $pid;
2836
$this->operatingSystem = $this->isWindows() ? self::systemWindows : self::systemUnix;
2937

3038
if ($this->operatingSystem == self::systemWindows) {
3139
$this->api = new Windows();
3240
} else {
3341
$this->api = new Unix();
3442
}
43+
44+
return $this->api;
45+
}
46+
47+
/**
48+
* @param $pid
49+
*
50+
* @return $this
51+
*/
52+
public function setPid ($pid) {
53+
$this->pid = $pid;
54+
55+
return $this;
56+
}
57+
58+
/**
59+
* If the process exists will return the array
60+
* else if not found will return false.
61+
*
62+
* @param null $pid
63+
*
64+
* @return \Craftpip\ProcessHandler\Process|boolean
65+
* @throws \Craftpip\ProcessHandler\Exception\ProcessHandlerException
66+
*/
67+
public function getProcess ($pid = null) {
68+
if (is_null($pid))
69+
$pid = $this->pid;
70+
71+
if (is_null($pid))
72+
throw new ProcessHandlerException('Pid is required');
73+
74+
$process = $this->api->getProcessByPid($pid);
75+
76+
return count($process) ? $process[0] : false;
77+
}
78+
79+
/**
80+
* Returns a list of processes in array.
81+
* if no processes were found will return empty array.
82+
*
83+
* @return \Craftpip\ProcessHandler\Process[]
84+
*/
85+
public function getAllProcesses () {
86+
return $this->api->getAllProcesses();
87+
}
88+
89+
/**
90+
* @param null $pid
91+
*
92+
* @return bool
93+
*/
94+
public function isRunning ($pid = null) {
95+
$process = $this->getProcess($pid);
96+
97+
return !$process ? false : true;
3598
}
3699

37100
private function isWindows () {

test.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
require_once "vendor/autoload.php";
44

55
$processHandler = new \Craftpip\ProcessHandler\ProcessHandler();
6-
$process = new \Symfony\Component\Process\Process('ls');
6+
$process = new \Symfony\Component\Process\Process('wait 5');
77
$process->start();
88
$pid = $process->getPid();
9-
print_r($processHandler->api->getProcessByPid($pid));
10-
11-
12-
$processHandler->api->getAllProcesses();
9+
echo $pid;
10+
$process = $processHandler->getProcess(12796);
11+
print_r($process);
12+
print_r($process->isRunning());
13+
print_r($process->getWindowTitle());

0 commit comments

Comments
 (0)