1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-04 16:10:08 +01:00

[wip] Valet run/refactor with brew opt (#9)

This commit is contained in:
Nasir Uddin Nobin
2022-03-24 05:15:45 +06:00
committed by GitHub
parent fbf96b9abc
commit 6d3d191f44
3 changed files with 95 additions and 152 deletions

View File

@@ -251,16 +251,7 @@ public function getParsedLinkedPhp()
$resolvedPath = $this->files->readLink(BREW_PREFIX.'/bin/php'); $resolvedPath = $this->files->readLink(BREW_PREFIX.'/bin/php');
/** return $this->parsePhpPath($resolvedPath);
* Typical homebrew path resolutions are like:
* "../Cellar/php@7.4/7.4.13/bin/php"
* or older styles:
* "../Cellar/php/7.4.9_2/bin/php
* "../Cellar/php55/bin/php.
*/
preg_match('~\w{3,}/(php)(@?\d\.?\d)?/(\d\.\d)?([_\d\.]*)?/?\w{3,}~', $resolvedPath, $matches);
return $matches;
} }
/** /**
@@ -289,82 +280,46 @@ public function linkedPhp()
return $this->supportedPhpVersions()->first( return $this->supportedPhpVersions()->first(
function ($version) use ($resolvedPhpVersion) { function ($version) use ($resolvedPhpVersion) {
$resolvedVersionNormalized = preg_replace('/[^\d]/', '', $resolvedPhpVersion); return $this->isPhpVersionsEqual($resolvedPhpVersion, $version);
$versionNormalized = preg_replace('/[^\d]/', '', $version);
return $resolvedVersionNormalized === $versionNormalized;
}, function () use ($resolvedPhpVersion) { }, function () use ($resolvedPhpVersion) {
throw new DomainException("Unable to determine linked PHP when parsing '$resolvedPhpVersion'"); throw new DomainException("Unable to determine linked PHP when parsing '$resolvedPhpVersion'");
}); });
} }
/**
* Get PHP executable path for a given PHP Version.
*
* @param string|null $phpVersion
* @param bool $skipCache
* @return string
*/
public function whichPhp($phpVersion = null, $skipCache = false)
{
if (! $phpVersion) {
return BREW_PREFIX.'/bin/php';
}
$versionInteger = preg_replace('~[^\d]~', '', $phpVersion);
$symlinkedValetPhpPath = BREW_PREFIX."/bin/valetphp{$versionInteger}";
// If the symlinked Valet PHP path exists, then we can use that
if (! $skipCache && $this->files->isLink($symlinkedValetPhpPath)) {
$phpExecutablePath = $this->files->readLink($symlinkedValetPhpPath);
// Still make sure that the version of the executable exists
if ($this->files->exists($phpExecutablePath)) {
return $phpExecutablePath;
}
}
// If the symlinked Valet PHP path doesn't exist, then we need to look for the correct executable path
// Create a symlink to the Valet PHP version, so next time Valet won't have to look for the executable path
if ($phpExecutablePath = $this->getPhpExecutablePath($phpVersion)) {
$this->files->symlinkAsUser($phpExecutablePath, $symlinkedValetPhpPath);
}
return $phpExecutablePath ?: BREW_PREFIX.'/bin/php';
}
/** /**
* Extract PHP executable path from PHP Version. * Extract PHP executable path from PHP Version.
* *
* @param string $phpVersion * @param string $phpVersion
* @return string * @return string
*/ */
public function getPhpExecutablePath($phpVersion) public function getPhpExecutablePath($phpVersion = null)
{ {
$phpExecutablePath = null; if (! $phpVersion) {
return BREW_PREFIX.'/bin/php';
}
$cellar = $this->cli->runAsUser("brew --cellar $phpVersion"); // Example output: `/opt/homebrew/Cellar/php@8.0` // Check the default `/opt/homebrew/opt/php@8.1/bin/php` location first
$details = json_decode($this->cli->runAsUser("brew info --json $phpVersion"), true); if ($this->files->exists(BREW_PREFIX."/opt/{$phpVersion}/bin/php")) {
$phpDirectory = data_get($details, '0.linked_keg'); return BREW_PREFIX."/opt/{$phpVersion}/bin/php";
}
if (is_null($phpDirectory) && $installed = data_get($details, '0.installed')) { // Check the `/opt/homebrew/opt/php71/bin/php` location for older installations
$phpDirectory = data_get(collect($installed)->where('installed_as_dependency', false)->last(), 'version'); $phpVersion = str_replace(['@', '.'], '' , $phpVersion); // php@8.1 to php81
if ($this->files->exists(BREW_PREFIX."/opt/{$phpVersion}/bin/php")) {
return BREW_PREFIX."/opt/{$phpVersion}/bin/php";
}
if (is_null($phpDirectory)) { // Check if the default PHP is the version we are looking for
$phpDirectory = data_get(collect($installed)->last(), 'version'); if ($this->files->isLink(BREW_PREFIX."/opt/php")) {
$resolvedPath = $this->files->readLink(BREW_PREFIX."/opt/php");
$matches = $this->parsePhpPath($resolvedPath);
$resolvedPhpVersion = $matches[3] ?: $matches[2];
if ($this->isPhpVersionsEqual($resolvedPhpVersion, $phpVersion)) {
return BREW_PREFIX."/opt/php/bin/php";
} }
} }
if (isset($phpDirectory) && $this->files->exists(trim($cellar).'/'.$phpDirectory.'/bin/php')) { return BREW_PREFIX.'/bin/php';
$phpExecutablePath = trim($cellar).'/'.$phpDirectory.'/bin/php';
}
// When PHP Version is installed directly though shivammathur/homebrew-php, it usually stores the binaries in this directory
if (is_null($phpExecutablePath) && $this->files->exists(BREW_PREFIX."/opt/{$phpVersion}/bin/php")) {
$phpExecutablePath = BREW_PREFIX."/opt/{$phpVersion}/bin/php";
}
return $phpExecutablePath;
} }
/** /**
@@ -532,4 +487,41 @@ function ($exitCode, $errorOutput) {
} }
); );
} }
/**
* Parse homebrew PHP Path
*
* @param string $resolvedPath
*
* @return mixed
*/
public function parsePhpPath($resolvedPath)
{
/**
* Typical homebrew path resolutions are like:
* "../Cellar/php@7.4/7.4.13/bin/php"
* or older styles:
* "../Cellar/php/7.4.9_2/bin/php
* "../Cellar/php55/bin/php.
*/
preg_match('~\w{3,}/(php)(@?\d\.?\d)?/(\d\.\d)?([_\d\.]*)?/?\w{3,}~', $resolvedPath, $matches);
return $matches;
}
/**
* Check if two PHP versions are equal
*
* @param string $resolvedPhpVersion
* @param string $version
*
* @return bool
*/
public function isPhpVersionsEqual($resolvedPhpVersion, $version)
{
$resolvedVersionNormalized = preg_replace('/[^\d]/', '', $resolvedPhpVersion);
$versionNormalized = preg_replace('/[^\d]/', '', $version);
return $resolvedVersionNormalized === $versionNormalized;
}
} }

View File

@@ -564,7 +564,7 @@
/** /**
* Get the PHP executable path for a site. * Get the PHP executable path for a site.
*/ */
$app->command('which-php [site] [--skip-cache]', function ($site, $skipCache) { $app->command('which-php [site]', function ($site) {
$host = Site::host($site ?: getcwd()).'.'.Configuration::read()['tld']; $host = Site::host($site ?: getcwd()).'.'.Configuration::read()['tld'];
$phpVersion = Site::customPhpVersion($host); $phpVersion = Site::customPhpVersion($host);
@@ -577,10 +577,9 @@
$phpVersion = $phpVersion ? PhpFpm::normalizePhpVersion($phpVersion) : null; $phpVersion = $phpVersion ? PhpFpm::normalizePhpVersion($phpVersion) : null;
return output(Brew::whichPhp($phpVersion, $skipCache)); return output(Brew::getPhpExecutablePath($phpVersion));
})->descriptions('Get the PHP executable path for a given site', [ })->descriptions('Get the PHP executable path for a given site', [
'site' => 'The site to get the PHP executable path for', 'site' => 'The site to get the PHP executable path for',
'--skip-cache' => 'Force re-check of the PHP executable path',
]); ]);
/** /**

View File

@@ -395,44 +395,51 @@ public function test_restart_linked_php_will_pass_through_linked_php_formula_to_
$brewMock->restartLinkedPhp(); $brewMock->restartLinkedPhp();
} }
public function test_it_can_get_php_binary_path_from_php_version_and_create_symlink() public function test_it_can_get_php_binary_path_from_php_version()
{ {
// When there is no symlinked Valet PHP exists // Check the default `/opt/homebrew/opt/php@8.1/bin/php` location first
$brewMock = Mockery::mock(Brew::class, [ $brewMock = Mockery::mock(Brew::class, [
Mockery::mock(CommandLine::class), Mockery::mock(CommandLine::class),
$files = Mockery::mock(Filesystem::class), $files = Mockery::mock(Filesystem::class),
])->makePartial(); ])->makePartial();
$files->shouldReceive('isLink')->once()->with(BREW_PREFIX.'/bin/valetphp74')->andReturn(false); $files->shouldReceive('exists')->once()->with(BREW_PREFIX.'/opt/php@7.4/bin/php')->andReturn(true);
$files->shouldReceive('symlinkAsUser')->once()->withArgs([BREW_PREFIX.'/Cellar/php@7.4/7.4.6/bin/php', BREW_PREFIX.'/bin/valetphp74']); $files->shouldNotReceive('exists')->with(BREW_PREFIX.'/opt/php@74/bin/php');
$brewMock->shouldReceive('getPhpExecutablePath')->once()->with('php@7.4')->andReturn(BREW_PREFIX.'/Cellar/php@7.4/7.4.6/bin/php'); $this->assertEquals(BREW_PREFIX.'/opt/php@7.4/bin/php', $brewMock->getPhpExecutablePath('php@7.4'));
$this->assertEquals(BREW_PREFIX.'/Cellar/php@7.4/7.4.6/bin/php', $brewMock->whichPhp('php@7.4')); // Check the `/opt/homebrew/opt/php71/bin/php` location for older installations
// When a symlinked Valet PHP exists
$brewMock = Mockery::mock(Brew::class, [ $brewMock = Mockery::mock(Brew::class, [
Mockery::mock(CommandLine::class), Mockery::mock(CommandLine::class),
$files = Mockery::mock(Filesystem::class), $files = Mockery::mock(Filesystem::class),
])->makePartial(); ])->makePartial();
$brewMock->shouldNotHaveReceived('getPhpExecutablePath'); $files->shouldReceive('exists')->once()->with(BREW_PREFIX.'/opt/php@7.4/bin/php')->andReturn(false);
$files->shouldReceive('isLink')->once()->with(BREW_PREFIX.'/bin/valetphp81')->andReturn(true); $files->shouldReceive('exists')->with(BREW_PREFIX.'/opt/php74/bin/php')->andReturn(true);
$files->shouldReceive('readLink')->once()->with(BREW_PREFIX.'/bin/valetphp81')->andReturn(BREW_PREFIX.'/Cellar/php@8.1/8.1.5/bin/php'); $this->assertEquals(BREW_PREFIX.'/opt/php74/bin/php', $brewMock->getPhpExecutablePath('php@7.4'));
$files->shouldReceive('exists')->once()->with(BREW_PREFIX.'/Cellar/php@8.1/8.1.5/bin/php')->andReturn(true);
$files->shouldNotHaveReceived('symlinkAsUser');
$this->assertEquals(BREW_PREFIX.'/Cellar/php@8.1/8.1.5/bin/php', $brewMock->whichPhp('php@8.1')); // When the default PHP is the version we are looking for
// Check with $skipCache enabled
$brewMock = Mockery::mock(Brew::class, [ $brewMock = Mockery::mock(Brew::class, [
Mockery::mock(CommandLine::class), Mockery::mock(CommandLine::class),
$files = Mockery::mock(Filesystem::class), $files = Mockery::mock(Filesystem::class),
])->makePartial(); ])->makePartial();
$brewMock->shouldReceive('getPhpExecutablePath')->once()->with('php@7.4')->andReturn(BREW_PREFIX.'/Cellar/php@7.4/7.4.6/bin/php'); $files->shouldReceive('exists')->once()->with(BREW_PREFIX.'/opt/php@7.4/bin/php')->andReturn(false);
$files->shouldReceive('symlinkAsUser')->once()->withArgs([BREW_PREFIX.'/Cellar/php@7.4/7.4.6/bin/php', BREW_PREFIX.'/bin/valetphp74']); $files->shouldReceive('exists')->with(BREW_PREFIX.'/opt/php74/bin/php')->andReturn(false);
$files->shouldReceive('isLink')->with(BREW_PREFIX."/opt/php")->andReturn(true);
$files->shouldReceive('readLink')->with(BREW_PREFIX."/opt/php")->andReturn('../Cellar/php@7.4/7.4.13/bin/php');
$this->assertEquals(BREW_PREFIX."/opt/php/bin/php", $brewMock->getPhpExecutablePath('php@7.4'));
$this->assertEquals(BREW_PREFIX.'/Cellar/php@7.4/7.4.6/bin/php', $brewMock->whichPhp('php@7.4', true)); // When the default PHP is not the version we are looking for
$brewMock = Mockery::mock(Brew::class, [
Mockery::mock(CommandLine::class),
$files = Mockery::mock(Filesystem::class),
])->makePartial();
$files->shouldReceive('exists')->once()->with(BREW_PREFIX.'/opt/php@7.4/bin/php')->andReturn(false);
$files->shouldReceive('exists')->with(BREW_PREFIX.'/opt/php74/bin/php')->andReturn(false);
$files->shouldReceive('isLink')->with(BREW_PREFIX."/opt/php")->andReturn(true);
$files->shouldReceive('readLink')->with(BREW_PREFIX."/opt/php")->andReturn('../Cellar/php@8.1/8.1.13/bin/php');
$this->assertEquals(BREW_PREFIX."/bin/php", $brewMock->getPhpExecutablePath('php@7.4')); // Could not find a version, so retuned the default binary
// When no PHP Version is provided // When no PHP Version is provided
$brewMock = Mockery::mock(Brew::class, [ $brewMock = Mockery::mock(Brew::class, [
@@ -440,72 +447,17 @@ public function test_it_can_get_php_binary_path_from_php_version_and_create_syml
Mockery::mock(Filesystem::class), Mockery::mock(Filesystem::class),
])->makePartial(); ])->makePartial();
$this->assertEquals(BREW_PREFIX.'/bin/php', $brewMock->whichPhp(null)); $this->assertEquals(BREW_PREFIX.'/bin/php', $brewMock->getPhpExecutablePath(null));
} }
public function test_it_can_get_php_binary_path_from_php_version() public function test_it_can_compare_two_php_versions()
{ {
// When brew info has `linked_keg` parameter $this->assertTrue(resolve(Brew::class)->isPhpVersionsEqual('php71', 'php@7.1'));
$brewMock = Mockery::mock(Brew::class, [ $this->assertTrue(resolve(Brew::class)->isPhpVersionsEqual('php71', 'php@71'));
$cli = Mockery::mock(CommandLine::class), $this->assertTrue(resolve(Brew::class)->isPhpVersionsEqual('php71', '71'));
$files = Mockery::mock(Filesystem::class),
])->makePartial();
$cli->shouldReceive('runAsUser')->once()->with('brew --cellar php@7.4')->andReturn(BREW_PREFIX.'/Cellar/php@7.4'); $this->assertFalse(resolve(Brew::class)->isPhpVersionsEqual('php71', 'php@70'));
$cli->shouldReceive('runAsUser')->once()->with('brew info --json php@7.4')->andReturn('[{"linked_keg":"7.4.6","installed":[{"version":"7.4.5"}]}]'); $this->assertFalse(resolve(Brew::class)->isPhpVersionsEqual('php71', '72'));
$files->shouldReceive('exists')->once()->with(BREW_PREFIX.'/Cellar/php@7.4/7.4.6/bin/php')->andReturn(true);
$this->assertEquals(BREW_PREFIX.'/Cellar/php@7.4/7.4.6/bin/php', $brewMock->getPhpExecutablePath('php@7.4'));
// When brew info doesn't have `linked_keg`
$brewMock = Mockery::mock(Brew::class, [
$cli = Mockery::mock(CommandLine::class),
$files = Mockery::mock(Filesystem::class),
])->makePartial();
$cli->shouldReceive('runAsUser')->once()->with('brew --cellar php@8.0')->andReturn(BREW_PREFIX.'/Cellar/php@8.0');
$cli->shouldReceive('runAsUser')->once()->with('brew info --json php@8.0')->andReturn('[{"installed":[{"version":"8.0.5"}]}]');
$files->shouldReceive('exists')->once()->with(BREW_PREFIX.'/Cellar/php@8.0/8.0.5/bin/php')->andReturn(true);
$this->assertEquals(BREW_PREFIX.'/Cellar/php@8.0/8.0.5/bin/php', $brewMock->getPhpExecutablePath('php@8.0'));
// When brew info has a version that was manually installed
$brewMock = Mockery::mock(Brew::class, [
$cli = Mockery::mock(CommandLine::class),
$files = Mockery::mock(Filesystem::class),
])->makePartial();
$cli->shouldReceive('runAsUser')->once()->with('brew --cellar php@8.1')->andReturn(BREW_PREFIX.'/Cellar/php@8.1');
$cli->shouldReceive('runAsUser')->once()->with('brew info --json php@8.1')
->andReturn('[{"installed":[{"version":"8.1.1", "installed_as_dependency":true}, {"version":"8.1.2", "installed_as_dependency":false}]}]');
$files->shouldReceive('exists')->once()->with(BREW_PREFIX.'/Cellar/php@8.1/8.1.2/bin/php')->andReturn(true);
$this->assertEquals(BREW_PREFIX.'/Cellar/php@8.1/8.1.2/bin/php', $brewMock->getPhpExecutablePath('php@8.1'));
// When brew info has no version that was installed manually, it should pick the last PHP version
$brewMock = Mockery::mock(Brew::class, [
$cli = Mockery::mock(CommandLine::class),
$files = Mockery::mock(Filesystem::class),
])->makePartial();
$cli->shouldReceive('runAsUser')->once()->with('brew --cellar php@7.4')->andReturn(BREW_PREFIX.'/Cellar/php@7.4');
$cli->shouldReceive('runAsUser')->once()->with('brew info --json php@7.4')
->andReturn('[{"installed":[{"version":"7.4.1", "installed_as_dependency":false}, {"version":"7.4.3", "installed_as_dependency":false}]}]');
$files->shouldReceive('exists')->once()->with(BREW_PREFIX.'/Cellar/php@7.4/7.4.3/bin/php')->andReturn(true);
$this->assertEquals(BREW_PREFIX.'/Cellar/php@7.4/7.4.3/bin/php', $brewMock->getPhpExecutablePath('php@7.4'));
// When user has installed directly though `shivammathur/homebrew-php`
$brewMock = Mockery::mock(Brew::class, [
$cli = Mockery::mock(CommandLine::class),
$files = Mockery::mock(Filesystem::class),
])->makePartial();
$cli->shouldReceive('runAsUser')->once()->with('brew --cellar php@7.4')->andReturn(BREW_PREFIX.'/Cellar/php@7.4');
$cli->shouldReceive('runAsUser')->once()->with('brew info --json php@7.4')->andReturn(false);
$files->shouldReceive('exists')->once()->with(BREW_PREFIX.'/opt/php@7.4/bin/php')->andReturn(true);
$this->assertEquals(BREW_PREFIX.'/opt/php@7.4/bin/php', $brewMock->getPhpExecutablePath('php@7.4'));
} }
/** /**