-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathPhpExecutable.php
More file actions
128 lines (106 loc) · 3.12 KB
/
Copy pathPhpExecutable.php
File metadata and controls
128 lines (106 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace PHP_Parallel_Lint\PhpParallelLint\Process;
use PHP_Parallel_Lint\PhpParallelLint\Exceptions\RuntimeException;
class PhpExecutable
{
/** @var string */
private $path;
/**
* Version as PHP_VERSION_ID constant
* @var int
*/
private $versionId;
/** @var string */
private $hhvmVersion;
/** @var bool */
private $isHhvmType;
/**
* @param string $path
* @param int $versionId
* @param string $hhvmVersion
* @param bool $isHhvmType
*/
public function __construct($path, $versionId, $hhvmVersion, $isHhvmType)
{
$this->path = $path;
$this->versionId = $versionId;
$this->hhvmVersion = $hhvmVersion;
$this->isHhvmType = $isHhvmType;
}
/**
* @return string
*/
public function getHhvmVersion()
{
return $this->hhvmVersion;
}
/**
* @return boolean
*/
public function isIsHhvmType()
{
return $this->isHhvmType;
}
/**
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* @return int
*/
public function getVersionId()
{
return $this->versionId;
}
/**
* @param string $phpExecutable
* @return PhpExecutable
* @throws \Exception
*/
public static function getPhpExecutable($phpExecutable)
{
$codeToExecute = <<<PHP
echo 'PHP;', PHP_VERSION_ID, ';', defined('HPHP_VERSION') ? HPHP_VERSION : null;
PHP;
$process = new Process($phpExecutable, array('-n', '-r', $codeToExecute));
$process->waitForFinish();
try {
if ($process->getStatusCode() !== 0 && $process->getStatusCode() !== 255) {
throw new RuntimeException("Unable to execute '{$phpExecutable}'.");
}
return self::getPhpExecutableFromOutput($phpExecutable, $process->getOutput());
} catch (RuntimeException $e) {
// Try HHVM type
$process = new Process($phpExecutable, array('--php', '-r', $codeToExecute));
$process->waitForFinish();
if ($process->getStatusCode() !== 0 && $process->getStatusCode() !== 255) {
throw new RuntimeException("Unable to execute '{$phpExecutable}'.");
}
return self::getPhpExecutableFromOutput($phpExecutable, $process->getOutput(), $isHhvmType = true);
}
}
/**
* @param string $phpExecutable
* @param string $output
* @param bool $isHhvmType
* @return PhpExecutable
* @throws RuntimeException
*/
private static function getPhpExecutableFromOutput($phpExecutable, $output, $isHhvmType = false)
{
$parts = explode(';', $output);
if ($parts[0] !== 'PHP' || !preg_match('~([0-9]+)~', $parts[1], $matches)) {
throw new RuntimeException("'{$phpExecutable}' is not valid PHP binary.");
}
$hhvmVersion = isset($parts[2]) ? $parts[2] : false;
return new PhpExecutable(
$phpExecutable,
intval($matches[1]),
$hhvmVersion,
$isHhvmType
);
}
}