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

Add support for .valetrc (#1347)

* Add .valetrc support

* Apply fixes from StyleCI

* wip

Co-authored-by: StyleCI Bot <bot@styleci.io>
This commit is contained in:
Matt Stauffer
2023-01-18 20:44:51 -05:00
committed by GitHub
parent 6a100fc648
commit 5c7b2d313a
10 changed files with 74 additions and 54 deletions

View File

@@ -13,7 +13,7 @@ class Magento2ValetDriver extends ValetDriver
*
* @param string|null
*/
/*?string*/ private $mageMode = null;
private $mageMode = null;
/**
* Determine if the driver serves the request.

View File

@@ -1021,18 +1021,44 @@ public function replaceSockFile(string $siteConf, string $phpVersion): string
}
/**
* Get PHP version from .valetphprc for a site.
* Get configuration items defined in .valetrc for a site.
*/
public function phpRcVersion(string $site): ?string
public function valetRc(string $siteName): array
{
if ($site = $this->parked()->merge($this->links())->where('site', $site)->first()) {
$path = data_get($site, 'path').'/.valetphprc';
if ($site = $this->parked()->merge($this->links())->where('site', $siteName)->first()) {
$path = data_get($site, 'path').'/.valetrc';
if ($this->files->exists($path)) {
return PhpFpm::normalizePhpVersion(trim($this->files->get($path)));
return collect(explode(PHP_EOL, trim($this->files->get($path))))->filter(function ($line) {
return str_contains($line, '=');
})->mapWithKeys(function ($item, $index) {
[$key, $value] = explode('=', $item);
return [strtolower($key) => $value];
})->all();
}
}
return [];
}
/**
* Get PHP version from .valetrc or .valetphprc for a site.
*/
public function phpRcVersion(string $siteName): ?string
{
if ($site = $this->parked()->merge($this->links())->where('site', $siteName)->first()) {
$oldPath = data_get($site, 'path').'/.valetphprc';
if ($this->files->exists($oldPath)) {
return PhpFpm::normalizePhpVersion(trim($this->files->get($oldPath)));
}
$valetRc = $this->valetRc($siteName);
return PhpFpm::normalizePhpVersion(data_get($valetRc, 'php'));
}
return null;
}
}

View File

@@ -572,7 +572,7 @@ function (ConsoleCommandEvent $event) {
$linkedVersion = Brew::linkedPhp();
if ($phpVersion = Site::phpRcVersion($site)) {
info("Found '{$site}/.valetphprc' specifying version: {$phpVersion}");
info("Found '{$site}/.valetrc' or '{$site}/.valetphprc' specifying version: {$phpVersion}");
} else {
$domain = $site.'.'.data_get(Configuration::read(), 'tld');
if ($phpVersion = PhpFpm::normalizePhpVersion(Site::customPhpVersion($domain))) {
@@ -604,7 +604,7 @@ function (ConsoleCommandEvent $event) {
if (is_null($phpVersion)) {
if ($phpVersion = Site::phpRcVersion($site)) {
info("Found '{$site}/.valetphprc' specifying version: {$phpVersion}");
info("Found '{$site}/.valetrc' or '{$site}/.valetphprc' specifying version: {$phpVersion}");
} else {
info(PHP_EOL.'Please provide a version number. E.g.:');
info('valet isolate php@8.2');

View File

@@ -35,7 +35,7 @@
]
},
"require": {
"php": "^8.0",
"php": "^7.1|^8.0",
"illuminate/collections": "^8.0|^9.0|^10.0",
"illuminate/container": "~5.1|^6.0|^7.0|^8.0|^9.0|^10.0",
"mnapoli/silly": "^1.0",

View File

@@ -900,54 +900,39 @@ public function test_it_returns_secured_sites()
$this->assertSame(['helloworld.tld'], $sites);
}
public function test_it_can_read_valet_rc_files()
{
resolve(Configuration::class)->addPath(__DIR__.'/fixtures/Parked/Sites');
$site = resolve(Site::class);
$this->assertEquals([
'item' => 'value',
'php' => 'php@8.0',
'other_item' => 'othervalue',
], $site->valetRc('site-w-valetrc-1'));
$this->assertEquals([
'php' => 'php@8.1',
], $site->valetRc('site-w-valetrc-2'));
$this->assertEquals([
'item' => 'value',
'php' => 'php@8.2',
], $site->valetRc('site-w-valetrc-3'));
}
public function test_it_can_read_php_rc_version()
{
$config = Mockery::mock(Configuration::class);
$files = Mockery::mock(Filesystem::class);
resolve(Configuration::class)->addPath(__DIR__.'/fixtures/Parked/Sites');
$site = resolve(Site::class);
swap(Configuration::class, $config);
swap(Filesystem::class, $files);
$siteMock = Mockery::mock(Site::class, [
resolve(Brew::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
$this->assertEquals('php@8.1', $site->phpRcVersion('site-w-valetphprc-1'));
$this->assertEquals('php@8.0', $site->phpRcVersion('site-w-valetphprc-2'));
$this->assertEquals(null, $site->phpRcVersion('my-best-site'));
$this->assertEquals(null, $site->phpRcVersion('non-existent-site'));
$this->assertEquals('php@8.0', $site->phpRcVersion('site-w-valetrc-1'));
$this->assertEquals('php@8.1', $site->phpRcVersion('site-w-valetrc-2'));
$this->assertEquals('php@8.2', $site->phpRcVersion('site-w-valetrc-3'));
}
}

View File

@@ -0,0 +1 @@
php@8.1

View File

@@ -0,0 +1 @@
php@8.0

View File

@@ -0,0 +1,4 @@
ITEM=value
PHP=php@8.0
# comment line
OTHER_ITEM=othervalue

View File

@@ -0,0 +1 @@
PHP=php@8.1

View File

@@ -0,0 +1,2 @@
ITEM=value
PHP=php@8.2