Skip to content

Commit

Permalink
Install older PHP versions via alternate tap
Browse files Browse the repository at this point in the history
Fixes #1407

Homebrew's support policy now aggressively disables older versions earlier than it did previously.
That is why we tap the `shivammathur/php` repository.

But since Homebrew also now "keeps" those old formulae but marks them as disabled, when Valet runs `brew install [email protected]` it fails because Homebrew just aborts due to its flags in the outdated formula.
(They mark it as "disabled" and/or "keg_only :versioned_formula", but don't actually delete the formula from their repository.)
To override that we must use `brew install shivammathur/php/[email protected]` internally.

Therefore, we need to maintain a list of which PHP Versions we wish to specifically prefix with the `shivammathur/php` tap in order to bypass Homebrew's flags.

**ANNUAL MAINTENANCE**
1. This PR adds the array of `LIMITED_PHP_VERSIONS` which we will have to update annually when new PHP versions are retired as others are released.
2. We should also annually update the `LATEST_PHP_VERSION` string.
  • Loading branch information
drbyte committed May 6, 2023
1 parent 9c974cd commit 615d61d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
30 changes: 27 additions & 3 deletions cli/Valet/Brew.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class Brew
{
// This is the array of PHP versions that Valet will attempt to install/configure when requested
const SUPPORTED_PHP_VERSIONS = [
'php',
'[email protected]',
Expand All @@ -19,9 +20,22 @@ class Brew
'[email protected]',
];

const BREW_DISABLE_AUTO_CLEANUP = 'HOMEBREW_NO_INSTALL_CLEANUP=1';
// Update this LATEST and the following LIMITED array when PHP versions are released or retired
// We specify a numbered version here even though Homebrew links its generic 'php' alias to it
const LATEST_PHP_VERSION = '[email protected]';

const LATEST_PHP_VERSION = '[email protected]';
// These are the PHP versions that should be installed via the shivammathur/php tap because
// Homebrew officially no longer bottles them or they're marked disabled in their formula
// Cue: Homebrew reports "[email protected] has been disabled because it is a versioned formula"
const LIMITED_PHP_VERSIONS = [
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
];

const BREW_DISABLE_AUTO_CLEANUP = 'HOMEBREW_NO_INSTALL_CLEANUP=1';

public function __construct(public CommandLine $cli, public Filesystem $files)
{
Expand Down Expand Up @@ -72,6 +86,14 @@ public function supportedPhpVersions(): Collection
return collect(static::SUPPORTED_PHP_VERSIONS);
}

/**
* Get a list of disabled/limited PHP versions.
*/
public function limitedPhpVersions(): Collection
{
return collect(static::LIMITED_PHP_VERSIONS);
}

/**
* Get a list of installed PHP formulae.
*/
Expand Down Expand Up @@ -135,7 +157,9 @@ public function installOrFail(string $formula, array $options = [], array $taps
}

output('<info>['.$formula.'] is not installed, installing it now via Brew...</info> 🍻');
if ($formula !== 'php' && starts_with($formula, 'php') && preg_replace('/[^\d]/', '', $formula) < '73') {

if ($this->limitedPhpVersions()->contains($formula)) {
$formula = 'shivammathur/php/' . $formula;
warning('Note: older PHP versions may take 10+ minutes to compile from source. Please wait ...');
}

Expand Down
16 changes: 16 additions & 0 deletions tests/BrewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,22 @@ public function test_linked_php_throws_exception_if_unsupported_php_version_is_l
resolve(Brew::class)->linkedPhp();
}

public function test_outdated_php_versions_use_the_alternate_tap()
{
$brewMock = Mockery::mock(Brew::class, [
$cli = Mockery::mock(CommandLine::class),
Mockery::mock(Filesystem::class),
])->makePartial();

$brewMock->shouldReceive('limitedPhpVersions')->andReturn(collect([
'[email protected]',
]));

$cli->shouldReceive('runAsUser')->once()->with(Brew::BREW_DISABLE_AUTO_CLEANUP.' brew install shivammathur/php/[email protected]', Mockery::type('Closure'));

$brewMock->installOrFail('[email protected]');
}

public function test_install_or_fail_will_install_brew_formulae()
{
$cli = Mockery::mock(CommandLine::class);
Expand Down

0 comments on commit 615d61d

Please sign in to comment.