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

Make primary valet.sock a symlink to an existing version

This commit is contained in:
Matt Stauffer
2022-03-12 23:29:01 -05:00
parent 3570c748a0
commit 51742b3795
4 changed files with 41 additions and 80 deletions

View File

@@ -52,7 +52,9 @@ public function install()
$this->files->ensureDirExists(VALET_HOME_PATH.'/Log', user());
$this->createConfigurationFiles();
$phpVersion = $this->brew->linkedPhp();
$this->createConfigurationFiles($phpVersion);
$this->symlinkPrimaryValetSock($phpVersion);
$this->restart();
}
@@ -74,12 +76,12 @@ public function uninstall()
*
* Writes FPM config file, pointing to the correct .sock file, and log and ini files.
*
* @param string|null $phpVersion
* @param string $phpVersion
* @return void
*/
public function createConfigurationFiles($phpVersion = null)
public function createConfigurationFiles($phpVersion)
{
info(sprintf('Updating PHP configuration%s...', ($phpVersion ? ' for '.$phpVersion : '')));
info("Updating PHP configuration for {$phpVersion}...");
$fpmConfigFile = $this->fpmConfigPath($phpVersion);
@@ -87,13 +89,10 @@ public function createConfigurationFiles($phpVersion = null)
// Create FPM Config File from stub
$contents = str_replace(
['VALET_USER', 'VALET_HOME_PATH'],
[user(), VALET_HOME_PATH],
['VALET_USER', 'VALET_HOME_PATH', 'valet.sock'],
[user(), VALET_HOME_PATH, self::fpmSockName($phpVersion)],
$this->files->get(__DIR__.'/../stubs/etc-phpfpm-valet.conf')
);
if ($phpVersion) {
$contents = str_replace('valet.sock', self::fpmSockName($phpVersion), $contents);
}
$this->files->put($fpmConfigFile, $contents);
// Create other config files from stubs
@@ -210,7 +209,6 @@ public function isolateDirectory($directory, $version)
$this->brew->ensureInstalled($version, [], $this->taps);
$oldCustomPhpVersion = $this->site->customPhpVersion($site); // Example output: "74"
$this->cli->quietly('sudo rm '.VALET_HOME_PATH.'/'.$this->fpmSockName($oldCustomPhpVersion));
$this->createConfigurationFiles($version);
$this->site->isolate($site, $version);
@@ -269,15 +267,6 @@ public function useVersion($version, $force = false)
// Unlink the current global PHP if there is one installed
if ($this->brew->hasLinkedPhp()) {
$linkedPhp = $this->brew->linkedPhp();
// Update the old FPM to keep running, using a custom sock file, so existing isolated sites aren't broken
$this->createConfigurationFiles($linkedPhp);
// Update existing custom Nginx config files; if they're using the old or new PHP version,
// update them to the new correct sock file location
$this->updateConfigurationForGlobalUpdate($version, $linkedPhp);
$currentVersion = $this->brew->getLinkedPhpFormula();
info(sprintf('Unlinking current version: %s', $currentVersion));
$this->brew->unlink($currentVersion);
@@ -288,9 +277,9 @@ public function useVersion($version, $force = false)
$this->stopRunning();
// remove any orphaned valet.sock files that PHP didn't clean up due to version conflicts
// Remove valet.sock
$this->files->unlink(VALET_HOME_PATH.'/valet.sock');
$this->cli->quietly('sudo rm '.VALET_HOME_PATH.'/valet*.sock');
$this->cli->quietly('sudo rm '.VALET_HOME_PATH.'/valet.sock');
$this->install();
@@ -304,6 +293,19 @@ public function useVersion($version, $force = false)
return $newVersion;
}
/**
* Symlink (Capistrano-style) a given Valet.sock file to be the primary valet.sock.
*
* @todo write tests
*
* @param string $phpVersion
* @return void
*/
public function symlinkPrimaryValetSock($phpVersion)
{
$this->files->symlink($this->fpmSockName($phpVersion), VALET_HOME_PATH.'/valet.sock');
}
/**
* If passed php7.4, or php74, 7.4, or 74 formats, normalize to php@7.4 format.
*/
@@ -356,42 +358,6 @@ public static function fpmSockName($phpVersion = null)
return "valet{$versionInteger}.sock";
}
/**
* Update all existing Nginx files when running a global PHP version update.
*
* If a given file is pointing to `valet.sock`, it's targeting the old global PHP version;
* update it to point to the new custom sock file for that version.
*
* If a given file is pointing the custom sock file for the new global version, that new
* version will now be hosted at `valet.sock`, so update the config file to point to that instead.
*
* @param string $newPhpVersion
* @param string $oldPhpVersion
* @return void
*/
public function updateConfigurationForGlobalUpdate($newPhpVersion, $oldPhpVersion)
{
collect($this->files->scandir(VALET_HOME_PATH.'/Nginx'))
->reject(function ($file) {
return starts_with($file, '.');
})
->each(function ($file) use ($newPhpVersion, $oldPhpVersion) {
$content = $this->files->get(VALET_HOME_PATH.'/Nginx/'.$file);
if (! starts_with($content, '# Valet isolated PHP version')) {
return;
}
if (strpos($content, self::fpmSockName($newPhpVersion)) !== false) {
info(sprintf('Updating site %s to keep using version: %s', $file, $newPhpVersion));
$this->files->put(VALET_HOME_PATH.'/Nginx/'.$file, str_replace(self::fpmSockName($newPhpVersion), 'valet.sock', $content));
} elseif (strpos($content, 'valet.sock') !== false) {
info(sprintf('Updating site %s to keep using version: %s', $file, $oldPhpVersion));
$this->files->put(VALET_HOME_PATH.'/Nginx/'.$file, str_replace('valet.sock', self::fpmSockName($oldPhpVersion), $content));
}
});
}
/**
* Get a list including the global PHP version and allPHP versions currently serving "isolated sites" (sites with
* custom Nginx configs pointing them to a specific PHP version).

View File

@@ -517,7 +517,7 @@ public function secure($url, $siteConf = null, $certificateExpireInDays = 396, $
// If the user had isolated the PHP version for this site, swap out .sock file
if ($phpVersion) {
$siteConf = $this->replaceSockFile($siteConf, "valet{$phpVersion}.sock", $phpVersion);
$siteConf = $this->replaceSockFile($siteConf, $phpVersion);
}
$this->files->putAsUser($this->nginxPath($url), $siteConf);
@@ -706,16 +706,14 @@ public function buildSecureNginxServer($url, $siteConf = null)
*/
public function isolate($valetSite, $phpVersion)
{
$fpmSockName = PhpFpm::fpmSockName($phpVersion);
if ($this->files->exists($this->nginxPath($valetSite))) {
// Modify the existing config if it exists (likely because it's secured)
$siteConf = $this->files->get($this->nginxPath($valetSite));
$siteConf = $this->replaceSockFile($siteConf, $fpmSockName, $phpVersion);
$siteConf = $this->replaceSockFile($siteConf, $phpVersion);
} else {
$siteConf = str_replace(
['VALET_HOME_PATH', 'VALET_SERVER_PATH', 'VALET_STATIC_PREFIX', 'VALET_SITE', 'VALET_PHP_FPM_SOCKET', 'VALET_ISOLATED_PHP_VERSION'],
[VALET_HOME_PATH, VALET_SERVER_PATH, VALET_STATIC_PREFIX, $valetSite, $fpmSockName, $phpVersion],
[VALET_HOME_PATH, VALET_SERVER_PATH, VALET_STATIC_PREFIX, $valetSite, PhpFpm::fpmSockName($phpVersion), $phpVersion],
$this->replaceLoopback($this->files->get(__DIR__.'/../stubs/site.valet.conf'))
);
}
@@ -1096,12 +1094,13 @@ public function customPhpVersion($url)
* Replace .sock file in an Nginx site configuration file contents.
*
* @param string $siteConf
* @param string $sockFile
* @param string $phpVersion
* @return string
*/
public function replaceSockFile($siteConf, $sockFile, $phpVersion)
public function replaceSockFile($siteConf, $phpVersion)
{
$sockFile = PhpFpm::fpmSockName($phpVersion);
$siteConf = preg_replace('/valet[0-9]*.sock/', $sockFile, $siteConf);
$siteConf = preg_replace('/# Valet isolated PHP version.*\n/', '', $siteConf); // Remove `Valet isolated PHP version` line from config