Skip to content

Commit

Permalink
[2.x] Add laravel/prompts. (#917)
Browse files Browse the repository at this point in the history
* feat: Add `laravel/prompts`.

* wip

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
2 people authored and driesvints committed Jun 24, 2024
1 parent 482f079 commit 6fea029
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 57 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"php": "^8.1.0",
"laminas/laminas-diactoros": "^3.0",
"laravel/framework": "^10.10.1|^11.0",
"laravel/prompts": "^0.1.24",
"laravel/serializable-closure": "^1.3.0",
"nesbot/carbon": "^2.66.0|^3.0",
"symfony/console": "^6.0|^7.0",
Expand Down
16 changes: 9 additions & 7 deletions src/Commands/Concerns/InstallsFrankenPhpDependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Symfony\Component\Process\Process;
use Throwable;

use function Laravel\Prompts\confirm;

trait InstallsFrankenPhpDependencies
{
use FindsFrankenPhpBinary;
Expand Down Expand Up @@ -45,7 +47,7 @@ protected function ensureFrankenPhpBinaryIsInstalled()
return $frankenphpBinary;
}

if ($this->confirm('Unable to locate FrankenPHP binary. Should Octane download the binary for your operating system?', true)) {
if (confirm('Unable to locate FrankenPHP binary. Should Octane download the binary for your operating system?', true)) {
$this->downloadFrankenPhpBinary();
}

Expand Down Expand Up @@ -77,7 +79,7 @@ protected function downloadFrankenPhpBinary()
$response = Http::accept('application/vnd.github+json')
->withHeaders(['X-GitHub-Api-Version' => '2022-11-28'])
->get('https://api.github.com/repos/dunglas/frankenphp/releases/latest')
->throw(fn () => $this->error('Failed to download FrankenPHP.'));
->throw(fn () => $this->components->error('Failed to download FrankenPHP.'));

$assets = $response['assets'] ?? [];

Expand Down Expand Up @@ -141,15 +143,15 @@ protected function ensureFrankenPhpBinaryMeetsRequirements($frankenPhpBinary)
});

if ($lineWithVersion === null) {
return $this->warn(
return $this->components->warn(
'Unable to determine the current FrankenPHP binary version. Please report this issue: https://github.com/laravel/octane/issues/new.',
);
}

$version = Str::of($lineWithVersion)->trim()->afterLast('v')->value();

if (preg_match('/\d+\.\d+\.\d+/', $version) !== 1) {
return $this->warn(
return $this->components->warn(
'Unable to determine the current FrankenPHP binary version. Please report this issue: https://github.com/laravel/octane/issues/new.',
);
}
Expand All @@ -158,9 +160,9 @@ protected function ensureFrankenPhpBinaryMeetsRequirements($frankenPhpBinary)
return;
}

$this->warn("Your FrankenPHP binary version (<fg=red>$version</>) may be incompatible with Octane.");
$this->components->warn("Your FrankenPHP binary version (<fg=red>$version</>) may be incompatible with Octane.");

if ($this->confirm('Should Octane download the latest FrankenPHP binary version for your operating system?', true)) {
if (confirm('Should Octane download the latest FrankenPHP binary version for your operating system?', true)) {
rename($frankenPhpBinary, "$frankenPhpBinary.backup");

try {
Expand All @@ -170,7 +172,7 @@ protected function ensureFrankenPhpBinaryMeetsRequirements($frankenPhpBinary)

rename("$frankenPhpBinary.backup", $frankenPhpBinary);

return $this->warn('Unable to download FrankenPHP binary. The underlying error has been logged.');
return $this->components->warn('Unable to download FrankenPHP binary. The underlying error has been logged.');
}

unlink("$frankenPhpBinary.backup");
Expand Down
16 changes: 9 additions & 7 deletions src/Commands/Concerns/InstallsRoadRunnerDependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Symfony\Component\Process\Process;
use Throwable;

use function Laravel\Prompts\confirm;

trait InstallsRoadRunnerDependencies
{
use FindsRoadRunnerBinary;
Expand Down Expand Up @@ -44,8 +46,8 @@ protected function ensureRoadRunnerPackageIsInstalled()
return true;
}

if (! $this->confirm('Octane requires "spiral/roadrunner-http:^3.3.0" and "spiral/roadrunner-cli:^2.6.0". Do you wish to install them as a dependencies?')) {
$this->error('Octane requires "spiral/roadrunner-http" and "spiral/roadrunner-cli".');
if (! confirm('Octane requires "spiral/roadrunner-http:^3.3.0" and "spiral/roadrunner-cli:^2.6.0". Do you wish to install them as a dependencies?')) {
$this->components->error('Octane requires "spiral/roadrunner-http" and "spiral/roadrunner-cli".');

return false;
}
Expand Down Expand Up @@ -102,7 +104,7 @@ protected function ensureRoadRunnerBinaryIsInstalled(): string
return $roadRunnerBinary;
}

if ($this->confirm('Unable to locate RoadRunner binary. Should Octane download the binary for your operating system?', true)) {
if (confirm('Unable to locate RoadRunner binary. Should Octane download the binary for your operating system?', true)) {
$this->downloadRoadRunnerBinary();

copy(__DIR__.'/../stubs/rr.yaml', base_path('.rr.yaml'));
Expand All @@ -124,7 +126,7 @@ protected function ensureRoadRunnerBinaryMeetsRequirements($roadRunnerBinary)
->getOutput();

if (! Str::startsWith($version, 'rr version')) {
return $this->warn(
return $this->components->warn(
'Unable to determine the current RoadRunner binary version. Please report this issue: https://github.com/laravel/octane/issues/new.'
);
}
Expand All @@ -135,9 +137,9 @@ protected function ensureRoadRunnerBinaryMeetsRequirements($roadRunnerBinary)
return;
}

$this->warn("Your RoadRunner binary version (<fg=red>$version</>) may be incompatible with Octane.");
$this->components->warn("Your RoadRunner binary version (<fg=red>$version</>) may be incompatible with Octane.");

if ($this->confirm('Should Octane download the latest RoadRunner binary version for your operating system?', true)) {
if (confirm('Should Octane download the latest RoadRunner binary version for your operating system?', true)) {
rename($roadRunnerBinary, "$roadRunnerBinary.backup");

try {
Expand All @@ -147,7 +149,7 @@ protected function ensureRoadRunnerBinaryMeetsRequirements($roadRunnerBinary)

rename("$roadRunnerBinary.backup", $roadRunnerBinary);

return $this->warn('Unable to download RoadRunner binary. The HTTP request exception has been logged.');
return $this->components->warn('Unable to download RoadRunner binary. The HTTP request exception has been logged.');
}

unlink("$roadRunnerBinary.backup");
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/Concerns/InteractsWithIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public function handleStream($stream, $verbosity = null)
'throwable' => $this->throwableInfo($stream, $verbosity),
'shutdown' => $this->shutdownInfo($stream, $verbosity),
'raw' => $this->raw(json_encode($stream)),
default => $this->info(json_encode($stream), $verbosity)
default => $this->components->info(json_encode($stream), $verbosity)
};
}
}
6 changes: 3 additions & 3 deletions src/Commands/Concerns/InteractsWithServers.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ protected function runServer($server, $inspector, $type)

if ($watcher->isRunning() &&
$watcher->getIncrementalOutput()) {
$this->info('Application change detected. Restarting workers…');
$this->components->info('Application change detected. Restarting workers…');

$inspector->reloadServer();
} elseif ($watcher->isTerminated()) {
$this->error(
$this->components->error(
'Watcher process has terminated. Please ensure Node and chokidar are installed.'.PHP_EOL.
$watcher->getErrorOutput()
);
Expand Down Expand Up @@ -100,7 +100,7 @@ public function __call($method, $parameters)
*/
protected function writeServerRunningMessage()
{
$this->info('Server running…');
$this->components->info('Server running…');

$this->output->writeln([
'',
Expand Down
19 changes: 11 additions & 8 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Symfony\Component\Console\Attribute\AsCommand;
use Throwable;

use function Laravel\Prompts\select;

#[AsCommand(name: 'octane:install')]
class InstallCommand extends Command
{
Expand Down Expand Up @@ -36,9 +38,10 @@ class InstallCommand extends Command
*/
public function handle()
{
$server = $this->option('server') ?: $this->choice(
'Which application server you would like to use?',
['roadrunner', 'swoole', 'frankenphp'],
$server = $this->option('server') ?: select(
label: 'Which application server you would like to use?',
options: ['frankenphp', 'roadrunner', 'swoole'],
default: 'frankenphp'
);

return (int) ! tap(match ($server) {
Expand All @@ -52,7 +55,7 @@ public function handle()

$this->callSilent('vendor:publish', ['--tag' => 'octane-config', '--force' => true]);

$this->info('Octane installed successfully.');
$this->components->info('Octane installed successfully.');
$this->newLine();
}
});
Expand All @@ -75,7 +78,7 @@ public function updateEnvironmentFile($server)
PHP_EOL.'OCTANE_SERVER='.$server.PHP_EOL,
);
} else {
$this->warn('Please adjust the `OCTANE_SERVER` environment variable.');
$this->components->warn('Please adjust the `OCTANE_SERVER` environment variable.');
}
}
}
Expand Down Expand Up @@ -115,7 +118,7 @@ public function installRoadRunnerServer()
public function installSwooleServer()
{
if (! resolve(SwooleExtension::class)->isInstalled()) {
$this->warn('The Swoole extension is missing.');
$this->components->warn('The Swoole extension is missing.');
}

return true;
Expand Down Expand Up @@ -147,7 +150,7 @@ public function installFrankenPhpServer()
try {
$this->ensureFrankenPhpBinaryIsInstalled();
} catch (Throwable $e) {
$this->error($e->getMessage());
$this->components->error($e->getMessage());

return false;
}
Expand All @@ -162,7 +165,7 @@ public function installFrankenPhpServer()
*/
protected function invalidServer(string $server)
{
$this->error("Invalid server: {$server}.");
$this->components->error("Invalid server: {$server}.");

return false;
}
Expand Down
14 changes: 7 additions & 7 deletions src/Commands/ReloadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ protected function reloadSwooleServer()
$inspector = app(SwooleServerProcessInspector::class);

if (! $inspector->serverIsRunning()) {
$this->error('Octane server is not running.');
$this->components->error('Octane server is not running.');

return 1;
}

$this->info('Reloading workers...');
$this->components->info('Reloading workers...');

$inspector->reloadServer();

Expand All @@ -73,12 +73,12 @@ protected function reloadRoadRunnerServer()
$inspector = app(RoadRunnerServerProcessInspector::class);

if (! $inspector->serverIsRunning()) {
$this->error('Octane server is not running.');
$this->components->error('Octane server is not running.');

return 1;
}

$this->info('Reloading workers...');
$this->components->info('Reloading workers...');

$inspector->reloadServer();

Expand All @@ -95,12 +95,12 @@ protected function reloadFrankenPhpServer()
$inspector = app(FrankenPhpServerProcessInspector::class);

if (! $inspector->serverIsRunning()) {
$this->error('Octane server is not running.');
$this->components->error('Octane server is not running.');

return 1;
}

$this->info('Reloading workers...');
$this->components->info('Reloading workers...');

$inspector->reloadServer();

Expand All @@ -114,7 +114,7 @@ protected function reloadFrankenPhpServer()
*/
protected function invalidServer(string $server)
{
$this->error("Invalid server: {$server}.");
$this->components->error("Invalid server: {$server}.");

return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/StartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected function startFrankenPhpServer()
*/
protected function invalidServer(string $server)
{
$this->error("Invalid server: {$server}.");
$this->components->error("Invalid server: {$server}.");

return 1;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Commands/StartFrankenPhpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function handle(ServerProcessInspector $inspector, ServerStateFile $serve
$frankenphpBinary = $this->ensureFrankenPhpBinaryIsInstalled();

if ($inspector->serverIsRunning()) {
$this->error('FrankenPHP server is already running.');
$this->components->error('FrankenPHP server is already running.');

return 1;
}
Expand Down Expand Up @@ -274,7 +274,7 @@ protected function writeServerOutput($server)

$errorOutput->each(function ($output) {
if (! is_array($debug = json_decode($output, true))) {
return $this->info($output);
return $this->components->info($output);
}

$message = $debug['msg'] ?? 'unknown error';
Expand Down Expand Up @@ -312,7 +312,7 @@ protected function writeServerOutput($server)

if (isset($debug['level'])) {
if ($debug['level'] === 'warn') {
return $this->warn($message);
return $this->components->warn($message);
}

if ($debug['level'] !== 'info') {
Expand All @@ -321,7 +321,7 @@ protected function writeServerOutput($server)
return;
}

return $this->error($message);
return $this->components->error($message);
}
}
});
Expand Down
8 changes: 4 additions & 4 deletions src/Commands/StartRoadRunnerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class StartRoadRunnerCommand extends Command implements SignalableCommandInterfa
public function handle(ServerProcessInspector $inspector, ServerStateFile $serverStateFile)
{
if (! $this->isRoadRunnerInstalled()) {
$this->error('RoadRunner not installed. Please execute the `octane:install` Artisan command.');
$this->components->error('RoadRunner not installed. Please execute the `octane:install` Artisan command.');

return 1;
}
Expand All @@ -67,7 +67,7 @@ public function handle(ServerProcessInspector $inspector, ServerStateFile $serve
$this->ensurePortIsAvailable();

if ($inspector->serverIsRunning()) {
$this->error('RoadRunner server is already running.');
$this->components->error('RoadRunner server is already running.');

return 1;
}
Expand Down Expand Up @@ -204,7 +204,7 @@ protected function writeServerOutput($server)
->filter()
->each(function ($output) {
if (! is_array($debug = json_decode($output, true))) {
return $this->info($output);
return $this->components->info($output);
}

if (is_array($stream = json_decode($debug['msg'], true))) {
Expand Down Expand Up @@ -240,7 +240,7 @@ protected function writeServerOutput($server)
->filter()
->each(function ($output) {
if (! Str::contains($output, ['DEBUG', 'INFO', 'WARN'])) {
$this->error($output);
$this->components->error($output);
}
});
}
Expand Down
Loading

0 comments on commit 6fea029

Please sign in to comment.