From 2a605a0b9f0892f0481e6960e93620d9b3650e91 Mon Sep 17 00:00:00 2001 From: Neodork Date: Fri, 7 Feb 2020 12:54:09 +0100 Subject: [PATCH 1/4] Fix switching php versions not listening to error states --- cli/Valet/PhpFpm.php | 73 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/cli/Valet/PhpFpm.php b/cli/Valet/PhpFpm.php index 70f0b94e..c2270e97 100644 --- a/cli/Valet/PhpFpm.php +++ b/cli/Valet/PhpFpm.php @@ -175,20 +175,85 @@ public function switchTo($version) $this->brew->ensureInstalled(self::SUPPORTED_PHP_FORMULAE[$version]); } - info("[php@$currentVersion] Unlinking"); - output($this->cli->runAsUser('brew unlink ' . self::SUPPORTED_PHP_FORMULAE[$currentVersion])); + // Unlink the current PHP version. + if (!$this->unlinkPhp($currentVersion)) { + return; + } + // Relink libjpeg info('[libjpeg] Relinking'); $this->cli->passthru('sudo ln -fs /usr/local/Cellar/jpeg/8d/lib/libjpeg.8.dylib /usr/local/opt/jpeg/lib/libjpeg.8.dylib'); - info("[php@$version] Linking"); - output($this->cli->runAsUser('brew link ' . self::SUPPORTED_PHP_FORMULAE[$version] . ' --force --overwrite')); + if (!$this->linkPHP($version, $currentVersion)) { + return; + } $this->stop(); $this->install(); info("Valet is now using " . self::SUPPORTED_PHP_FORMULAE[$version]); } + /** + * Link a PHP version to be used as binary. + * + * @param $version + * + * @return bool + */ + private function linkPhp($version, $currentVersion = null) + { + $isLinked = true; + info("[php@$version] Linking"); + $output = $this->cli->runAsUser('brew link ' . self::SUPPORTED_PHP_FORMULAE[$version] . ' --force --overwrite', function () use (&$isLinked) { + $isLinked = false; + }); + + // If the output is about how many symlinks were created. + // Sanitize the second half to prevent users from being confused. + // So the only ouput would be: + // Linking /usr/local/Cellar/valet-php@7.3/7.3.8... 25 symlinks created + // Without the directions to create exports pointing towards the binaries. + if (strpos($output, 'symlinks created')) { + $output = substr($output, 0, strpos($output, 'symlinks created') + 8); + } + output($output); + + if ($isLinked === false) { + warning("Could not link PHP version!" . PHP_EOL . + "There appears to be an issue with your PHP $version installation!" . PHP_EOL . + "See the output above for more information." . PHP_EOL); + } + + if ($currentVersion !== null && $isLinked === false) { + info("Linking back to previous version to prevent broken installation!"); + $this->linkPhp($currentVersion); + } + + return $isLinked; + } + + /** + * Unlink a PHP version, removing the binary symlink. + * + * @param $version + * @return bool + */ + private function unlinkPhp($version) + { + $isUnlinked = true; + info("[php@$version] Unlinking"); + output($this->cli->runAsUser('brew unlink ' . self::SUPPORTED_PHP_FORMULAE[$version], function () use (&$isUnlinked) { + $isUnlinked = false; + })); + if ($isUnlinked === false) { + warning("Could not unlink PHP version!" . PHP_EOL . + "There appears to be an issue with your PHP $version installation!" . PHP_EOL . + "See the output above for more information."); + } + + return $isUnlinked; + } + /** * @deprecated Deprecated in favor of Pecl#installExtension(); * From a4c7427b4b02a65f15529310babf5f2f511043de Mon Sep 17 00:00:00 2001 From: Lou van der Laarse Date: Fri, 7 Feb 2020 16:12:00 +0100 Subject: [PATCH 2/4] Fix typo in comment Co-Authored-By: Sander <30903591+SanderAtom@users.noreply.github.com> --- cli/Valet/PhpFpm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/Valet/PhpFpm.php b/cli/Valet/PhpFpm.php index c2270e97..8e13f7ed 100644 --- a/cli/Valet/PhpFpm.php +++ b/cli/Valet/PhpFpm.php @@ -210,7 +210,7 @@ private function linkPhp($version, $currentVersion = null) // If the output is about how many symlinks were created. // Sanitize the second half to prevent users from being confused. - // So the only ouput would be: + // So the only output would be: // Linking /usr/local/Cellar/valet-php@7.3/7.3.8... 25 symlinks created // Without the directions to create exports pointing towards the binaries. if (strpos($output, 'symlinks created')) { From 799ecd505d59d696322a1da286e27ac28771a12e Mon Sep 17 00:00:00 2001 From: Lou van der Laarse Date: Fri, 7 Feb 2020 16:12:09 +0100 Subject: [PATCH 3/4] Fix typo in comment Co-Authored-By: Sander <30903591+SanderAtom@users.noreply.github.com> --- cli/Valet/PhpFpm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/Valet/PhpFpm.php b/cli/Valet/PhpFpm.php index 8e13f7ed..90474e31 100644 --- a/cli/Valet/PhpFpm.php +++ b/cli/Valet/PhpFpm.php @@ -208,7 +208,7 @@ private function linkPhp($version, $currentVersion = null) $isLinked = false; }); - // If the output is about how many symlinks were created. + // The output is about how many symlinks were created. // Sanitize the second half to prevent users from being confused. // So the only output would be: // Linking /usr/local/Cellar/valet-php@7.3/7.3.8... 25 symlinks created From 130b89d08807ac603d0035db800197da2e382a4d Mon Sep 17 00:00:00 2001 From: Lou van der Laarse Date: Fri, 7 Feb 2020 16:12:27 +0100 Subject: [PATCH 4/4] Fix typo in PHP doc Co-Authored-By: Sander <30903591+SanderAtom@users.noreply.github.com> --- cli/Valet/PhpFpm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/Valet/PhpFpm.php b/cli/Valet/PhpFpm.php index 90474e31..6bd53e0f 100644 --- a/cli/Valet/PhpFpm.php +++ b/cli/Valet/PhpFpm.php @@ -197,7 +197,7 @@ public function switchTo($version) * Link a PHP version to be used as binary. * * @param $version - * + * @param $currentVersion * @return bool */ private function linkPhp($version, $currentVersion = null)