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

Ensure base configuration is correct even if file already exists

This commit is contained in:
Matt Stauffer
2023-05-16 08:23:41 -04:00
parent a4145d77b5
commit 9ce0ca97b2
3 changed files with 31 additions and 5 deletions

View File

@@ -2,6 +2,8 @@
namespace Valet; namespace Valet;
use Exception;
class Configuration class Configuration
{ {
public function __construct(public Filesystem $files) public function __construct(public Filesystem $files)
@@ -18,7 +20,7 @@ public function install(): void
$this->createSitesDirectory(); $this->createSitesDirectory();
$this->createLogDirectory(); $this->createLogDirectory();
$this->createCertificatesDirectory(); $this->createCertificatesDirectory();
$this->writeBaseConfiguration(); $this->ensureBaseConfiguration();
$this->files->chown($this->path(), user()); $this->files->chown($this->path(), user());
} }
@@ -84,7 +86,23 @@ public function createCertificatesDirectory(): void
} }
/** /**
* Write the base, initial configuration for Valet. * Ensure the base initial configuration has been installed.
*/
public function ensureBaseConfiguration(): void
{
$this->writeBaseConfiguration();
if (empty($this->read()['tld'])) {
$this->updateKey('tld', 'test');
}
if (empty($this->read()['loopback'])) {
$this->updateKey('loopback', '127.0.0.1');
}
}
/**
* Write the base initial configuration for Valet.
*/ */
public function writeBaseConfiguration(): void public function writeBaseConfiguration(): void
{ {

View File

@@ -100,9 +100,7 @@ function (ConsoleCommandEvent $event) {
/** /**
* Upgrade helper: ensure the tld config exists and the loopback config exists. * Upgrade helper: ensure the tld config exists and the loopback config exists.
*/ */
if (empty(Configuration::read()['tld']) || empty(Configuration::read()['loopback'])) { Configuration::ensureBaseConfiguration();
Configuration::writeBaseConfiguration();
}
/** /**
* Get or set the TLD currently being used by Valet. * Get or set the TLD currently being used by Valet.

View File

@@ -132,4 +132,14 @@ public function test_trust_adds_the_sudoer_files()
resolve(Brew::class)->createSudoersEntry(); resolve(Brew::class)->createSudoersEntry();
resolve(Valet::class)->createSudoersEntry(); resolve(Valet::class)->createSudoersEntry();
} }
public function test_ensure_configuration_exists_writes_tld_and_loopback_if_empty()
{
$config = Mockery::mock(Configuration::class.'[writeBaseConfiguration,read,updateKey]', [new Filesystem]);
$config->shouldReceive('writeBaseConfiguration')->once();
$config->shouldReceive('read')->times(2)->andReturn([]);
$config->shouldReceive('updateKey')->with('tld', 'test');
$config->shouldReceive('updateKey')->with('loopback', '127.0.0.1');
$config->ensureBaseConfiguration();
}
} }