Skip to content

Commit 63f4678

Browse files
authored
Merge pull request #1 from mozartk/addMacOS
add macOS related functions
2 parents 056c462 + 71ed929 commit 63f4678

File tree

4 files changed

+94
-10
lines changed

4 files changed

+94
-10
lines changed

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ php:
99
- 5.6
1010
- 5.5
1111
- 5.4
12-
- 5.3
12+
matrix:
13+
include:
14+
- php: 5.3
15+
dist: precise
1316

1417
before_script:
1518
- git --version
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Craftpip\ProcessHandler\Drivers;
4+
5+
use Craftpip\ProcessHandler\Process as Process2;
6+
use Symfony\Component\Process\Process;
7+
8+
class MacOS implements DriversInterface {
9+
/**
10+
* @return \Craftpip\ProcessHandler\Process[]
11+
*/
12+
protected $options = "pid,time,rss,user,sess,args";
13+
14+
function getAllProcesses () {
15+
$process = new Process("ps -eo $this->options");
16+
$process->run();
17+
$op = trim($process->getOutput());
18+
19+
return $this->parse($op);
20+
}
21+
22+
/**
23+
* @param $pid
24+
*
25+
* @return \Craftpip\ProcessHandler\Process[]
26+
*/
27+
function getProcessByPid ($pid) {
28+
$process = new Process("ps -p $pid -o $this->options");
29+
$process->run();
30+
$op = trim($process->getOutput());
31+
32+
return $this->parse($op);
33+
}
34+
35+
/**
36+
* @param $output
37+
*
38+
* @return \Craftpip\ProcessHandler\Process[]
39+
*/
40+
private function parse ($output) {
41+
$op = explode("\n", $output);
42+
43+
44+
$processes = [];
45+
foreach ($op as $k => $item) {
46+
if ($k < 1)
47+
continue;
48+
49+
$item = explode(" ", preg_replace('!\s+!', ' ', trim($item)));
50+
$line = [];
51+
foreach ($item as $i) {
52+
if ($i != '')
53+
$line[] = $i;
54+
}
55+
56+
$processName = implode(" ", array_slice($line, 5));
57+
$processes[] = new Process2($processName, $line[0], false, $line[4], $line[2] . ' KB', 'RUNNING', $line[3], $line[1], false);
58+
}
59+
60+
return $processes;
61+
}
62+
}

src/ProcessHandler/Process.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Craftpip\ProcessHandler;
44

55
use Craftpip\ProcessHandler\Drivers\DriversInterface;
6+
use Craftpip\ProcessHandler\Drivers\MacOS;
67
use Craftpip\ProcessHandler\Drivers\Unix;
78
use Craftpip\ProcessHandler\Drivers\Windows;
89
use Craftpip\ProcessHandler\Exception\ProcessHandlerException;

src/ProcessHandler/ProcessHandler.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Craftpip\ProcessHandler;
44

55
use Craftpip\ProcessHandler\Drivers\DriversInterface;
6+
use Craftpip\ProcessHandler\Drivers\MacOS;
67
use Craftpip\ProcessHandler\Drivers\Unix;
78
use Craftpip\ProcessHandler\Drivers\Windows;
89
use Craftpip\ProcessHandler\Exception\ProcessHandlerException;
@@ -18,6 +19,7 @@ class ProcessHandler {
1819
*/
1920
public $api;
2021

22+
const systemMacOS = "MacOS";
2123
const systemWindows = "Win";
2224
const systemUnix = "Unix";
2325

@@ -33,12 +35,18 @@ class ProcessHandler {
3335
*/
3436
public function __construct ($pid = null) {
3537
$this->pid = $pid;
36-
$this->operatingSystem = $this->isWindows() ? self::systemWindows : self::systemUnix;
37-
38-
if ($this->operatingSystem == self::systemWindows) {
39-
$this->api = new Windows();
40-
} else {
41-
$this->api = new Unix();
38+
$this->operatingSystem = $this->getCurrentOS();
39+
40+
switch($this->operatingSystem){
41+
case self::systemMacOS:
42+
$this->api = new MacOS();
43+
break;
44+
case self::systemWindows:
45+
$this->api = new Windows();
46+
break;
47+
case self::systemUnix:
48+
$this->api = new Unix();
49+
break;
4250
}
4351

4452
return $this->api;
@@ -97,8 +105,18 @@ public function isRunning ($pid = null) {
97105
return !$process ? false : true;
98106
}
99107

100-
private function isWindows () {
101-
return !(strpos(PHP_OS, 'WIN') === false);
108+
/**
109+
* Returns a current OS systems string using uname.
110+
* You can find a list of uname strings :
111+
* https://en.wikipedia.org/wiki/Uname#Table_of_standard_uname_output
112+
*
113+
* @return string
114+
*/
115+
private function getCurrentOS () {
116+
switch (true) {
117+
case stristr(PHP_OS, 'DAR'): return self::systemMacOS;
118+
case stristr(PHP_OS, 'WIN'): return self::systemWindows;
119+
default : return self::systemUnix;
120+
}
102121
}
103-
104122
}

0 commit comments

Comments
 (0)