From 116c7a7b8cf35a0c346bf757f590d06fb0b30682 Mon Sep 17 00:00:00 2001 From: Chris Brown Date: Sat, 4 Jan 2020 13:36:20 -0500 Subject: [PATCH] Detect running php when no version constraint specified with use command Fixes #756 Previously if `php` was installed as just `php` (the default Homebrew alias), Valet would not detect *which* PHP version was actually installed, and therefore `valet use` might do incorrect or unnecessary installations/links/etc. NOTE: This does NOT "convert" existing `php` alias to a numbered version. It merely accepts it as-is, but notes its version in an attempt to avoid extra installations. *NOTE: Specifally tested with PHP 7.4 and 7.3. No promises about old 5.6, etc aliases.* --- cli/Valet/Brew.php | 9 +++++++++ cli/Valet/PhpFpm.php | 45 +++++++++++++++++++++++++++++++++----------- cli/valet.php | 7 ++++--- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/cli/Valet/Brew.php b/cli/Valet/Brew.php index 95334f1..9f9c762 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 d809ac0..9bff1b2 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 965d53b..c47213e 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', ]); /**