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

refactor with which-php command

This commit is contained in:
NasirNobin
2022-03-22 15:26:53 +06:00
parent 09f3d3e392
commit 4f098a8028
3 changed files with 68 additions and 38 deletions

View File

@@ -300,43 +300,48 @@ function ($version) use ($resolvedPhpVersion) {
/** /**
* Get PHP binary path for a given version * Get PHP binary path for a given PHP Version
* *
* @param string $phpVersion * @param string|null $phpVersion
* *
* @return string * @return string
*/ */
public function getPhpBinaryPath($phpVersion) public function getPhpBinaryPath($phpVersion = null)
{ {
$versionInteger = preg_replace('~[^\d]~', '', $phpVersion); if(! $phpVersion){
$binPath = null; return BREW_PREFIX.'/bin/php';
}
if ($this->files->isLink(BREW_PREFIX . "/bin/valetphp{$versionInteger}")) { $versionInteger = preg_replace('~[^\d]~', '', $phpVersion);
$binPath = $this->files->readLink(BREW_PREFIX . "/bin/valetphp{$versionInteger}"); $phpBinaryPath = null;
if($this->files->exists($binPath)){ $symlinkedPath = BREW_PREFIX. "/bin/valetphp{$versionInteger}";
return $binPath;
if ($this->files->isLink($symlinkedPath)) {
$phpBinaryPath = $this->files->readLink($symlinkedPath);
if ($this->files->exists($phpBinaryPath)) {
return $symlinkedPath;
} }
} }
$cellar = $this->cli->runAsUser("brew --cellar $phpVersion"); $cellar = $this->cli->runAsUser("brew --cellar $phpVersion");
$details = json_decode($this->cli->runAsUser("brew info --json $phpVersion")); $details = json_decode($this->cli->runAsUser("brew info --json $phpVersion"));
$path = !empty($details[0]->linked_keg) ? $details[0]->linked_keg : $details[0]->installed[0]->version; $phpDirectory = !empty($details[0]->linked_keg) ? $details[0]->linked_keg : $details[0]->installed[0]->version;
if ($this->files->exists(trim($cellar).'/'.$path.'/bin/php')) { if ($this->files->exists(trim($cellar).'/'.$phpDirectory.'/bin/php')) {
$binPath = trim($cellar).'/'.$path.'/bin/php'; $phpBinaryPath = trim($cellar).'/'.$phpDirectory.'/bin/php';
} }
// if (!$binPath && $this->>files->exists(BREW_PREFIX . "/opt/$phpVersion/bin/php")) { if (is_null($phpBinaryPath) && $this->files->exists(BREW_PREFIX . "/opt/{$phpVersion}/bin/php")) {
// return BREW_PREFIX . "/opt/$phpVersion/bin/php"; $phpBinaryPath = BREW_PREFIX . "/opt/{$phpVersion}/bin/php";
// }
if($binPath){
$this->files->symlinkAsUser($binPath, BREW_PREFIX . "/bin/valetphp{$versionInteger}");
return $binPath;
} }
return "php"; if($phpBinaryPath){
$this->files->symlinkAsUser($phpBinaryPath, $symlinkedPath);
return $phpBinaryPath;
}
return BREW_PREFIX.'/bin/php';
} }
/** /**

View File

@@ -555,25 +555,46 @@
/** /**
* List isolated sites. * List isolated sites.
*/ */
$app->command('isolated [--site=] [--binary]', function ($site, $binary) { $app->command('isolated', function () {
if ($site) { $sites = PhpFpm::isolatedDirectories();
if ($phpVersion = Site::customPhpVersion($site.'.'.data_get(Configuration::read(), 'tld'))) {
$phpVersion = PhpFpm::normalizePhpVersion($phpVersion); table(['Path', 'PHP Version'], $sites->all());
if($binary){
return output(Brew::getPhpBinaryPath($phpVersion));
}
return output($phpVersion);
}
} else {
$sites = PhpFpm::isolatedDirectories();
table(['Path', 'PHP Version'], $sites->all());
}
})->descriptions('List all sites using isolated versions of PHP.'); })->descriptions('List all sites using isolated versions of PHP.');
/**
* Get PHP Birnary
*/
$app->command('which-php [site]', function ($site = null) {
$host = Site::host($site ? $site : getcwd()).'.'.Configuration::read()['tld'];
$phpVersion = Site::customPhpVersion($host);
if(! $phpVersion){
$path = getcwd().'/.valetphprc';
if (file_exists($path)) {
$phpVersion = trim(file_get_contents($path));
}
}
$phpVersion = $phpVersion ? PhpFpm::normalizePhpVersion($phpVersion) : null;
return output(Brew::getPhpBinaryPath($phpVersion));
})->descriptions('Get the PHP binary path for a given site', [
'site' => 'The site to get the PHP binary path for',
]);
/**
* Proxy PHP Commands with correct version
*/
$app->command('php', function () {
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');
/**
* Proxy composer commands with the "php" binary on the isolated site
*/
$app->command('composer', function () {
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');
/** /**
* Tail log file. * Tail log file.
*/ */

8
valet
View File

@@ -82,13 +82,17 @@ then
exit exit
# Proxy PHP commands to the "php" binary on the isolated site
elif [[ "$1" = "php" ]] elif [[ "$1" = "php" ]]
then then
$(php "$DIR/cli/valet.php" isolated --site=$(basename "$PWD") --binary) "${@:2}" $(php "$DIR/cli/valet.php" which-php) "${@:2}"
exit
# Proxy composer commands with the "php" binary on the isolated site
elif [[ "$1" = "composer" ]] elif [[ "$1" = "composer" ]]
then then
$(php "$DIR/cli/valet.php" isolated --site=$(basename "$PWD") --binary) $(which composer) "${@:2}" $(php "$DIR/cli/valet.php" which-php) $(which composer) "${@:2}"
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
# and let it handle the request. These are commands which can be run # and let it handle the request. These are commands which can be run