1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-06 16:50:09 +01:00

wip - refactor with valetphprc version (#10)

This commit is contained in:
Nasir Uddin Nobin
2022-03-30 03:48:03 +06:00
committed by GitHub
parent 0202f7773a
commit d198d7739c
4 changed files with 81 additions and 11 deletions

View File

@@ -2,8 +2,8 @@
namespace Valet; namespace Valet;
use PhpFpm;
use DomainException; use DomainException;
use PhpFpm;
class Brew class Brew
{ {

View File

@@ -3,6 +3,7 @@
namespace Valet; namespace Valet;
use DomainException; use DomainException;
use PhpFpm;
class Site class Site
{ {
@@ -1108,4 +1109,21 @@ public function replaceSockFile($siteConf, $phpVersion)
return '# '.ISOLATED_PHP_VERSION.'='.$phpVersion.PHP_EOL.$siteConf; 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)));
}
}
}
} }

View File

@@ -506,18 +506,19 @@
*/ */
$app->command('use [phpVersion] [--force]', function ($phpVersion, $force) { $app->command('use [phpVersion] [--force]', function ($phpVersion, $force) {
if (! $phpVersion) { if (! $phpVersion) {
$path = getcwd().'/.valetphprc'; $site = basename(getcwd());
$linkedVersion = Brew::linkedPhp(); $linkedVersion = Brew::linkedPhp();
if (! file_exists($path)) { $phpVersion = Site::phpRcVersion($site);
if (! $phpVersion) {
return info("Valet is using {$linkedVersion}."); return info("Valet is using {$linkedVersion}.");
} }
$phpVersion = trim(file_get_contents($path)); if ($linkedVersion == $phpVersion && ! $force) {
info("Found '{$path}' specifying version: {$phpVersion}");
if ($linkedVersion == $phpVersion) {
return info("Valet is already using {$linkedVersion}."); return info("Valet is already using {$linkedVersion}.");
} }
info("Found '{$site}' specifying version: {$phpVersion}");
} }
PhpFpm::useVersion($phpVersion, $force); PhpFpm::useVersion($phpVersion, $force);
@@ -533,6 +534,11 @@
$site = basename(getcwd()); $site = basename(getcwd());
} }
if (! $phpVersion) {
$phpVersion = Site::phpRcVersion($site);
info("Found '{$site}' specifying version: {$phpVersion}");
}
PhpFpm::isolateDirectory($site, $phpVersion); PhpFpm::isolateDirectory($site, $phpVersion);
})->descriptions('Change the version of PHP used by Valet to serve the current working directory', [ })->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', 'phpVersion' => 'The PHP version you want to use; e.g php@8.1',
@@ -569,10 +575,7 @@
$phpVersion = Site::customPhpVersion($host); $phpVersion = Site::customPhpVersion($host);
if (! $phpVersion) { if (! $phpVersion) {
$path = getcwd().'/.valetphprc'; $phpVersion = Site::phpRcVersion($site ?: basename(getcwd()));
if (file_exists($path)) {
$phpVersion = trim(file_get_contents($path));
}
} }
return output(Brew::getPhpExecutablePath($phpVersion)); return output(Brew::getPhpExecutablePath($phpVersion));

View File

@@ -819,6 +819,55 @@ public function test_it_returns_secured_sites()
$this->assertSame(['helloworld.tld'], $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 class CommandLineFake extends CommandLine