1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-05 08:30:07 +01:00

Merge pull request #1216 from NasirNobin/feature/valet-run

PHP version isolation helper for command line
This commit is contained in:
Matt Stauffer
2022-03-31 09:55:22 -04:00
committed by GitHub
6 changed files with 272 additions and 28 deletions

View File

@@ -3,6 +3,7 @@
namespace Valet;
use DomainException;
use PhpFpm;
class Brew
{
@@ -264,16 +265,7 @@ public function getParsedLinkedPhp()
$resolvedPath = $this->files->readLink(BREW_PREFIX.'/bin/php');
/**
* Typical homebrew path resolutions are like:
* "../Cellar/php@7.4/7.4.13/bin/php"
* or older styles:
* "../Cellar/php/7.4.9_2/bin/php
* "../Cellar/php55/bin/php.
*/
preg_match('~\w{3,}/(php)(@?\d\.?\d)?/(\d\.\d)?([_\d\.]*)?/?\w{3,}~', $resolvedPath, $matches);
return $matches;
return $this->parsePhpPath($resolvedPath);
}
/**
@@ -302,15 +294,51 @@ public function linkedPhp()
return $this->supportedPhpVersions()->first(
function ($version) use ($resolvedPhpVersion) {
$resolvedVersionNormalized = preg_replace('/[^\d]/', '', $resolvedPhpVersion);
$versionNormalized = preg_replace('/[^\d]/', '', $version);
return $resolvedVersionNormalized === $versionNormalized;
return $this->arePhpVersionsEqual($resolvedPhpVersion, $version);
}, function () use ($resolvedPhpVersion) {
throw new DomainException("Unable to determine linked PHP when parsing '$resolvedPhpVersion'");
});
}
/**
* Extract PHP executable path from PHP Version.
*
* @param string $phpVersion For example, "php@8.1"
* @return string
*/
public function getPhpExecutablePath($phpVersion = null)
{
if (! $phpVersion) {
return BREW_PREFIX.'/bin/php';
}
$phpVersion = PhpFpm::normalizePhpVersion($phpVersion);
// Check the default `/opt/homebrew/opt/php@8.1/bin/php` location first
if ($this->files->exists(BREW_PREFIX."/opt/{$phpVersion}/bin/php")) {
return BREW_PREFIX."/opt/{$phpVersion}/bin/php";
}
// Check the `/opt/homebrew/opt/php71/bin/php` location for older installations
$phpVersion = str_replace(['@', '.'], '', $phpVersion); // php@8.1 to php81
if ($this->files->exists(BREW_PREFIX."/opt/{$phpVersion}/bin/php")) {
return BREW_PREFIX."/opt/{$phpVersion}/bin/php";
}
// Check if the default PHP is the version we are looking for
if ($this->files->isLink(BREW_PREFIX.'/opt/php')) {
$resolvedPath = $this->files->readLink(BREW_PREFIX.'/opt/php');
$matches = $this->parsePhpPath($resolvedPath);
$resolvedPhpVersion = $matches[3] ?: $matches[2];
if ($this->arePhpVersionsEqual($resolvedPhpVersion, $phpVersion)) {
return BREW_PREFIX.'/opt/php/bin/php';
}
}
return BREW_PREFIX.'/bin/php';
}
/**
* Restart the linked PHP-FPM Homebrew service.
*
@@ -476,4 +504,39 @@ function ($exitCode, $errorOutput) {
}
);
}
/**
* Parse homebrew PHP Path.
*
* @param string $resolvedPath
* @return array
*/
public function parsePhpPath($resolvedPath)
{
/**
* Typical homebrew path resolutions are like:
* "../Cellar/php@7.4/7.4.13/bin/php"
* or older styles:
* "../Cellar/php/7.4.9_2/bin/php
* "../Cellar/php55/bin/php.
*/
preg_match('~\w{3,}/(php)(@?\d\.?\d)?/(\d\.\d)?([_\d\.]*)?/?\w{3,}~', $resolvedPath, $matches);
return $matches;
}
/**
* Check if two PHP versions are equal.
*
* @param string $versionA
* @param string $versionB
* @return bool
*/
public function arePhpVersionsEqual($versionA, $versionB)
{
$versionANormalized = preg_replace('/[^\d]/', '', $versionA);
$versionBNormalized = preg_replace('/[^\d]/', '', $versionB);
return $versionANormalized === $versionBNormalized;
}
}

View File

@@ -3,6 +3,7 @@
namespace Valet;
use DomainException;
use PhpFpm;
class Site
{
@@ -1108,4 +1109,21 @@ public function replaceSockFile($siteConf, $phpVersion)
return '# '.ISOLATED_PHP_VERSION.'='.$phpVersion.PHP_EOL.$siteConf;
}
/**
* Get PHP version from .valetphprc for a site.
*
* @param string $site
* @return string|null
*/
public function phpRcVersion($site)
{
if ($site = $this->parked()->merge($this->links())->where('site', $site)->first()) {
$path = data_get($site, 'path').'/.valetphprc';
if ($this->files->exists($path)) {
return PhpFpm::normalizePhpVersion(trim($this->files->get($path)));
}
}
}
}