Skip to content

Commit 5c7b2d3

Browse files
Add support for .valetrc (#1347)
* Add .valetrc support * Apply fixes from StyleCI * wip Co-authored-by: StyleCI Bot <[email protected]>
1 parent 6a100fc commit 5c7b2d3

File tree

10 files changed

+71
-51
lines changed

10 files changed

+71
-51
lines changed

Diff for: cli/Valet/Drivers/Specific/Magento2ValetDriver.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Magento2ValetDriver extends ValetDriver
1313
*
1414
* @param string|null
1515
*/
16-
/*?string*/ private $mageMode = null;
16+
private $mageMode = null;
1717

1818
/**
1919
* Determine if the driver serves the request.

Diff for: cli/Valet/Site.php

+31-5
Original file line numberDiff line numberDiff line change
@@ -1021,16 +1021,42 @@ public function replaceSockFile(string $siteConf, string $phpVersion): string
10211021
}
10221022

10231023
/**
1024-
* Get PHP version from .valetphprc for a site.
1024+
* Get configuration items defined in .valetrc for a site.
10251025
*/
1026-
public function phpRcVersion(string $site): ?string
1026+
public function valetRc(string $siteName): array
10271027
{
1028-
if ($site = $this->parked()->merge($this->links())->where('site', $site)->first()) {
1029-
$path = data_get($site, 'path').'/.valetphprc';
1028+
if ($site = $this->parked()->merge($this->links())->where('site', $siteName)->first()) {
1029+
$path = data_get($site, 'path').'/.valetrc';
10301030

10311031
if ($this->files->exists($path)) {
1032-
return PhpFpm::normalizePhpVersion(trim($this->files->get($path)));
1032+
return collect(explode(PHP_EOL, trim($this->files->get($path))))->filter(function ($line) {
1033+
return str_contains($line, '=');
1034+
})->mapWithKeys(function ($item, $index) {
1035+
[$key, $value] = explode('=', $item);
1036+
1037+
return [strtolower($key) => $value];
1038+
})->all();
1039+
}
1040+
}
1041+
1042+
return [];
1043+
}
1044+
1045+
/**
1046+
* Get PHP version from .valetrc or .valetphprc for a site.
1047+
*/
1048+
public function phpRcVersion(string $siteName): ?string
1049+
{
1050+
if ($site = $this->parked()->merge($this->links())->where('site', $siteName)->first()) {
1051+
$oldPath = data_get($site, 'path').'/.valetphprc';
1052+
1053+
if ($this->files->exists($oldPath)) {
1054+
return PhpFpm::normalizePhpVersion(trim($this->files->get($oldPath)));
10331055
}
1056+
1057+
$valetRc = $this->valetRc($siteName);
1058+
1059+
return PhpFpm::normalizePhpVersion(data_get($valetRc, 'php'));
10341060
}
10351061

10361062
return null;

Diff for: cli/app.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ function (ConsoleCommandEvent $event) {
572572
$linkedVersion = Brew::linkedPhp();
573573

574574
if ($phpVersion = Site::phpRcVersion($site)) {
575-
info("Found '{$site}/.valetphprc' specifying version: {$phpVersion}");
575+
info("Found '{$site}/.valetrc' or '{$site}/.valetphprc' specifying version: {$phpVersion}");
576576
} else {
577577
$domain = $site.'.'.data_get(Configuration::read(), 'tld');
578578
if ($phpVersion = PhpFpm::normalizePhpVersion(Site::customPhpVersion($domain))) {
@@ -604,7 +604,7 @@ function (ConsoleCommandEvent $event) {
604604

605605
if (is_null($phpVersion)) {
606606
if ($phpVersion = Site::phpRcVersion($site)) {
607-
info("Found '{$site}/.valetphprc' specifying version: {$phpVersion}");
607+
info("Found '{$site}/.valetrc' or '{$site}/.valetphprc' specifying version: {$phpVersion}");
608608
} else {
609609
info(PHP_EOL.'Please provide a version number. E.g.:');
610610
info('valet isolate [email protected]');

Diff for: composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
]
3636
},
3737
"require": {
38-
"php": "^8.0",
38+
"php": "^7.1|^8.0",
3939
"illuminate/collections": "^8.0|^9.0|^10.0",
4040
"illuminate/container": "~5.1|^6.0|^7.0|^8.0|^9.0|^10.0",
4141
"mnapoli/silly": "^1.0",

Diff for: tests/SiteTest.php

+27-42
Original file line numberDiff line numberDiff line change
@@ -900,54 +900,39 @@ public function test_it_returns_secured_sites()
900900
$this->assertSame(['helloworld.tld'], $sites);
901901
}
902902

903-
public function test_it_can_read_php_rc_version()
903+
public function test_it_can_read_valet_rc_files()
904904
{
905-
$config = Mockery::mock(Configuration::class);
906-
$files = Mockery::mock(Filesystem::class);
907-
908-
swap(Configuration::class, $config);
909-
swap(Filesystem::class, $files);
910-
911-
$siteMock = Mockery::mock(Site::class, [
912-
resolve(Brew::class),
913-
resolve(Configuration::class),
914-
resolve(CommandLine::class),
915-
resolve(Filesystem::class),
916-
])->makePartial();
917-
918-
swap(Site::class, $siteMock);
919-
920-
$config->shouldReceive('read')
921-
->andReturn(['tld' => 'test', 'loopback' => VALET_LOOPBACK, 'paths' => []]);
905+
resolve(Configuration::class)->addPath(__DIR__.'/fixtures/Parked/Sites');
906+
$site = resolve(Site::class);
922907

923-
$siteMock->shouldReceive('parked')
924-
->andReturn(collect([
925-
'site1' => [
926-
'site' => 'site1',
927-
'secured' => '',
928-
'url' => 'http://site1.test',
929-
'path' => '/Users/name/code/site1',
930-
],
931-
]));
908+
$this->assertEquals([
909+
'item' => 'value',
910+
'php' => '[email protected]',
911+
'other_item' => 'othervalue',
912+
], $site->valetRc('site-w-valetrc-1'));
932913

933-
$siteMock->shouldReceive('links')->andReturn(collect([
934-
'site2' => [
935-
'site' => 'site2',
936-
'secured' => 'X',
937-
'url' => 'http://site2.test',
938-
'path' => '/Users/name/some-other-directory/site2',
939-
],
940-
]));
914+
$this->assertEquals([
915+
'php' => '[email protected]',
916+
], $site->valetRc('site-w-valetrc-2'));
941917

942-
$files->shouldReceive('exists')->with('/Users/name/code/site1/.valetphprc')->andReturn(true);
943-
$files->shouldReceive('get')->with('/Users/name/code/site1/.valetphprc')->andReturn('[email protected]');
918+
$this->assertEquals([
919+
'item' => 'value',
920+
'php' => '[email protected]',
921+
], $site->valetRc('site-w-valetrc-3'));
922+
}
944923

945-
$files->shouldReceive('exists')->with('/Users/name/some-other-directory/site2/.valetphprc')->andReturn(true);
946-
$files->shouldReceive('get')->with('/Users/name/some-other-directory/site2/.valetphprc')->andReturn('[email protected]');
924+
public function test_it_can_read_php_rc_version()
925+
{
926+
resolve(Configuration::class)->addPath(__DIR__.'/fixtures/Parked/Sites');
927+
$site = resolve(Site::class);
947928

948-
$this->assertEquals('[email protected]', $siteMock->phpRcVersion('site1'));
949-
$this->assertEquals('[email protected]', $siteMock->phpRcVersion('site2'));
950-
$this->assertEquals(null, $siteMock->phpRcVersion('site3')); // Site doesn't exists
929+
$this->assertEquals('[email protected]', $site->phpRcVersion('site-w-valetphprc-1'));
930+
$this->assertEquals('[email protected]', $site->phpRcVersion('site-w-valetphprc-2'));
931+
$this->assertEquals(null, $site->phpRcVersion('my-best-site'));
932+
$this->assertEquals(null, $site->phpRcVersion('non-existent-site'));
933+
$this->assertEquals('[email protected]', $site->phpRcVersion('site-w-valetrc-1'));
934+
$this->assertEquals('[email protected]', $site->phpRcVersion('site-w-valetrc-2'));
935+
$this->assertEquals('[email protected]', $site->phpRcVersion('site-w-valetrc-3'));
951936
}
952937
}
953938

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ITEM=value
2+
3+
# comment line
4+
OTHER_ITEM=othervalue
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ITEM=value
2+

0 commit comments

Comments
 (0)