1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-04 16:10:08 +01:00

Rework PHP version resolution

Last year's Homebrew's PHP packaging changes altered their version numbering strategy.
Now that their changes appear to have stabilized, Valet also needs some updates to match.

The `linkedPhp()` function was parsing the symlinked directory name for where the php binaries are stored, but that numbering strategy has morphed over time.

This PR changes the logic to accommodate the most common directory naming strategies I can find, including those of older installs.
I've included some examples of these names in code comments for future reference since finding a variety of them can be complicated.
This commit is contained in:
Chris Brown
2019-01-08 00:46:39 -05:00
committed by Matt Stauffer
parent 7dd06db2b0
commit 83b1b2c467
2 changed files with 39 additions and 10 deletions

View File

@@ -192,17 +192,28 @@ function stopService($services)
function linkedPhp()
{
if (! $this->files->isLink('/usr/local/bin/php')) {
throw new DomainException("Unable to determine linked PHP.");
throw new DomainException("Homebrew PHP appears not to be linked.");
}
$resolvedPath = $this->files->readLink('/usr/local/bin/php');
return $this->supportedPhpVersions()->first(function ($version) use ($resolvedPath) {
$resolvedPathNormalized= preg_replace('/([@|\.])/', '', $resolvedPath);
$versionNormalized = preg_replace('/([@|\.])/', '', $version);
return strpos($resolvedPathNormalized, "/$versionNormalized/") !== false;
}, function () {
throw new DomainException("Unable to determine linked PHP.");
/**
* Typical homebrew path resolutions are like:
* "../Cellar/php@7.2/7.2.13/bin/php"
* or older styles:
* "../Cellar/php/7.2.9_2/bin/php
* "../Cellar/php55/bin/php
*/
preg_match('~\w{3,}/(php)(@?\d\.?\d)?/(\d\.\d)?([_\d\.]*)?/?\w{3,}~', $resolvedPath, $matches);
$resolvedPhpVersion = $matches[3] ?: $matches[2];
return $this->supportedPhpVersions()->first(
function ($version) use ($resolvedPhpVersion) {
$resolvedVersionNormalized = preg_replace('/[^\d]/', '', $resolvedPhpVersion);
$versionNormalized = preg_replace('/[^\d]/', '', $version);
return $resolvedVersionNormalized === $versionNormalized;
}, function () use ($resolvedPhpVersion) {
throw new DomainException("Unable to determine linked PHP when parsing '$resolvedPhpVersion'");
});
}