Skip to content

Commit bf606d5

Browse files
authored
refactor: restructure upgrade keys (#37)
This pull request refactors how upgrade state is tracked in the configuration, consolidating upgrade keys under a single `upgrades` array and improving configuration key management. The changes also add helper methods for manipulating nested configuration keys using Laravel's array helpers. Configuration management improvements: * Added `removeKey` method in `Configuration.php` to remove nested configuration values using Laravel's `Arr::forget` helper, allowing for dot notation. * Changed `updateKey` method in `Configuration.php` to use Laravel's `Arr::set` helper, allowing for dot notation and more flexible configuration updates. Upgrade state tracking refactor: * Consolidated upgrade keys (such as `symlinks_upgraded` and `php_port_overrides_upgraded`) under a new `upgrades` array in the configuration, with new names `symlinks` and `nginx_site_php_port_overrides`. * Added new helper methods `isUpgraded` and `markAsUpgraded` to access the new `upgrades` config format and improve upgrade tracking consistency. * Added a migration method (`migrateSymlinksUpgradeKey`) to move the legacy `symlinks_upgraded` key to the new `upgrades.symlinks` key and clean up the old key, ensuring backward compatibility and config cleanliness. This prevents the breaking change from being a _breaking change_. **Note**: The `symlinks_upgraded` is the only legacy key that needs to be migrated. The `php_port_overrides_upgraded` key wasn't released in a stable version, so it doesn't need migrating. * Replaced the `shouldUpgradeNginxSitePhpPortOverrides` and `shouldUpgradeSymbolicLinks` method calls with a direct call to `isUpgraded` method. * Removed the now unused `shouldUpgradeNginxSitePhpPortOverrides` and `shouldUpgradeSymbolicLinks` methods. These changes make the upgrade process more robust and future proof and the configuration file easier to maintain.
2 parents 4655eaa + 4797874 commit bf606d5

2 files changed

Lines changed: 75 additions & 35 deletions

File tree

cli/Valet/Configuration.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,10 @@ public function read(): array {
431431
}
432432

433433
/**
434-
* Get an item from the configuration file using "dot" notation.
434+
* Get an item from the configuration file.
435+
*
436+
* @uses Arr:get Gets a value from a nested array using "dot" notation.
437+
* @link https://laravel.com/docs/13.x/helpers#method-array-get
435438
*
436439
* @param string|int|null $key
437440
* @param mixed $default
@@ -445,14 +448,35 @@ public function get($key, $default = null) {
445448
/**
446449
* Update a specific key in the configuration file.
447450
*
451+
* @uses Arr:set Sets a value within a nested array using "dot" notation.
452+
* @link https://laravel.com/docs/13.x/helpers#method-array-set
453+
*
448454
* @param string $key
449455
* @param mixed $value
450456
*
451457
* @return array
452458
*/
453459
public function updateKey(string $key, $value): array {
454460
return tap($this->read(), function (&$config) use ($key, $value) {
455-
$config[$key] = $value;
461+
Arr::set($config, $key, $value);
462+
463+
$this->write($config);
464+
});
465+
}
466+
467+
/**
468+
* Remove a specific key from the configuration file.
469+
*
470+
* @uses Arr:forget Removes a value within a nested array using "dot" notation.
471+
* @link https://laravel.com/docs/13.x/helpers#method-array-forget
472+
*
473+
* @param string $key
474+
*
475+
* @return array
476+
*/
477+
public function removeKey(string $key): array {
478+
return tap($this->read(), function (&$config) use ($key) {
479+
Arr::forget($config, $key);
456480

457481
$this->write($config);
458482
});

cli/Valet/Upgrader.php

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -72,41 +72,22 @@ private function pruneSymbolicLinks() {
7272
* This is a one-time upgrade that will be run when Valet is first installed.
7373
*/
7474
private function upgradeSymbolicLinks() {
75-
if ($this->shouldUpgradeSymbolicLinks()) {
75+
// Migrate legacy symlinks upgrade key to the new format.
76+
$this->migrateSymlinksUpgradeKey();
77+
78+
// Check if the symlinks have NOT been upgraded yet AND the sites directory is NOT empty.
79+
if (!$this->isUpgraded('symlinks') && !$this->files->isDirEmpty($this->site->sitesPath())) {
7680
info("Upgrading your linked sites from the old junction links to symbolic links...");
7781
// Convert all junction links to symbolic links.
7882
$this->files->convertJunctionsToSymlinks($this->site->sitesPath());
7983

80-
// Add a new key to the config file to indicate that the symlinks have been upgraded.
81-
// This will prevent the upgrade from running again, since it is a one-time upgrade.
82-
$this->config->updateKey("symlinks_upgraded", true);
84+
// Mark this upgrade as complete so it will not run again.
85+
$this->markAsUpgraded('symlinks');
8386

8487
info("Successfully upgraded junction links to symbolic links.");
8588
}
8689
}
8790

88-
/**
89-
* Check if the symbolic links should be upgraded.
90-
*
91-
* @return bool Returns a boolean indicating whether the symlinks should be upgraded.
92-
*
93-
* The symlinks should be upgraded if:
94-
*
95-
* 1. The symlinks have not been upgraded yet (`$symlinksUpgraded` is `false`).
96-
* 2. The sites directory is not empty (`$isDirEmpty` is `false`).
97-
*/
98-
private function shouldUpgradeSymbolicLinks() {
99-
// Get the value of the "symlinks_upgraded" key from the config.
100-
// If the key doesn't exist, it will return false.
101-
$symlinksUpgraded = $this->config->get("symlinks_upgraded", false);
102-
103-
// Check if the sites directory is empty.
104-
$isDirEmpty = $this->files->isDirEmpty($this->site->sitesPath());
105-
106-
// Check if the symlinks have not been upgraded yet AND the sites directory is not empty.
107-
return !$symlinksUpgraded && !$isDirEmpty;
108-
}
109-
11091
/**
11192
* Lint the Nginx configuration files.
11293
*
@@ -226,7 +207,7 @@ private function upgradeDeprecatedNginxConfigDirectives() {
226207
* If the user has the old `SampleValetDriver` without the Valet namespace,
227208
* replace it with the new `SampleValetDriver` that uses the namespace.
228209
*/
229-
public function fixOldSampleValetDriver(): void {
210+
public function fixOldSampleValetDriver() {
230211
$samplePath = Valet::homePath() . '/Drivers/SampleValetDriver.php';
231212

232213
if ($this->files->exists($samplePath)) {
@@ -256,14 +237,14 @@ public function fixOldSampleValetDriver(): void {
256237
*/
257238
private function upgradeNginxSitePhpPortOverrides() {
258239
// If the PHP port definitions have already been upgraded, skip.
259-
if (!$this->shouldUpgradeNginxSitePhpPortOverrides()) {
240+
if ($this->isUpgraded('nginx_site_php_port_overrides')) {
260241
return;
261242
}
262243

263244
// If the Nginx config directory doesn't exist, skip and mark it as upgraded to prevent
264245
// this from running again.
265246
if (!$this->files->exists($this->site->nginxPath())) {
266-
$this->config->updateKey('php_port_overrides_upgraded', true);
247+
$this->markAsUpgraded('nginx_site_php_port_overrides');
267248
return;
268249
}
269250

@@ -293,15 +274,50 @@ private function upgradeNginxSitePhpPortOverrides() {
293274
info("Upgraded {$upgraded} Nginx site config(s) to the new PHP port override format.");
294275
}
295276

296-
$this->config->updateKey('php_port_overrides_upgraded', true);
277+
$this->markAsUpgraded('nginx_site_php_port_overrides');
297278
}
298279

299280
/**
300-
* Determine whether Nginx site PHP port overrides should be upgraded.
281+
* Determine if a named upgrade has already been completed.
282+
*
283+
* @param string $upgradeId
301284
*
302285
* @return bool
303286
*/
304-
private function shouldUpgradeNginxSitePhpPortOverrides() {
305-
return !$this->config->get('php_port_overrides_upgraded', false);
287+
private function isUpgraded(string $upgradeId): bool {
288+
return $this->config->get("upgrades.{$upgradeId}", false);
289+
}
290+
291+
/**
292+
* Mark a named upgrade as completed in the configuration.
293+
*
294+
* @param string $upgradeId
295+
*/
296+
private function markAsUpgraded(string $upgradeId) {
297+
$this->config->updateKey("upgrades.{$upgradeId}", true);
298+
}
299+
300+
/**
301+
* Migrate legacy `symlinks_upgraded` upgrade key to the new `symlinks` key under
302+
* the `upgrades` array in the configuration (`upgrades.symlinks`).
303+
*
304+
* If the legacy key exists but the new key doesn't, it marks the upgrade as completed,
305+
* and removes legacy key from configuration.
306+
*/
307+
private function migrateSymlinksUpgradeKey() {
308+
$legacyUpgradeKey = 'symlinks_upgraded';
309+
$newUpgradeKey = 'symlinks';
310+
311+
$hasNewKey = $this->isUpgraded($newUpgradeKey);
312+
$hasLegacyKey = $this->config->get($legacyUpgradeKey, false);
313+
314+
// If the legacy key exists AND the new key doesn't,
315+
// mark the new upgrade key as upgraded.
316+
if ($hasLegacyKey && !$hasNewKey) {
317+
$this->markAsUpgraded($newUpgradeKey);
318+
}
319+
320+
// Remove the legacy upgrade key to clean up the config.
321+
$this->config->removeKey($legacyUpgradeKey);
306322
}
307323
}

0 commit comments

Comments
 (0)