Skip to content

Commit

Permalink
gracefully fail when unable to locate expected binary on the system
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Aug 3, 2022
1 parent abcc6ea commit 1bc16f2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 13 additions & 3 deletions src/Illuminate/Foundation/Console/DocsCommand.php
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;
use Throwable;

Expand Down Expand Up @@ -351,7 +352,7 @@ protected function open($url)
} elseif (in_array($this->systemOsFamily, ['Darwin', 'Windows', 'Linux'])) {
$this->openViaBuiltInStrategy($url);
} else {
$this->components->warn('Unable to open the URL on your system. You will need to open it yourself.');
$this->components->warn('Unable to open the URL on your system. You will need to open it yourself or create a custom opener for your system.');
}
})($url);
}
Expand Down Expand Up @@ -385,13 +386,22 @@ protected function openViaCustomStrategy($url)
* @param string $url
* @return void
*/

protected function openViaBuiltInStrategy($url)
{
$process = tap(Process::fromShellCommandline(match ($this->systemOsFamily) {
$binary = (new ExecutableFinder())->find(match ($this->systemOsFamily) {
'Darwin' => 'open',
'Windows' => 'start',
'Linux' => 'xdg-open',
}.' '.escapeshellarg($url)))->run();
});

if ($binary === null) {
$this->components->warn('Unable to open the URL on your system. You will need to open it yourself or create a custom opener for your system.');

return;
}

$process = tap(Process::fromShellCommandline($binary.' '.escapeshellarg($url)))->run();

if (! $process->isSuccessful()) {
throw new ProcessFailedException($process);
Expand Down
2 changes: 1 addition & 1 deletion tests/Foundation/FoundationDocsCommandTest.php
Expand Up @@ -292,7 +292,7 @@ public function testUnknownSystemNotifiedToOpenManualy()
$this->app[Kernel::class]->registerCommand($this->command()->setUrlOpener(null)->setSystemOsFamily('Laravel OS'));

$this->artisan('docs validation')
->expectsOutputToContain('Unable to open the URL on your system. You will need to open it yourself.')
->expectsOutputToContain('Unable to open the URL on your system. You will need to open it yourself or create a custom opener for your system.')
->assertSuccessful();
}

Expand Down

0 comments on commit 1bc16f2

Please sign in to comment.