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

Self code review

This commit is contained in:
NasirNobin
2022-03-23 01:35:30 +06:00
parent 84f59b4890
commit ca2924daca
4 changed files with 47 additions and 40 deletions

View File

@@ -300,7 +300,7 @@ function ($version) use ($resolvedPhpVersion) {
/** /**
* Get PHP binary path for a given PHP Version * Get PHP executable path for a given PHP Version
* *
* @param string|null $phpVersion * @param string|null $phpVersion
* @param boolean $skipCache * @param boolean $skipCache
@@ -318,34 +318,35 @@ public function whichPhp($phpVersion = null, $skipCache = false)
// If the symlinked Valet PHP path exists, then we can use that // If the symlinked Valet PHP path exists, then we can use that
if (!$skipCache && $this->files->isLink($symlinkedValetPhpPath)) { if (!$skipCache && $this->files->isLink($symlinkedValetPhpPath)) {
$phpBinaryPath = $this->files->readLink($symlinkedValetPhpPath); $phpExecutablePath = $this->files->readLink($symlinkedValetPhpPath);
// Still make sure that the version of the binary exists // Still make sure that the version of the executable exists
if ($this->files->exists($phpBinaryPath)) { if ($this->files->exists($phpExecutablePath)) {
return $phpBinaryPath; return $phpExecutablePath;
} }
} }
// Create a symlink to the Valet PHP version, so next time valet won't have to look for the binary path // Create a symlink to the Valet PHP version, so next time valet won't have to look for the executable path
if ($phpBinaryPath = $this->getPhpBinaryPath($phpVersion)) { if ($phpExecutablePath = $this->getPhpExecutablePath($phpVersion)) {
$this->files->symlinkAsUser($phpBinaryPath, $symlinkedValetPhpPath); $this->files->symlinkAsUser($phpExecutablePath, $symlinkedValetPhpPath);
} }
return $phpBinaryPath ?: BREW_PREFIX.'/bin/php'; return $phpExecutablePath ?: BREW_PREFIX.'/bin/php';
} }
/** /**
* Get PHP binary path from PHP Version * Extract PHP executable path from PHP Version
* *
* @param string $phpVersion * @param string $phpVersion
*
* @return string * @return string
*/ */
public function getPhpBinaryPath($phpVersion) public function getPhpExecutablePath($phpVersion)
{ {
$phpBinaryPath = null; $phpExecutablePath = null;
// If the symlinked Valet PHP path doesn't exist, then we need to look for the correct binary path // If the symlinked Valet PHP path doesn't exist, then we need to look for the correct executable path
$cellar = $this->cli->runAsUser("brew --cellar $phpVersion"); // Example output: `/opt/homebrew/Cellar/php@8.0` $cellar = $this->cli->runAsUser("brew --cellar $phpVersion"); // Example output: `/opt/homebrew/Cellar/php@8.0`
$details = json_decode($this->cli->runAsUser("brew info --json $phpVersion"), true); $details = json_decode($this->cli->runAsUser("brew info --json $phpVersion"), true);
$phpDirectory = data_get($details, '0.linked_keg'); $phpDirectory = data_get($details, '0.linked_keg');
@@ -359,15 +360,15 @@ public function getPhpBinaryPath($phpVersion)
} }
if (isset($phpDirectory) && $this->files->exists(trim($cellar).'/'.$phpDirectory.'/bin/php')) { if (isset($phpDirectory) && $this->files->exists(trim($cellar).'/'.$phpDirectory.'/bin/php')) {
$phpBinaryPath = trim($cellar).'/'.$phpDirectory.'/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 // When PHP Version is installed directly though shivammathur/homebrew-php, it usually stores the binaries in this directory
if (is_null($phpBinaryPath) && $this->files->exists(BREW_PREFIX."/opt/{$phpVersion}/bin/php")) { if (is_null($phpExecutablePath) && $this->files->exists(BREW_PREFIX."/opt/{$phpVersion}/bin/php")) {
$phpBinaryPath = BREW_PREFIX."/opt/{$phpVersion}/bin/php"; $phpExecutablePath = BREW_PREFIX."/opt/{$phpVersion}/bin/php";
} }
return $phpBinaryPath; return $phpExecutablePath;
} }
/** /**

View File

@@ -562,7 +562,7 @@
})->descriptions('List all sites using isolated versions of PHP.'); })->descriptions('List all sites using isolated versions of PHP.');
/** /**
* Get PHP Birnary * Get PHP Executable
*/ */
$app->command('which-php [site] [--skip-cache]', function ($site, $skipCache) { $app->command('which-php [site] [--skip-cache]', function ($site, $skipCache) {
$host = Site::host($site ?: getcwd()).'.'.Configuration::read()['tld']; $host = Site::host($site ?: getcwd()).'.'.Configuration::read()['tld'];
@@ -578,24 +578,28 @@
$phpVersion = $phpVersion ? PhpFpm::normalizePhpVersion($phpVersion) : null; $phpVersion = $phpVersion ? PhpFpm::normalizePhpVersion($phpVersion) : null;
return output(Brew::whichPhp($phpVersion, $skipCache)); return output(Brew::whichPhp($phpVersion, $skipCache));
})->descriptions('Get the PHP binary path for a given site', [ })->descriptions('Get the PHP executable path for a given site', [
'site' => 'The site to get the PHP binary path for', 'site' => 'The site to get the PHP executable path for',
'--skip-cache' => 'Force a re-check of the PHP binary path', '--skip-cache' => 'Force re-check of the PHP executable path',
]); ]);
/** /**
* Proxy PHP Commands with correct version * Proxy PHP commands with isolated site's PHP executable
*/ */
$app->command('php', function () { $app->command('php [command]', function ($command) {
warning('It looks like you are running `cli/valet.php` directly; please use the `valet` script in the project root instead.'); warning('It looks like you are running `cli/valet.php` directly; please use the `valet` script in the project root instead.');
})->descriptions('Proxy PHP Commands with isolated PHP version'); })->descriptions("Proxy PHP commands with isolated site's PHP executable", [
'command' => "Command to run with isolated site's PHP executable",
]);
/** /**
* Proxy composer commands with the "php" binary on the isolated site * Proxy Composer commands with isolated site's PHP executable
*/ */
$app->command('composer', function () { $app->command('composer [command]', function ($command) {
warning('It looks like you are running `cli/valet.php` directly; please use the `valet` script in the project root instead.'); warning('It looks like you are running `cli/valet.php` directly; please use the `valet` script in the project root instead.');
})->descriptions('Proxy composer Commands with isolated PHP version'); })->descriptions("Proxy Composer commands with isolated site's PHP executable", [
'command' => "Composer command to run with isolated site's PHP executable",
]);
/** /**
* Tail log file. * Tail log file.

View File

@@ -395,7 +395,7 @@ 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_symblink() public function test_it_can_get_php_binary_path_from_php_version_and_create_symlink()
{ {
// Scenario when there is no symlinked Valet PHP exists // Scenario when there is no symlinked Valet PHP exists
$brewMock = Mockery::mock(Brew::class, [ $brewMock = Mockery::mock(Brew::class, [
@@ -405,7 +405,7 @@ public function test_it_can_get_php_binary_path_from_php_version_and_create_symb
$files->shouldReceive('isLink')->once()->with(BREW_PREFIX."/bin/valetphp74")->andReturn(false); $files->shouldReceive('isLink')->once()->with(BREW_PREFIX."/bin/valetphp74")->andReturn(false);
$files->shouldReceive('symlinkAsUser')->once()->withArgs([BREW_PREFIX."/Cellar/php@7.4/7.4.6/bin/php", BREW_PREFIX."/bin/valetphp74"]); $files->shouldReceive('symlinkAsUser')->once()->withArgs([BREW_PREFIX."/Cellar/php@7.4/7.4.6/bin/php", BREW_PREFIX."/bin/valetphp74"]);
$brewMock->shouldReceive('getPhpBinaryPath')->once()->with('php@7.4')->andReturn(BREW_PREFIX."/Cellar/php@7.4/7.4.6/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."/Cellar/php@7.4/7.4.6/bin/php", $brewMock->whichPhp('php@7.4')); $this->assertEquals(BREW_PREFIX."/Cellar/php@7.4/7.4.6/bin/php", $brewMock->whichPhp('php@7.4'));
@@ -415,7 +415,7 @@ public function test_it_can_get_php_binary_path_from_php_version_and_create_symb
$files = Mockery::mock(Filesystem::class) $files = Mockery::mock(Filesystem::class)
])->makePartial(); ])->makePartial();
$brewMock->shouldNotHaveReceived('getPhpBinaryPath'); $brewMock->shouldNotHaveReceived('getPhpExecutablePath');
$files->shouldReceive('isLink')->once()->with(BREW_PREFIX."/bin/valetphp81")->andReturn(true); $files->shouldReceive('isLink')->once()->with(BREW_PREFIX."/bin/valetphp81")->andReturn(true);
$files->shouldReceive('readLink')->once()->with(BREW_PREFIX."/bin/valetphp81")->andReturn(BREW_PREFIX."/Cellar/php@8.1/8.1.5/bin/php"); $files->shouldReceive('readLink')->once()->with(BREW_PREFIX."/bin/valetphp81")->andReturn(BREW_PREFIX."/Cellar/php@8.1/8.1.5/bin/php");
$files->shouldReceive('exists')->once()->with(BREW_PREFIX."/Cellar/php@8.1/8.1.5/bin/php")->andReturn(true); $files->shouldReceive('exists')->once()->with(BREW_PREFIX."/Cellar/php@8.1/8.1.5/bin/php")->andReturn(true);
@@ -429,7 +429,7 @@ public function test_it_can_get_php_binary_path_from_php_version_and_create_symb
$files = Mockery::mock(Filesystem::class) $files = Mockery::mock(Filesystem::class)
])->makePartial(); ])->makePartial();
$brewMock->shouldReceive('getPhpBinaryPath')->once()->with('php@7.4')->andReturn(BREW_PREFIX."/Cellar/php@7.4/7.4.6/bin/php"); $brewMock->shouldReceive('getPhpExecutablePath')->once()->with('php@7.4')->andReturn(BREW_PREFIX."/Cellar/php@7.4/7.4.6/bin/php");
$files->shouldReceive('symlinkAsUser')->once()->withArgs([BREW_PREFIX."/Cellar/php@7.4/7.4.6/bin/php", BREW_PREFIX."/bin/valetphp74"]); $files->shouldReceive('symlinkAsUser')->once()->withArgs([BREW_PREFIX."/Cellar/php@7.4/7.4.6/bin/php", BREW_PREFIX."/bin/valetphp74"]);
$this->assertEquals(BREW_PREFIX."/Cellar/php@7.4/7.4.6/bin/php", $brewMock->whichPhp('php@7.4', true)); $this->assertEquals(BREW_PREFIX."/Cellar/php@7.4/7.4.6/bin/php", $brewMock->whichPhp('php@7.4', true));
@@ -455,7 +455,7 @@ public function test_it_can_get_php_binary_path_from_php_version()
$cli->shouldReceive('runAsUser')->once()->with("brew info --json php@7.4")->andReturn('[{"linked_keg":"7.4.6","installed":[{"version":"7.4.5"}]}]'); $cli->shouldReceive('runAsUser')->once()->with("brew info --json php@7.4")->andReturn('[{"linked_keg":"7.4.6","installed":[{"version":"7.4.5"}]}]');
$files->shouldReceive('exists')->once()->with(BREW_PREFIX."/Cellar/php@7.4/7.4.6/bin/php")->andReturn(true); $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->getPhpBinaryPath('php@7.4')); $this->assertEquals(BREW_PREFIX."/Cellar/php@7.4/7.4.6/bin/php", $brewMock->getPhpExecutablePath('php@7.4'));
// Scenario when brew info doesn't have `linked_keg` // Scenario when brew info doesn't have `linked_keg`
$brewMock = Mockery::mock(Brew::class, [ $brewMock = Mockery::mock(Brew::class, [
@@ -467,7 +467,7 @@ public function test_it_can_get_php_binary_path_from_php_version()
$cli->shouldReceive('runAsUser')->once()->with("brew info --json php@8.0")->andReturn('[{"installed":[{"version":"8.0.5"}]}]'); $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); $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->getPhpBinaryPath('php@8.0')); $this->assertEquals(BREW_PREFIX."/Cellar/php@8.0/8.0.5/bin/php", $brewMock->getPhpExecutablePath('php@8.0'));
// Scenario when brew info has a version with that was not installed_as_dependency // Scenario when brew info has a version with that was not installed_as_dependency
$brewMock = Mockery::mock(Brew::class, [ $brewMock = Mockery::mock(Brew::class, [
@@ -480,7 +480,7 @@ public function test_it_can_get_php_binary_path_from_php_version()
->andReturn('[{"installed":[{"version":"8.1.1", "installed_as_dependency":true}, {"version":"8.1.2", "installed_as_dependency":false}]}]'); ->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); $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->getPhpBinaryPath('php@8.1')); $this->assertEquals(BREW_PREFIX."/Cellar/php@8.1/8.1.2/bin/php", $brewMock->getPhpExecutablePath('php@8.1'));
// Scenario when brew info has no version that was installed manually, it should pick the last PHP version // Scenario when brew info has no version that was installed manually, it should pick the last PHP version
$brewMock = Mockery::mock(Brew::class, [ $brewMock = Mockery::mock(Brew::class, [
@@ -493,7 +493,7 @@ public function test_it_can_get_php_binary_path_from_php_version()
->andReturn('[{"installed":[{"version":"7.4.1", "installed_as_dependency":false}, {"version":"7.4.3", "installed_as_dependency":false}]}]'); ->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); $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->getPhpBinaryPath('php@7.4')); $this->assertEquals(BREW_PREFIX."/Cellar/php@7.4/7.4.3/bin/php", $brewMock->getPhpExecutablePath('php@7.4'));
// Scenario when user has installed directly though shivammathur/homebrew-php // Scenario when user has installed directly though shivammathur/homebrew-php
$brewMock = Mockery::mock(Brew::class, [ $brewMock = Mockery::mock(Brew::class, [
@@ -505,7 +505,7 @@ public function test_it_can_get_php_binary_path_from_php_version()
$cli->shouldReceive('runAsUser')->once()->with("brew info --json php@7.4")->andReturn(false); $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); $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->getPhpBinaryPath('php@7.4')); $this->assertEquals(BREW_PREFIX."/opt/php@7.4/bin/php", $brewMock->getPhpExecutablePath('php@7.4'));
} }
/** /**

10
valet
View File

@@ -75,23 +75,25 @@ then
ARCH=$(uname -m) ARCH=$(uname -m)
if [[ $ARCH == 'arm64' ]]; then if [[ $ARCH == 'arm64' ]]; then
sudo -u "$USER" "$DIR/bin/ngrok-arm" http "$HOST.$TLD:$PORT" -host-header=rewrite $PARAMS "$DIR/bin/ngrok-arm" http "$HOST.$TLD:$PORT" -host-header=rewrite $PARAMS
else else
sudo -u "$USER" "$DIR/bin/ngrok" http "$HOST.$TLD:$PORT" -host-header=rewrite $PARAMS "$DIR/bin/ngrok" http "$HOST.$TLD:$PORT" -host-header=rewrite $PARAMS
fi fi
exit exit
# Proxy PHP commands to the "php" binary on the isolated site # Proxy PHP commands to the "php" executable on the isolated site
elif [[ "$1" = "php" ]] elif [[ "$1" = "php" ]]
then then
$(php "$DIR/cli/valet.php" which-php) "${@:2}" $(php "$DIR/cli/valet.php" which-php) "${@:2}"
exit exit
# Proxy composer commands with the "php" binary on the isolated site # Proxy Composer commands with the "php" executable on the isolated site
elif [[ "$1" = "composer" ]] elif [[ "$1" = "composer" ]]
then then
$(php "$DIR/cli/valet.php" which-php) $(which composer) "${@:2}" $(php "$DIR/cli/valet.php" which-php) $(which composer) "${@:2}"
exit exit
# Finally, for every other command we will just proxy into the PHP tool # Finally, for every other command we will just proxy into the PHP tool