diff --git a/cli/Valet/Brew.php b/cli/Valet/Brew.php index 53e9d32..828eabe 100644 --- a/cli/Valet/Brew.php +++ b/cli/Valet/Brew.php @@ -2,8 +2,8 @@ namespace Valet; -use PhpFpm; use DomainException; +use PhpFpm; class Brew { diff --git a/cli/Valet/Site.php b/cli/Valet/Site.php index c1a3152..7922c85 100644 --- a/cli/Valet/Site.php +++ b/cli/Valet/Site.php @@ -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))); + } + } + } } diff --git a/cli/valet.php b/cli/valet.php index 70002ee..ecb5fd9 100755 --- a/cli/valet.php +++ b/cli/valet.php @@ -506,18 +506,19 @@ */ $app->command('use [phpVersion] [--force]', function ($phpVersion, $force) { if (! $phpVersion) { - $path = getcwd().'/.valetphprc'; + $site = basename(getcwd()); $linkedVersion = Brew::linkedPhp(); - if (! file_exists($path)) { + $phpVersion = Site::phpRcVersion($site); + + if (! $phpVersion) { return info("Valet is using {$linkedVersion}."); } - $phpVersion = trim(file_get_contents($path)); - info("Found '{$path}' specifying version: {$phpVersion}"); - - if ($linkedVersion == $phpVersion) { + if ($linkedVersion == $phpVersion && ! $force) { return info("Valet is already using {$linkedVersion}."); } + + info("Found '{$site}' specifying version: {$phpVersion}"); } PhpFpm::useVersion($phpVersion, $force); @@ -533,6 +534,11 @@ $site = basename(getcwd()); } + if (! $phpVersion) { + $phpVersion = Site::phpRcVersion($site); + info("Found '{$site}' specifying version: {$phpVersion}"); + } + PhpFpm::isolateDirectory($site, $phpVersion); })->descriptions('Change the version of PHP used by Valet to serve the current working directory', [ 'phpVersion' => 'The PHP version you want to use; e.g php@8.1', @@ -569,10 +575,7 @@ $phpVersion = Site::customPhpVersion($host); if (! $phpVersion) { - $path = getcwd().'/.valetphprc'; - if (file_exists($path)) { - $phpVersion = trim(file_get_contents($path)); - } + $phpVersion = Site::phpRcVersion($site ?: basename(getcwd())); } return output(Brew::getPhpExecutablePath($phpVersion)); diff --git a/tests/SiteTest.php b/tests/SiteTest.php index abe8360..9b5d606 100644 --- a/tests/SiteTest.php +++ b/tests/SiteTest.php @@ -819,6 +819,55 @@ public function test_it_returns_secured_sites() $this->assertSame(['helloworld.tld'], $sites); } + + public function test_it_can_read_php_rc_version() + { + $config = Mockery::mock(Configuration::class); + $files = Mockery::mock(Filesystem::class); + + swap(Configuration::class, $config); + swap(Filesystem::class, $files); + + $siteMock = Mockery::mock(Site::class, [ + resolve(Configuration::class), + resolve(CommandLine::class), + resolve(Filesystem::class) + ])->makePartial(); + + swap(Site::class, $siteMock); + + $config->shouldReceive('read') + ->andReturn(['tld' => 'test', 'loopback' => VALET_LOOPBACK, 'paths' => []]); + + $siteMock->shouldReceive('parked') + ->andReturn(collect([ + 'site1' => [ + 'site' => 'site1', + 'secured' => '', + 'url' => 'http://site1.test', + 'path' => '/Users/name/code/site1', + ], + ])); + + $siteMock->shouldReceive('links')->andReturn(collect([ + 'site2' => [ + 'site' => 'site2', + 'secured' => 'X', + 'url' => 'http://site2.test', + 'path' => '/Users/name/some-other-directory/site2', + ], + ])); + + $files->shouldReceive('exists')->with('/Users/name/code/site1/.valetphprc')->andReturn(true); + $files->shouldReceive('get')->with('/Users/name/code/site1/.valetphprc')->andReturn('php@8.1'); + + $files->shouldReceive('exists')->with('/Users/name/some-other-directory/site2/.valetphprc')->andReturn(true); + $files->shouldReceive('get')->with('/Users/name/some-other-directory/site2/.valetphprc')->andReturn('php@8.0'); + + $this->assertEquals('php@8.1', $siteMock->phpRcVersion('site1')); + $this->assertEquals('php@8.0', $siteMock->phpRcVersion('site2')); + $this->assertEquals(null, $siteMock->phpRcVersion('site3')); // Site doesn't exists + } } class CommandLineFake extends CommandLine