Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cli/Valet/Brew.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
45 changes: 34 additions & 11 deletions cli/Valet/PhpFpm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}
7 changes: 4 additions & 3 deletions cli/valet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <comment>composer global update</comment> 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',
]);

/**
Expand Down