From 383aa621379b531a532cfc015c0985859be1659f Mon Sep 17 00:00:00 2001 From: Matt Stauffer Date: Mon, 14 Mar 2022 14:40:45 -0400 Subject: [PATCH] Change magic isolation config string to "ISOLATED_PHP_VERSION" Co-Authored-By: Jacob Baker-Kretzmar <18192441+bakerkretzmar@users.noreply.github.com> --- cli/Valet/PhpFpm.php | 2 +- cli/Valet/Site.php | 6 +++--- cli/includes/helpers.php | 2 ++ cli/stubs/site.valet.conf | 2 +- tests/PhpFpmTest.php | 10 +++++----- tests/SiteTest.php | 20 ++++++++++---------- 6 files changed, 22 insertions(+), 20 deletions(-) diff --git a/cli/Valet/PhpFpm.php b/cli/Valet/PhpFpm.php index a1e209775..42ac1b097 100644 --- a/cli/Valet/PhpFpm.php +++ b/cli/Valet/PhpFpm.php @@ -258,7 +258,7 @@ public function isolatedDirectories() $configuredSites = $this->nginx->configuredSites(); return $configuredSites->filter(function ($item) { - return strpos($this->files->get(VALET_HOME_PATH.'/Nginx/'.$item), 'Valet isolated PHP version') !== false; + return strpos($this->files->get(VALET_HOME_PATH.'/Nginx/'.$item), ISOLATED_PHP_VERSION) !== false; })->map(function ($item) { return ['url' => $item, 'version' => $this->normalizePhpVersion($this->site->customPhpVersion($item))]; }); diff --git a/cli/Valet/Site.php b/cli/Valet/Site.php index 4d124c957..660f68bac 100644 --- a/cli/Valet/Site.php +++ b/cli/Valet/Site.php @@ -1082,7 +1082,7 @@ public function customPhpVersion($url) if ($this->files->exists($this->nginxPath($url))) { $siteConf = $this->files->get($this->nginxPath($url)); - if (starts_with($siteConf, '# Valet isolated PHP version')) { + if (starts_with($siteConf, '# '.ISOLATED_PHP_VERSION)) { $firstLine = explode(PHP_EOL, $siteConf)[0]; return preg_replace("/[^\d]*/", '', $firstLine); // Example output: "74" or "81" @@ -1102,8 +1102,8 @@ 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 + $siteConf = preg_replace('/# '.ISOLATED_PHP_VERSION.'.*\n/', '', $siteConf); // Remove ISOLATED_PHP_VERSION line from config - return '# Valet isolated PHP version: '.$phpVersion.PHP_EOL.$siteConf; + return '# '.ISOLATED_PHP_VERSION.'='.$phpVersion.PHP_EOL.$siteConf; } } diff --git a/cli/includes/helpers.php b/cli/includes/helpers.php index 1daf74fec..352d867fb 100644 --- a/cli/includes/helpers.php +++ b/cli/includes/helpers.php @@ -23,6 +23,8 @@ define('BREW_PREFIX', (new CommandLine())->runAsUser('printf $(brew --prefix)')); +define('ISOLATED_PHP_VERSION', 'ISOLATED_PHP_VERSION'); + /** * Output the given text to the console. * diff --git a/cli/stubs/site.valet.conf b/cli/stubs/site.valet.conf index 26f531141..9fb0a7939 100644 --- a/cli/stubs/site.valet.conf +++ b/cli/stubs/site.valet.conf @@ -1,4 +1,4 @@ -# Valet isolated PHP version: VALET_ISOLATED_PHP_VERSION +# ISOLATED_PHP_VERSION=VALET_ISOLATED_PHP_VERSION server { listen 127.0.0.1:80; server_name VALET_SITE www.VALET_SITE *.VALET_SITE; diff --git a/tests/PhpFpmTest.php b/tests/PhpFpmTest.php index 3a528227b..0632c6741 100644 --- a/tests/PhpFpmTest.php +++ b/tests/PhpFpmTest.php @@ -98,15 +98,15 @@ public function test_utilized_php_versions() $sites = [ [ 'site' => 'isolated-site-71.test', - 'conf' => '# Valet isolated PHP version: 71'.PHP_EOL.'valet71.sock', + 'conf' => '# '.ISOLATED_PHP_VERSION.'=71'.PHP_EOL.'valet71.sock', ], [ 'site' => 'isolated-site-72.test', - 'conf' => '# Valet isolated PHP version: php@7.2'.PHP_EOL.'valet72.sock', + 'conf' => '# '.ISOLATED_PHP_VERSION.'=php@7.2'.PHP_EOL.'valet72.sock', ], [ 'site' => 'isolated-site-73.test', - 'conf' => '# Valet isolated PHP version: 73'.PHP_EOL.'valet.sock', + 'conf' => '# '.ISOLATED_PHP_VERSION.'=73'.PHP_EOL.'valet.sock', ], ]; @@ -146,11 +146,11 @@ public function test_it_lists_isolated_directories() $sites = [ [ 'site' => 'isolated-site-71.test', - 'conf' => '# Valet isolated PHP version: 71'.PHP_EOL.'valet71.sock', + 'conf' => '# '.ISOLATED_PHP_VERSION.'=71'.PHP_EOL.'valet71.sock', ], [ 'site' => 'isolated-site-72.test', - 'conf' => '# Valet isolated PHP version: php@7.2'.PHP_EOL.'valet72.sock', + 'conf' => '# '.ISOLATED_PHP_VERSION.'=php@7.2'.PHP_EOL.'valet72.sock', ], [ 'site' => 'not-isolated-site.test', diff --git a/tests/SiteTest.php b/tests/SiteTest.php index 840ca94f9..e8e03d41e 100644 --- a/tests/SiteTest.php +++ b/tests/SiteTest.php @@ -659,13 +659,13 @@ public function test_can_install_nginx_site_config_for_specific_php_version() $files->shouldReceive('get') ->once() ->with($siteMock->nginxPath('site1.test')) - ->andReturn('# Valet isolated PHP version: php@7.4'.PHP_EOL.'server { fastcgi_pass: valet74.sock }'); + ->andReturn('# '.ISOLATED_PHP_VERSION.'=php@7.4'.PHP_EOL.'server { fastcgi_pass: valet74.sock }'); $files->shouldReceive('putAsUser') ->once() ->withArgs([ $siteMock->nginxPath('site1.test'), - '# Valet isolated PHP version: php@8.0'.PHP_EOL.'server { fastcgi_pass: valet80.sock }', + '# '.ISOLATED_PHP_VERSION.'=php@8.0'.PHP_EOL.'server { fastcgi_pass: valet80.sock }', ]); $siteMock->isolate('site1.test', 'php@8.0'); @@ -682,7 +682,7 @@ public function test_can_install_nginx_site_config_for_specific_php_version() ->withArgs([ $siteMock->nginxPath('site2.test'), Mockery::on(function ($argument) { - return preg_match('/^# Valet isolated PHP version: php@8.0/', $argument) + return preg_match('/^# '.ISOLATED_PHP_VERSION.'=php@8.0/', $argument) && preg_match('#fastcgi_pass "unix:.*/valet80.sock#', $argument) && strpos($argument, 'server_name site2.test www.site2.test *.site2.test;') !== false; }), @@ -733,7 +733,7 @@ public function test_retrieves_custom_php_version_from_nginx_config() $files->shouldReceive('get') ->once() ->with($siteMock->nginxPath('site1.test')) - ->andReturn('# Valet isolated PHP version: php@7.4'); + ->andReturn('# '.ISOLATED_PHP_VERSION.'=php@7.4'); $this->assertEquals('74', resolve(Site::class)->customPhpVersion('site1.test')); // Site without any custom nginx config @@ -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: valet71.sock }', + '# '.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 }', + '# '.ISOLATED_PHP_VERSION.'=72'.PHP_EOL.'server { fastcgi_pass: valet72.sock }', $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 }', '73') + '# '.ISOLATED_PHP_VERSION.'=73'.PHP_EOL.'server { fastcgi_pass: valet73.sock }', + $site->replaceSockFile('# '.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 }', 'php@7.4') + '# '.ISOLATED_PHP_VERSION.'=php@7.4'.PHP_EOL.'server { fastcgi_pass: valet74.sock }', + $site->replaceSockFile('# '.ISOLATED_PHP_VERSION.'=72'.PHP_EOL.'server { fastcgi_pass: valet.sock }', 'php@7.4') ); }