OS::hasBinary()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 21
rs 8.7624
cc 5
eloc 13
nc 5
nop 1
1
<?php
2
namespace nochso\Omni;
3
4
/**
5
 * OS.
6
 */
7
class OS {
8
	/**
9
	 * isWindows returns true if the current OS is Windows.
10
	 *
11
	 * @param string $phpOs Optional, defaults to the PHP_OS constant.
12
	 *
13
	 * @return bool
14
	 */
15
	public static function isWindows($phpOs = PHP_OS) {
16
		return strtolower(substr($phpOs, 0, 3)) === 'win';
17
	}
18
19
	/**
20
	 * hasBinary returns true if the binary is available in any of the PATHs.
21
	 *
22
	 * @param string $binaryName
23
	 *
24
	 * @return bool
25
	 */
26
	public static function hasBinary($binaryName) {
27
		$exec = Exec::create();
28
		if (self::isWindows()) {
29
			// 'where.exe' uses 0 for success, 1 for failure to find
30
			$exec->run('where.exe', '/q', $binaryName);
31
			return $exec->getStatus() === 0;
32
		}
33
		// 'which' uses 0 for success, 1 for failure to find
34
		$exec->run('which', $binaryName);
35
		if ($exec->getStatus() === 0) {
36
			return true;
37
		}
38
		// 'whereis' does not use status codes. Check for a matching line instead.
39
		$exec->run('whereis', '-b', $binaryName);
40
		foreach ($exec->getOutput() as $line) {
41
			if (preg_match('/^' . preg_quote($binaryName) . ': .*$/', $line) === 1) {
42
				return true;
43
			}
44
		}
45
		return false;
46
	}
47
}
48