diff --git a/cli/Valet/Brew.php b/cli/Valet/Brew.php index 95334f12a..9f9c76220 100644 --- a/cli/Valet/Brew.php +++ b/cli/Valet/Brew.php @@ -71,6 +71,15 @@ function supportedPhpVersions() return collect(static::SUPPORTED_PHP_VERSIONS); } + /** + * Get the aliased formula version from Homebrew + */ + function determineAliasedVersion($formula) + { + $details = json_decode($this->cli->runAsUser("brew info $formula --json")); + return $details[0]->aliases[0] ?: 'ERROR - NO BREW ALIAS FOUND'; + } + /** * Determine if a compatible nginx version is Homebrewed. * diff --git a/cli/Valet/PhpFpm.php b/cli/Valet/PhpFpm.php index d809ac067..9bff1b2a2 100644 --- a/cli/Valet/PhpFpm.php +++ b/cli/Valet/PhpFpm.php @@ -167,17 +167,7 @@ function stopRunning() */ function useVersion($version) { - // If passed php7.2 or php72 formats, convert to php@7.2 format: - $version = preg_replace('/(php)([0-9+])(?:.)?([0-9+])/i', '$1@$2.$3', $version); - - if (!$this->brew->supportedPhpVersions()->contains($version)) { - throw new DomainException( - sprintf( - 'Valet doesn\'t support PHP version: %s (try something like \'php7.2\' instead)', - $version - ) - ); - } + $version = $this->validateRequestedVersion($version); // Install the relevant formula if not already installed $this->brew->ensureInstalled($version); @@ -194,6 +184,39 @@ function useVersion($version) $this->install(); + return $version === 'php' ? $this->brew->determineAliasedVersion($version) : $version; + } + + /** + * Validate the requested version to be sure we can support it. + * + * @param $version + * @return string + */ + function validateRequestedVersion($version) + { + // If passed php7.2 or php72 formats, normalize to php@7.2 format: + $version = preg_replace('/(php)([0-9+])(?:.)?([0-9+])/i', '$1@$2.$3', $version); + + if ($version === 'php') { + if (strpos($this->brew->determineAliasedVersion($version), '@')) { + return $version; + } + + if ($this->brew->hasInstalledPhp()) { + throw new DomainException('Brew is already using PHP '.PHP_VERSION.' as \'php\' in Homebrew. To use another version, please specify. eg: php@7.3'); + } + } + + if (!$this->brew->supportedPhpVersions()->contains($version)) { + throw new DomainException( + sprintf( + 'Valet doesn\'t support PHP version: %s (try something like \'php@7.3\' instead)', + $version + ) + ); + } + return $version; } } diff --git a/cli/valet.php b/cli/valet.php index 965d53b70..c47213e6c 100755 --- a/cli/valet.php +++ b/cli/valet.php @@ -384,15 +384,16 @@ * Allow the user to change the version of php valet uses */ $app->command('use phpVersion', function ($phpVersion) { - PhpFpm::stopRunning(); + PhpFpm::validateRequestedVersion($phpVersion); + PhpFpm::stopRunning(); $newVersion = PhpFpm::useVersion($phpVersion); Nginx::restart(); - info(sprintf('Valet is now using %s.', $newVersion)); + info(sprintf('Valet is now using %s.', $newVersion) . PHP_EOL); info('Note that you might need to run composer global update if your PHP version change affects the dependencies of global packages required by Composer.'); })->descriptions('Change the version of PHP used by valet', [ - 'phpVersion' => 'The PHP version you want to use, e.g php@7.2', + 'phpVersion' => 'The PHP version you want to use, e.g php@7.3', ]); /**