From ddcdca64ad2091e4c228a7d2218d6d0ba6d8812c Mon Sep 17 00:00:00 2001 From: yCodeTech Date: Tue, 24 Mar 2026 03:47:07 +0000 Subject: [PATCH 1/5] refactor: upgrade checks using a new `isUpgraded` method. - Added a new `Upgrader::isUpgraded` method to determine if the named upgrade has already been completed. - Replaced the direct config access in the Upgrader methods `shouldUpgradeSymbolicLinks` and `shouldUpgradeNginxSitePhpPortOverrides` to use the new `isUpgraded` method. - Refactored `Configuration::updateKey` method to use Laravel's `Arr::set` method to set a value within an array instead of the direct array access. It enables usage of dot notation to easily access deeply nested array keys. --- cli/Valet/Configuration.php | 10 ++++++++-- cli/Valet/Upgrader.php | 17 +++++++++++++---- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/cli/Valet/Configuration.php b/cli/Valet/Configuration.php index 6492809e4..bcddbee55 100644 --- a/cli/Valet/Configuration.php +++ b/cli/Valet/Configuration.php @@ -431,7 +431,10 @@ public function read(): array { } /** - * Get an item from the configuration file using "dot" notation. + * Get an item from the configuration file. + * + * @uses Arr:get Gets a value from a nested array using "dot" notation. + * @link https://laravel.com/docs/13.x/helpers#method-array-get * * @param string|int|null $key * @param mixed $default @@ -445,6 +448,9 @@ public function get($key, $default = null) { /** * Update a specific key in the configuration file. * + * @uses Arr:set Sets a value within a nested array using "dot" notation. + * @link https://laravel.com/docs/13.x/helpers#method-array-set + * * @param string $key * @param mixed $value * @@ -452,7 +458,7 @@ public function get($key, $default = null) { */ public function updateKey(string $key, $value): array { return tap($this->read(), function (&$config) use ($key, $value) { - $config[$key] = $value; + Arr::set($config, $key, $value); $this->write($config); }); diff --git a/cli/Valet/Upgrader.php b/cli/Valet/Upgrader.php index 1e7e90671..5f8bca0af 100644 --- a/cli/Valet/Upgrader.php +++ b/cli/Valet/Upgrader.php @@ -96,9 +96,7 @@ private function upgradeSymbolicLinks() { * 2. The sites directory is not empty (`$isDirEmpty` is `false`). */ private function shouldUpgradeSymbolicLinks() { - // Get the value of the "symlinks_upgraded" key from the config. - // If the key doesn't exist, it will return false. - $symlinksUpgraded = $this->config->get("symlinks_upgraded", false); + $symlinksUpgraded = $this->isUpgraded('symlinks_upgraded'); // Check if the sites directory is empty. $isDirEmpty = $this->files->isDirEmpty($this->site->sitesPath()); @@ -302,6 +300,17 @@ private function upgradeNginxSitePhpPortOverrides() { * @return bool */ private function shouldUpgradeNginxSitePhpPortOverrides() { - return !$this->config->get('php_port_overrides_upgraded', false); + return !$this->isUpgraded('php_port_overrides_upgraded'); + } + + /** + * Determine if a named upgrade has already been completed. + * + * @param string $upgradeId + * + * @return bool + */ + private function isUpgraded(string $upgradeId): bool { + return $this->config->get($upgradeId, false); } } From a0f0769ce2c0376bfddbebeae23c5b6ae7d685e0 Mon Sep 17 00:00:00 2001 From: yCodeTech Date: Tue, 24 Mar 2026 05:18:33 +0000 Subject: [PATCH 2/5] refactor: mark an upgrade as complete using new `markAsUpgraded` method. - Added a new `Upgrader::markAsUpgraded` method to mark an upgrade as completed, and add a key into Valet's config. - Replaced the direct config `updateKey` in the Upgrader methods `upgradeSymbolicLinks` and `upgradeNginxSitePhpPortOverrides` to use the new `markAsUpgraded` method. --- cli/Valet/Upgrader.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/cli/Valet/Upgrader.php b/cli/Valet/Upgrader.php index 5f8bca0af..081bd54b7 100644 --- a/cli/Valet/Upgrader.php +++ b/cli/Valet/Upgrader.php @@ -77,9 +77,8 @@ private function upgradeSymbolicLinks() { // Convert all junction links to symbolic links. $this->files->convertJunctionsToSymlinks($this->site->sitesPath()); - // Add a new key to the config file to indicate that the symlinks have been upgraded. - // This will prevent the upgrade from running again, since it is a one-time upgrade. - $this->config->updateKey("symlinks_upgraded", true); + // Mark this upgrade as complete so it will not run again. + $this->markAsUpgraded('symlinks_upgraded'); info("Successfully upgraded junction links to symbolic links."); } @@ -261,7 +260,7 @@ private function upgradeNginxSitePhpPortOverrides() { // If the Nginx config directory doesn't exist, skip and mark it as upgraded to prevent // this from running again. if (!$this->files->exists($this->site->nginxPath())) { - $this->config->updateKey('php_port_overrides_upgraded', true); + $this->markAsUpgraded('php_port_overrides_upgraded'); return; } @@ -291,7 +290,7 @@ private function upgradeNginxSitePhpPortOverrides() { info("Upgraded {$upgraded} Nginx site config(s) to the new PHP port override format."); } - $this->config->updateKey('php_port_overrides_upgraded', true); + $this->markAsUpgraded('php_port_overrides_upgraded'); } /** @@ -313,4 +312,13 @@ private function shouldUpgradeNginxSitePhpPortOverrides() { private function isUpgraded(string $upgradeId): bool { return $this->config->get($upgradeId, false); } + + /** + * Mark a named upgrade as completed in the configuration. + * + * @param string $upgradeId + */ + private function markAsUpgraded(string $upgradeId): void { + $this->config->updateKey($upgradeId, true); + } } From 73381bc925970b80ed62998b2a55289cd64461c8 Mon Sep 17 00:00:00 2001 From: yCodeTech Date: Wed, 25 Mar 2026 05:34:45 +0000 Subject: [PATCH 3/5] refactor: simplify the should be upgraded check for PHP port override - Replaced the `shouldUpgradeNginxSitePhpPortOverrides` method call with a direct call to `isUpgraded` method for improved clarity and conciseness. The former method was only a light wrapper around `isUpgraded` method anyway. - Removed the now unused `shouldUpgradeNginxSitePhpPortOverrides` method. --- cli/Valet/Upgrader.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/cli/Valet/Upgrader.php b/cli/Valet/Upgrader.php index 081bd54b7..4f059f3ae 100644 --- a/cli/Valet/Upgrader.php +++ b/cli/Valet/Upgrader.php @@ -253,7 +253,7 @@ public function fixOldSampleValetDriver(): void { */ private function upgradeNginxSitePhpPortOverrides() { // If the PHP port definitions have already been upgraded, skip. - if (!$this->shouldUpgradeNginxSitePhpPortOverrides()) { + if ($this->isUpgraded('nginx_site_php_port_overrides')) { return; } @@ -293,15 +293,6 @@ private function upgradeNginxSitePhpPortOverrides() { $this->markAsUpgraded('php_port_overrides_upgraded'); } - /** - * Determine whether Nginx site PHP port overrides should be upgraded. - * - * @return bool - */ - private function shouldUpgradeNginxSitePhpPortOverrides() { - return !$this->isUpgraded('php_port_overrides_upgraded'); - } - /** * Determine if a named upgrade has already been completed. * From bd3ea3cefce7cf8200e98b76f92fb896acd665f6 Mon Sep 17 00:00:00 2001 From: yCodeTech Date: Wed, 25 Mar 2026 06:27:17 +0000 Subject: [PATCH 4/5] refactor: restructure the upgrade keys to a standardised format. The new format allows for multiple one-time upgrade keys to be stored in an organised manner under the `upgrades` array. This stops valet's config from being polluted with many top-level `*_upgraded` config keys when new one-time upgrades are added in the future. - Changed the `symlinks_upgraded` key to `symlinks`. - Changed the `php_port_overrides_upgraded` key to `nginx_site_php_port_overrides`. - Changed the config `get` and `updateKey` calls in `isUpgraded` and `markAsUpgraded` methods to use dot notation to get/update the specified upgrade key from the new top-level `upgrades`. --- cli/Valet/Upgrader.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cli/Valet/Upgrader.php b/cli/Valet/Upgrader.php index 4f059f3ae..b61715b03 100644 --- a/cli/Valet/Upgrader.php +++ b/cli/Valet/Upgrader.php @@ -78,7 +78,7 @@ private function upgradeSymbolicLinks() { $this->files->convertJunctionsToSymlinks($this->site->sitesPath()); // Mark this upgrade as complete so it will not run again. - $this->markAsUpgraded('symlinks_upgraded'); + $this->markAsUpgraded('symlinks'); info("Successfully upgraded junction links to symbolic links."); } @@ -95,7 +95,7 @@ private function upgradeSymbolicLinks() { * 2. The sites directory is not empty (`$isDirEmpty` is `false`). */ private function shouldUpgradeSymbolicLinks() { - $symlinksUpgraded = $this->isUpgraded('symlinks_upgraded'); + $symlinksUpgraded = $this->isUpgraded('symlinks'); // Check if the sites directory is empty. $isDirEmpty = $this->files->isDirEmpty($this->site->sitesPath()); @@ -260,7 +260,7 @@ private function upgradeNginxSitePhpPortOverrides() { // If the Nginx config directory doesn't exist, skip and mark it as upgraded to prevent // this from running again. if (!$this->files->exists($this->site->nginxPath())) { - $this->markAsUpgraded('php_port_overrides_upgraded'); + $this->markAsUpgraded('nginx_site_php_port_overrides'); return; } @@ -290,7 +290,7 @@ private function upgradeNginxSitePhpPortOverrides() { info("Upgraded {$upgraded} Nginx site config(s) to the new PHP port override format."); } - $this->markAsUpgraded('php_port_overrides_upgraded'); + $this->markAsUpgraded('nginx_site_php_port_overrides'); } /** @@ -301,7 +301,7 @@ private function upgradeNginxSitePhpPortOverrides() { * @return bool */ private function isUpgraded(string $upgradeId): bool { - return $this->config->get($upgradeId, false); + return $this->config->get("upgrades.{$upgradeId}", false); } /** @@ -310,6 +310,6 @@ private function isUpgraded(string $upgradeId): bool { * @param string $upgradeId */ private function markAsUpgraded(string $upgradeId): void { - $this->config->updateKey($upgradeId, true); + $this->config->updateKey("upgrades.{$upgradeId}", true); } } From 9e466f3d60812c862ccc4249253a85d4f162a0f8 Mon Sep 17 00:00:00 2001 From: yCodeTech Date: Wed, 25 Mar 2026 15:07:13 +0000 Subject: [PATCH 5/5] refactor: migrate legacy symlinks upgrade key to new format - Added `Upgrader::migrateSymlinksUpgradeKey` method to handle the transition from `symlinks_upgraded` to `symlinks`. 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. - Added `Configuration::removeKey` method to remove a specified key from the config. --- cli/Valet/Configuration.php | 18 ++++++++++++++++++ cli/Valet/Upgrader.php | 27 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/cli/Valet/Configuration.php b/cli/Valet/Configuration.php index bcddbee55..7905bde5e 100644 --- a/cli/Valet/Configuration.php +++ b/cli/Valet/Configuration.php @@ -464,6 +464,24 @@ public function updateKey(string $key, $value): array { }); } + /** + * Remove a specific key from the configuration file. + * + * @uses Arr:forget Removes a value within a nested array using "dot" notation. + * @link https://laravel.com/docs/13.x/helpers#method-array-forget + * + * @param string $key + * + * @return array + */ + public function removeKey(string $key): array { + return tap($this->read(), function (&$config) use ($key) { + Arr::forget($config, $key); + + $this->write($config); + }); + } + /** * Write the given configuration to disk. * diff --git a/cli/Valet/Upgrader.php b/cli/Valet/Upgrader.php index b61715b03..3ffd7b3bc 100644 --- a/cli/Valet/Upgrader.php +++ b/cli/Valet/Upgrader.php @@ -72,6 +72,9 @@ private function pruneSymbolicLinks() { * This is a one-time upgrade that will be run when Valet is first installed. */ private function upgradeSymbolicLinks() { + // Migrate legacy symlinks upgrade key to the new format. + $this->migrateSymlinksUpgradeKey(); + if ($this->shouldUpgradeSymbolicLinks()) { info("Upgrading your linked sites from the old junction links to symbolic links..."); // Convert all junction links to symbolic links. @@ -312,4 +315,28 @@ private function isUpgraded(string $upgradeId): bool { private function markAsUpgraded(string $upgradeId): void { $this->config->updateKey("upgrades.{$upgradeId}", true); } + + /** + * Migrate legacy `symlinks_upgraded` upgrade key to the new `symlinks` key under + * the `upgrades` array in the configuration (`upgrades.symlinks`). + * + * If the legacy key exists but the new key doesn't, it marks the upgrade as completed, + * and removes legacy key from configuration. + */ + private function migrateSymlinksUpgradeKey(): void { + $legacyUpgradeKey = 'symlinks_upgraded'; + $newUpgradeKey = 'symlinks'; + + $hasNewKey = $this->config->get("upgrades.{$newUpgradeKey}", false); + $hasLegacyKey = $this->config->get($legacyUpgradeKey, false); + + // If the legacy key exists AND the new key doesn't, + // mark the new upgrade key as upgraded. + if ($hasLegacyKey && !$hasNewKey) { + $this->markAsUpgraded($newUpgradeKey); + } + + // Remove the legacy upgrade key to clean up the config. + $this->config->removeKey($legacyUpgradeKey); + } }