Skip to content

Commit

Permalink
[5.x] Fixes issue if there is no herd or valet installed (#343)
Browse files Browse the repository at this point in the history
* Fixes issue if there is no `herd` or `valet` installed.

* style: fixes

* Update NewCommand.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
xiCO2k and taylorotwell authored May 31, 2024
1 parent 0094be6 commit 8ec6dbd
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Exception\ProcessStartFailedException;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -760,17 +761,7 @@ protected function generateAppUrl($name)
*/
protected function getTld()
{
foreach (['herd', 'valet'] as $tool) {
$process = new Process([$tool, 'tld', '-v']);

$process->run();

if ($process->isSuccessful()) {
return trim($process->getOutput());
}
}

return 'test';
return $this->runOnValetOrHerd('tld') ?? 'test';
}

/**
Expand All @@ -792,19 +783,9 @@ protected function canResolveHostname($hostname)
*/
protected function isParked(string $directory)
{
foreach (['herd', 'valet'] as $tool) {
$process = new Process([$tool, 'paths', '-v']);
$output = $this->runOnValetOrHerd('paths');

$process->run();

if ($process->isSuccessful()) {
$output = json_decode(trim($process->getOutput()));

return in_array(dirname($directory), $output);
}
}

return false;
return $output !== false ? in_array(dirname($directory), json_decode($output)) : false;
}

/**
Expand Down Expand Up @@ -846,6 +827,30 @@ protected function phpBinary()
: 'php';
}

/**
* Runs the given command on the "herd" or "valet" CLI.
*
* @param string $command
* @return string|bool
*/
protected function runOnValetOrHerd(string $command)
{
foreach (['herd', 'valet'] as $tool) {
$process = new Process([$tool, $command, '-v']);

try {
$process->run();

if ($process->isSuccessful()) {
return trim($process->getOutput());
}
} catch (ProcessStartFailedException) {
}
}

return false;
}

/**
* Run the given commands.
*
Expand Down

0 comments on commit 8ec6dbd

Please sign in to comment.