1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-04 16:10: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

View File

@@ -35,13 +35,7 @@ public function test_fpm_is_configured_with_the_correct_user_group_and_port()
copy(__DIR__.'/files/fpm.conf', __DIR__.'/output/fpm.conf');
mkdir(__DIR__.'/output/conf.d');
copy(__DIR__.'/files/php-memory-limits.ini', __DIR__.'/output/conf.d/php-memory-limits.ini');
resolve(StubForUpdatingFpmConfigFiles::class)->createConfigurationFiles();
$contents = file_get_contents(__DIR__.'/output/fpm.conf');
$this->assertStringContainsString(sprintf("\nuser = %s", user()), $contents);
$this->assertStringContainsString("\ngroup = staff", $contents);
$this->assertStringContainsString("\nlisten = ".VALET_HOME_PATH.'/valet.sock', $contents);
// Passing specific version will change the .sock file
resolve(StubForUpdatingFpmConfigFiles::class)->createConfigurationFiles('php@7.2');
$contents = file_get_contents(__DIR__.'/output/fpm.conf');
$this->assertStringContainsString(sprintf("\nuser = %s", user()), $contents);
@@ -113,6 +107,8 @@ public function test_utilized_php_versions()
public function test_global_php_version_update_will_swap_socks()
{
$this->markTestIncomplete('Needs deleting or refactoring');
$fileSystemMock = Mockery::mock(Filesystem::class);
$phpFpmMock = Mockery::mock(PhpFpm::class, [
@@ -315,11 +311,11 @@ public function test_use_version_if_already_linked_php_will_unlink_before_instal
])->makePartial();
$phpFpmMock->shouldReceive('install');
$cliMock->shouldReceive('quietly')->with('sudo rm '.VALET_HOME_PATH.'/valet*.sock')->once();
$cliMock->shouldReceive('quietly')->with('sudo rm '.VALET_HOME_PATH.'/valet.sock')->once();
$fileSystemMock->shouldReceive('unlink')->with(VALET_HOME_PATH.'/valet.sock')->once();
$phpFpmMock->shouldReceive('createConfigurationFiles')->with('php@7.1')->once();
$phpFpmMock->shouldReceive('updateConfigurationForGlobalUpdate')->withArgs(['php@7.2', 'php@7.1'])->once();
// $phpFpmMock->shouldReceive('createConfigurationFiles')->with('php@7.1')->once();
// $phpFpmMock->shouldReceive('updateConfigurationForGlobalUpdate')->withArgs(['php@7.2', 'php@7.1'])->once();
$brewMock->shouldReceive('supportedPhpVersions')->andReturn(collect([
'php@7.2',
@@ -384,7 +380,7 @@ public function test_isolate_will_isolate_a_site()
$brewMock->shouldNotReceive('unlink');
$phpFpmMock->shouldNotReceive('stopRunning');
$phpFpmMock->shouldNotReceive('install');
$phpFpmMock->shouldNotReceive('updateConfigurationForGlobalUpdate');
// $phpFpmMock->shouldNotReceive('updateConfigurationForGlobalUpdate');
$this->assertSame(null, $phpFpmMock->isolateDirectory('test', 'php@7.2'));
}

View File

@@ -602,7 +602,7 @@ public function test_isolation_will_persist_when_adding_ssl_certificate()
// If site has an isolated PHP version for the site, it would replace .sock file
$siteMock->shouldReceive('customPhpVersion')->with('site1.test')->andReturn('73')->once();
$siteMock->shouldReceive('replaceSockFile')->withArgs([Mockery::any(), 'valet73.sock', '73'])->once();
$siteMock->shouldReceive('replaceSockFile')->withArgs([Mockery::any(), '73'])->once();
resolve(Site::class)->secure('site1.test');
// For sites without an isolated PHP version, nothing should be replaced
@@ -757,28 +757,28 @@ public function test_replace_sock_file_in_nginx_config()
// When switching to php71, valet71.sock should be replaced with valet.sock;
// isolation header should be prepended
$this->assertEquals(
'# Valet isolated PHP version : 71'.PHP_EOL.'server { fastcgi_pass: valet.sock }',
$site->replaceSockFile('server { fastcgi_pass: valet71.sock }', 'valet.sock', '71')
'# Valet isolated PHP version : 71'.PHP_EOL.'server { fastcgi_pass: valet71.sock }',
$site->replaceSockFile('server { fastcgi_pass: valet71.sock }', '71')
);
// When switching to php72, valet.sock should be replaced with valet72.sock
$this->assertEquals(
'# Valet isolated PHP version : 72'.PHP_EOL.'server { fastcgi_pass: valet72.sock }',
$site->replaceSockFile('server { fastcgi_pass: valet.sock }', 'valet72.sock', '72')
$site->replaceSockFile('server { fastcgi_pass: valet.sock }', '72')
);
// When switching to php73 from php72, valet72.sock should be replaced with valet73.sock;
// isolation header should be updated to php@7.3
$this->assertEquals(
'# Valet isolated PHP version : 73'.PHP_EOL.'server { fastcgi_pass: valet73.sock }',
$site->replaceSockFile('# Valet isolated PHP version : 72'.PHP_EOL.'server { fastcgi_pass: valet72.sock }', 'valet73.sock', '73')
$site->replaceSockFile('# Valet isolated PHP version : 72'.PHP_EOL.'server { fastcgi_pass: valet72.sock }', '73')
);
// When switching to php72 from php74, valet72.sock should be replaced with valet74.sock;
// isolation header should be updated to php@7.4
$this->assertEquals(
'# Valet isolated PHP version : php@7.4'.PHP_EOL.'server { fastcgi_pass: valet74.sock }',
$site->replaceSockFile('# Valet isolated PHP version : 72'.PHP_EOL.'server { fastcgi_pass: valet.sock }', 'valet74.sock', 'php@7.4')
$site->replaceSockFile('# Valet isolated PHP version : 72'.PHP_EOL.'server { fastcgi_pass: valet.sock }', 'php@7.4')
);
}
}