Skip to content

Commit

Permalink
Rework PHP version resolution
Browse files Browse the repository at this point in the history
Last year's Homebrew's PHP packaging changes altered their version numbering strategy.
Now that their changes appear to have stabilized, Valet also needs some updates to match.

The `linkedPhp()` function was parsing the symlinked directory name for where the php binaries are stored, but that numbering strategy has morphed over time.

This PR changes the logic to accommodate the most common directory naming strategies I can find, including those of older installs.
I've included some examples of these names in code comments for future reference since finding a variety of them can be complicated.
  • Loading branch information
drbyte authored and mattstauffer committed Jan 9, 2019
1 parent 7dd06db commit 83b1b2c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
25 changes: 18 additions & 7 deletions cli/Valet/Brew.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,28 @@ function stopService($services)
function linkedPhp()
{
if (! $this->files->isLink('/usr/local/bin/php')) {
throw new DomainException("Unable to determine linked PHP.");
throw new DomainException("Homebrew PHP appears not to be linked.");
}

$resolvedPath = $this->files->readLink('/usr/local/bin/php');

return $this->supportedPhpVersions()->first(function ($version) use ($resolvedPath) {
$resolvedPathNormalized= preg_replace('/([@|\.])/', '', $resolvedPath);
$versionNormalized = preg_replace('/([@|\.])/', '', $version);
return strpos($resolvedPathNormalized, "/$versionNormalized/") !== false;
}, function () {
throw new DomainException("Unable to determine linked PHP.");
/**
* Typical homebrew path resolutions are like:
* "../Cellar/[email protected]/7.2.13/bin/php"
* or older styles:
* "../Cellar/php/7.2.9_2/bin/php
* "../Cellar/php55/bin/php
*/
preg_match('~\w{3,}/(php)(@?\d\.?\d)?/(\d\.\d)?([_\d\.]*)?/?\w{3,}~', $resolvedPath, $matches);
$resolvedPhpVersion = $matches[3] ?: $matches[2];

return $this->supportedPhpVersions()->first(
function ($version) use ($resolvedPhpVersion) {
$resolvedVersionNormalized = preg_replace('/[^\d]/', '', $resolvedPhpVersion);
$versionNormalized = preg_replace('/[^\d]/', '', $version);
return $resolvedVersionNormalized === $versionNormalized;
}, function () use ($resolvedPhpVersion) {
throw new DomainException("Unable to determine linked PHP when parsing '$resolvedPhpVersion'");
});
}

Expand Down
24 changes: 21 additions & 3 deletions tests/BrewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,27 @@ public function test_linked_php_returns_linked_php_formula_name()
{
$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('isLink')->once()->with('/usr/local/bin/php')->andReturn(true);
$files->shouldReceive('readLink')->once()->with('/usr/local/bin/php')->andReturn('/test/path/php71/test');
$files->shouldReceive('readLink')->once()->with('/usr/local/bin/php')->andReturn('/test/path/php/7.3.0/test');
swap(Filesystem::class, $files);
$this->assertSame('[email protected]', resolve(Brew::class)->linkedPhp());
$this->assertSame('[email protected]', resolve(Brew::class)->linkedPhp());

$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('isLink')->once()->with('/usr/local/bin/php')->andReturn(true);
$files->shouldReceive('readLink')->once()->with('/usr/local/bin/php')->andReturn('/test/path/[email protected]/7.2.13/test');
swap(Filesystem::class, $files);
$this->assertSame('[email protected]', resolve(Brew::class)->linkedPhp());

$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('isLink')->once()->with('/usr/local/bin/php')->andReturn(true);
$files->shouldReceive('readLink')->once()->with('/usr/local/bin/php')->andReturn('/test/path/php/7.2.9_2/test');
swap(Filesystem::class, $files);
$this->assertSame('[email protected]', resolve(Brew::class)->linkedPhp());

$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('isLink')->once()->with('/usr/local/bin/php')->andReturn(true);
$files->shouldReceive('readLink')->once()->with('/usr/local/bin/php')->andReturn('/test/path/php72/7.2.9_2/test');
swap(Filesystem::class, $files);
$this->assertSame('[email protected]', resolve(Brew::class)->linkedPhp());

$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('isLink')->once()->with('/usr/local/bin/php')->andReturn(true);
Expand Down Expand Up @@ -205,7 +223,7 @@ public function test_linked_php_throws_exception_if_unsupported_php_version_is_l
{
$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('isLink')->once()->with('/usr/local/bin/php')->andReturn(true);
$files->shouldReceive('readLink')->once()->with('/usr/local/bin/php')->andReturn('/test/path/php42/test');
$files->shouldReceive('readLink')->once()->with('/usr/local/bin/php')->andReturn('/test/path/php/5.4.14/test');
swap(Filesystem::class, $files);
resolve(Brew::class)->linkedPhp();
}
Expand Down

0 comments on commit 83b1b2c

Please sign in to comment.