1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-05 00:20:08 +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
*/
public function getPhpBinaryPath($phpVersion)
public function getPhpBinaryPath($phpVersion = null)
{
$versionInteger = preg_replace('~[^\d]~', '', $phpVersion);
$binPath = null;
if(! $phpVersion){
return BREW_PREFIX.'/bin/php';
}
if ($this->files->isLink(BREW_PREFIX . "/bin/valetphp{$versionInteger}")) {
$binPath = $this->files->readLink(BREW_PREFIX . "/bin/valetphp{$versionInteger}");
if($this->files->exists($binPath)){
return $binPath;
$versionInteger = preg_replace('~[^\d]~', '', $phpVersion);
$phpBinaryPath = null;
$symlinkedPath = BREW_PREFIX. "/bin/valetphp{$versionInteger}";
if ($this->files->isLink($symlinkedPath)) {
$phpBinaryPath = $this->files->readLink($symlinkedPath);
if ($this->files->exists($phpBinaryPath)) {
return $symlinkedPath;
}
}
$cellar = $this->cli->runAsUser("brew --cellar $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')) {
$binPath = trim($cellar).'/'.$path.'/bin/php';
if ($this->files->exists(trim($cellar).'/'.$phpDirectory.'/bin/php')) {
$phpBinaryPath = trim($cellar).'/'.$phpDirectory.'/bin/php';
}
// if (!$binPath && $this->>files->exists(BREW_PREFIX . "/opt/$phpVersion/bin/php")) {
// return BREW_PREFIX . "/opt/$phpVersion/bin/php";
// }
if($binPath){
$this->files->symlinkAsUser($binPath, BREW_PREFIX . "/bin/valetphp{$versionInteger}");
return $binPath;
if (is_null($phpBinaryPath) && $this->files->exists(BREW_PREFIX . "/opt/{$phpVersion}/bin/php")) {
$phpBinaryPath = BREW_PREFIX . "/opt/{$phpVersion}/bin/php";
}
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.
*/
$app->command('isolated [--site=] [--binary]', function ($site, $binary) {
if ($site) {
if ($phpVersion = Site::customPhpVersion($site.'.'.data_get(Configuration::read(), 'tld'))) {
$phpVersion = PhpFpm::normalizePhpVersion($phpVersion);
if($binary){
return output(Brew::getPhpBinaryPath($phpVersion));
}
return output($phpVersion);
}
} else {
$app->command('isolated', function () {
$sites = PhpFpm::isolatedDirectories();
table(['Path', 'PHP Version'], $sites->all());
}
})->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.
*/

8
valet
View File

@@ -82,13 +82,17 @@ then
exit
# Proxy PHP commands to the "php" binary on the isolated site
elif [[ "$1" = "php" ]]
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" ]]
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
# and let it handle the request. These are commands which can be run