-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind-usable-php.php
107 lines (86 loc) · 3.33 KB
/
find-usable-php.php
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
<?php
$minimumPhpVersion = '7.4';
//check for valet
//$use_valet = !empty(shell_exec('command -v valet'));
$use_valet = false;
$use_ddev = false;
// First, check if the system's linked "php" is 7.4+; if so, return that. This
// is the most likely, most ideal, and fastest possible case
if ($use_valet) {
$linkedPhpVersion = shell_exec('valet php -r "echo phpversion();"');
} else if ($use_ddev) {
$linkedPhpVersion = shell_exec('ddev php -r "echo phpversion();"');
} else {
$linkedPhpVersion = shell_exec('php -r "echo phpversion();"');
}
if (version_compare($linkedPhpVersion, $minimumPhpVersion) >= 0) {
if ($use_valet) {
echo exec('valet php -dxdebug.remote_enable=true -dxdebug.mode=debug -dxdebug.start_with_request=yes -r \'echo $_SERVER["_"];\'');
} else if ($use_ddev) {
echo exec('ddev php -r \'echo $_SERVER["_"];\'');
} else {
echo exec('which php');
}
return;
}
// If not, let's find it whether we have a version of PHP installed that's 7.4+;
$phps = explode(PHP_EOL, trim(shell_exec('brew list --formula | grep php')));
// Normalize version numbers
$phps = array_reduce($phps, function ($carry, $php) {
$carry[$php] = presumePhpVersionFromBrewFormulaName($php);
return $carry;
}, []);
// Filter out older versions of PHP
$modernPhps = array_filter($phps, function ($php) use ($minimumPhpVersion) {
return version_compare($php, $minimumPhpVersion) >= 0;
});
// If we don't have any modern versions of PHP, throw an error
if (empty($modernPhps)) {
throw new Exception('Sorry, but you do not have a version of PHP installed that is compatible with Charcoal Conductor (7.4+).');
}
// Sort newest version to oldest
sort($modernPhps);
$modernPhps = array_reverse($modernPhps);
// Grab the highest, set as $foundVersion, and output its path
$foundVersion = reset($modernPhps);
echo getPhpExecutablePath(array_search($foundVersion, $phps));
/**
* Function definitions.
*/
/**
* Extract PHP executable path from PHP Version.
* Copied from Brew.php and modified.
*
* @param string|null $phpFormulaName For example, "[email protected]"
* @return string
*/
function getPhpExecutablePath(string $phpFormulaName = null)
{
$brewPrefix = exec('printf $(brew --prefix)');
// Check the default `/opt/homebrew/opt/[email protected]/bin/php` location first
if (file_exists($brewPrefix."/opt/{$phpFormulaName}/bin/php")) {
return $brewPrefix."/opt/{$phpFormulaName}/bin/php";
}
// Check the `/opt/homebrew/opt/php71/bin/php` location for older installations
$oldPhpFormulaName = str_replace(['@', '.'], '', $phpFormulaName); // [email protected] to php71
if (file_exists($brewPrefix."/opt/{$oldPhpFormulaName}/bin/php")) {
return $brewPrefix."/opt/{$oldPhpFormulaName}/bin/php";
}
throw new Exception('Cannot find an executable path for provided PHP version: '.$phpFormulaName);
}
function presumePhpVersionFromBrewFormulaName(string $formulaName)
{
if ($formulaName === 'php') {
// Figure out its link
$details = json_decode(shell_exec("brew info $formulaName --json"));
if (! empty($details[0]->aliases[0])) {
$formulaName = $details[0]->aliases[0];
} else {
return null;
}
}
if (strpos($formulaName, 'php@') === false) {
return null;
}
return substr($formulaName, strpos($formulaName, '@') + 1);
}