Skip to content

Commit 4f455d4

Browse files
committed
add new system features
1 parent 5273917 commit 4f455d4

File tree

1 file changed

+150
-45
lines changed

1 file changed

+150
-45
lines changed

src/System.php

Lines changed: 150 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* @author Steeve Andrian Salim
99
* @copyright Copyright (c) Steeve Andrian Salim
1010
*/
11+
1112
// ------------------------------------------------------------------------
1213

1314
namespace O2System\Filesystem;
@@ -24,10 +25,10 @@ class System
2425
/**
2526
* System::getInfo
2627
*
27-
* Get Info System
28+
* Gets system info.
2829
*
2930
* @link http://php.net/manual/en/function.php-uname.php
30-
*
31+
*
3132
* @return string Returns the description, as a string.
3233
*/
3334
public function getInfo()
@@ -40,80 +41,82 @@ public function getInfo()
4041
/**
4142
* System::getHostname
4243
*
43-
* Get Hostname
44-
*
44+
* Gets system hostname.
45+
*
4546
* @return string Host name. eg. localhost.example.com.
4647
*/
4748
public function getHostname()
4849
{
49-
return php_uname( 'n' );
50+
return php_uname('n');
5051
}
5152

5253
// ------------------------------------------------------------------------
5354

5455
/**
55-
* System::getOS
56+
* System::getName
57+
*
58+
* Gets operating system name.
5659
*
57-
* Get Operating System
58-
*
5960
* @return string Operating system name. eg. FreeBSD.
6061
*/
61-
public function getOS()
62+
public function getName()
6263
{
63-
return php_uname( 's' );
64+
return php_uname('s');
6465
}
6566

6667
// ------------------------------------------------------------------------
6768

6869
/**
6970
* System::getVersion
7071
*
71-
* Get Version Of Operating System
72-
*
72+
* Gets version of operating system.
73+
*
7374
* @return string Version information. Varies a lot between operating systems.
7475
*/
7576
public function getVersion()
7677
{
77-
return php_uname( 'v' );
78+
return php_uname('v');
7879
}
7980

8081
// ------------------------------------------------------------------------
8182

8283
/**
8384
* System::getRelease
8485
*
85-
* Get Release Name of Operating System
86-
*
86+
* Gets release name of operating system
87+
*
8788
* @return string Release name. eg. 5.1.2-RELEASE.
8889
*/
8990
public function getRelease()
9091
{
91-
return php_uname( 'r' );
92+
return php_uname('r');
9293
}
9394

9495
// ------------------------------------------------------------------------
9596

9697
/**
9798
* System::getMachine
9899
*
99-
* Get Matchine
100-
*
100+
* Gets machine type.
101+
*
101102
* @return string Machine type. eg. i386.
102103
*/
103104
public function getMachine()
104105
{
105-
return php_uname( 'm' );
106+
return php_uname('m');
106107
}
107108

108109
// ------------------------------------------------------------------------
109110

110111
/**
111112
* System::getPHPSapi
112113
*
113-
* Returns a lowercase string that describes the type of interface (the Server API, SAPI) that PHP is using. For example, in CLI PHP this string will be "cli" whereas with Apache it may have several different values depending on the exact SAPI used. Possible values are listed below.
114-
*
114+
* Returns a lowercase string that describes the type of interface (the Server API, SAPI) that PHP is using. For
115+
* example, in CLI PHP this string will be "cli" whereas with Apache it may have several different values depending
116+
* on the exact SAPI used. Possible values are listed below.
117+
*
115118
* @link http://php.net/manual/en/function.php-sapi-name.php
116-
*
119+
*
117120
* @return string Returns the interface type, as a lowercase string.
118121
*/
119122
public function getPhpSapi()
@@ -125,10 +128,11 @@ public function getPhpSapi()
125128

126129
/**
127130
* System::getPhpVersion
128-
*
131+
*
129132
* Gets the current PHP version
130-
*
131-
* @return void If the optional extension parameter is specified, phpversion() returns the version of that extension, or FALSE if there is no version information associated or the extension isn't enabled.
133+
*
134+
* @return void If the optional extension parameter is specified, phpversion() returns the version of that
135+
* extension, or FALSE if there is no version information associated or the extension isn't enabled.
132136
*/
133137
public function getPhpVersion()
134138
{
@@ -139,28 +143,32 @@ public function getPhpVersion()
139143

140144
/**
141145
* System::getPhpExtensionVersion
142-
*
146+
*
147+
* Gets php extension version.
148+
*
143149
* @param string $extension An optional extension name.
150+
*
144151
* @return void
145152
*/
146-
public function getPhpExtensionVersion( $extension )
153+
public function getPhpExtensionVersion($extension)
147154
{
148-
return phpversion( $extension );
155+
return phpversion($extension);
149156
}
150157

151158
// ------------------------------------------------------------------------
152159

153160
/**
154161
* System::getPhpExtensions
155162
*
156-
* Get Php Extensions
157-
*
163+
* Gets Php Extensions
164+
*
158165
* @param boolean $zendExtensions
166+
*
159167
* @return array Returns an array with the names of all modules compiled and loaded
160168
*/
161-
public function getPhpExtensions( $zendExtensions = false )
169+
public function getPhpExtensions($zendExtensions = false)
162170
{
163-
return get_loaded_extensions( $zendExtensions );
171+
return get_loaded_extensions($zendExtensions);
164172
}
165173

166174
// ------------------------------------------------------------------------
@@ -169,22 +177,23 @@ public function getPhpExtensions( $zendExtensions = false )
169177
* System::isPhpExtensionLoaded
170178
*
171179
* Get Status Is Php Extension Loaded.
172-
*
180+
*
173181
* @param string $extension An optional extension name.
182+
*
174183
* @return boolean
175184
*/
176-
public function isPhpExtensionLoaded( $extension )
185+
public function isPhpExtensionLoaded($extension)
177186
{
178-
return (bool)extension_loaded( $extension );
187+
return (bool)extension_loaded($extension);
179188
}
180189

181190
// ------------------------------------------------------------------------
182191

183192
/**
184193
* System::getZendVersion
185-
*
194+
*
186195
* Gets the version of the current Zend engine
187-
*
196+
*
188197
* @return string Returns the Zend Engine version number, as a string.
189198
*/
190199
public function getZendVersion()
@@ -197,26 +206,122 @@ public function getZendVersion()
197206
/**
198207
* System::getZendOptimizerVersion
199208
*
200-
* Get Version of Zend Optimizer
201-
*
209+
* Gets Version of Zend Optimizer
210+
*
202211
* @return boolean Returns TRUE if function_name exists and is a function, FALSE otherwise.
203212
*/
204213
public function getZendOptimizerVersion()
205214
{
206-
return function_exists( 'zend_optimizer_version' ) ? zend_optimizer_version() : false;
215+
return function_exists('zend_optimizer_version') ? zend_optimizer_version() : false;
207216
}
208217

209218
// ------------------------------------------------------------------------
210219

211220
/**
212221
* System::getConfigurations
213-
*
214-
* @param null|string $extension An Optional extension name
215-
* @param boolean $details
222+
*
223+
* @param null|string $extension An Optional extension name
224+
* @param boolean $details
225+
*
216226
* @return mixed Returns the return value of the callback, or FALSE on error.
217227
*/
218-
public function getConfigurations( $extension = null, $details = true )
228+
public function getConfigurations($extension = null, $details = true)
219229
{
220-
return call_user_func_array( 'ini_get_all', func_get_args() );
230+
return call_user_func_array('ini_get_all', func_get_args());
221231
}
232+
233+
// ------------------------------------------------------------------------
234+
235+
/**
236+
* System::getCpuCores
237+
*
238+
* Gets the numbers of system cores.
239+
*
240+
* @return int
241+
*/
242+
public function getCpuCores()
243+
{
244+
$numCpus = 1;
245+
if (is_file('/proc/cpuinfo')) {
246+
$cpuinfo = file_get_contents('/proc/cpuinfo');
247+
preg_match_all('/^processor/m', $cpuinfo, $matches);
248+
$numCpus = count($matches[ 0 ]);
249+
} else {
250+
if ('WIN' == strtoupper(substr(PHP_OS, 0, 3))) {
251+
$process = @popen('wmic cpu get NumberOfCores', 'rb');
252+
if (false !== $process) {
253+
fgets($process);
254+
$numCpus = intval(fgets($process));
255+
pclose($process);
256+
}
257+
} else {
258+
$process = @popen('sysctl -a', 'rb');
259+
if (false !== $process) {
260+
$output = stream_get_contents($process);
261+
preg_match('/hw.ncpu: (\d+)/', $output, $matches);
262+
if ($matches) {
263+
$numCpus = intval($matches[ 1 ][ 0 ]);
264+
}
265+
pclose($process);
266+
}
267+
}
268+
}
269+
270+
return $numCpus;
271+
}
272+
273+
// ------------------------------------------------------------------------
274+
275+
/**
276+
* System::getMacAddress
277+
*
278+
* Gets system mac address.
279+
*
280+
* @return string
281+
*/
282+
public function getMacAddress()
283+
{
284+
switch (PHP_OS) {
285+
default:
286+
case 'Darwin':
287+
case 'FreeBSD':
288+
$cmd = '/sbin/ifconfig';
289+
break;
290+
case 'Windows':
291+
$cmd = "ipconfig /all ";
292+
break;
293+
}
294+
295+
$string = trim(shell_exec($cmd));
296+
297+
if (preg_match_all('/([0-9a-f]{2}:){5}\w\w/i', $string, $matches)) {
298+
if (isset($matches[ 0 ])) {
299+
return reset($matches[ 0 ]); // get first mac address
300+
}
301+
} else {
302+
return implode(':', str_split(substr(md5('none'), 0, 12), 2));
303+
}
304+
}
305+
306+
// ------------------------------------------------------------------------
307+
308+
/**
309+
* System::getLoadAvg
310+
*
311+
* Gets system load averages.
312+
*
313+
* @param int $interval
314+
*
315+
* @return float
316+
*/
317+
public function getLoadAvg($interval = 1)
318+
{
319+
$rs = sys_getloadavg();
320+
$interval = $interval >= 1 && 3 <= $interval ? $interval : 1;
321+
$load = $rs[ $interval ];
322+
323+
return round(($load * 100) / $this->getCpuCores(), 2);
324+
}
325+
326+
// ------------------------------------------------------------------------
222327
}

0 commit comments

Comments
 (0)