Skip to content

Commit 6fea029

Browse files
xiCO2ktaylorotwell
authored andcommitted
[2.x] Add laravel/prompts. (#917)
* feat: Add `laravel/prompts`. * wip --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 482f079 commit 6fea029

13 files changed

+65
-57
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"php": "^8.1.0",
1818
"laminas/laminas-diactoros": "^3.0",
1919
"laravel/framework": "^10.10.1|^11.0",
20+
"laravel/prompts": "^0.1.24",
2021
"laravel/serializable-closure": "^1.3.0",
2122
"nesbot/carbon": "^2.66.0|^3.0",
2223
"symfony/console": "^6.0|^7.0",

src/Commands/Concerns/InstallsFrankenPhpDependencies.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Symfony\Component\Process\Process;
1212
use Throwable;
1313

14+
use function Laravel\Prompts\confirm;
15+
1416
trait InstallsFrankenPhpDependencies
1517
{
1618
use FindsFrankenPhpBinary;
@@ -45,7 +47,7 @@ protected function ensureFrankenPhpBinaryIsInstalled()
4547
return $frankenphpBinary;
4648
}
4749

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

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

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

@@ -141,15 +143,15 @@ protected function ensureFrankenPhpBinaryMeetsRequirements($frankenPhpBinary)
141143
});
142144

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

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

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

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

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

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

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

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

176178
unlink("$frankenPhpBinary.backup");

src/Commands/Concerns/InstallsRoadRunnerDependencies.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Symfony\Component\Process\Process;
1313
use Throwable;
1414

15+
use function Laravel\Prompts\confirm;
16+
1517
trait InstallsRoadRunnerDependencies
1618
{
1719
use FindsRoadRunnerBinary;
@@ -44,8 +46,8 @@ protected function ensureRoadRunnerPackageIsInstalled()
4446
return true;
4547
}
4648

47-
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?')) {
48-
$this->error('Octane requires "spiral/roadrunner-http" and "spiral/roadrunner-cli".');
49+
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?')) {
50+
$this->components->error('Octane requires "spiral/roadrunner-http" and "spiral/roadrunner-cli".');
4951

5052
return false;
5153
}
@@ -102,7 +104,7 @@ protected function ensureRoadRunnerBinaryIsInstalled(): string
102104
return $roadRunnerBinary;
103105
}
104106

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

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

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

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

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

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

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

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

153155
unlink("$roadRunnerBinary.backup");

src/Commands/Concerns/InteractsWithIO.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public function handleStream($stream, $verbosity = null)
248248
'throwable' => $this->throwableInfo($stream, $verbosity),
249249
'shutdown' => $this->shutdownInfo($stream, $verbosity),
250250
'raw' => $this->raw(json_encode($stream)),
251-
default => $this->info(json_encode($stream), $verbosity)
251+
default => $this->components->info(json_encode($stream), $verbosity)
252252
};
253253
}
254254
}

src/Commands/Concerns/InteractsWithServers.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ protected function runServer($server, $inspector, $type)
3737

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

4242
$inspector->reloadServer();
4343
} elseif ($watcher->isTerminated()) {
44-
$this->error(
44+
$this->components->error(
4545
'Watcher process has terminated. Please ensure Node and chokidar are installed.'.PHP_EOL.
4646
$watcher->getErrorOutput()
4747
);
@@ -100,7 +100,7 @@ public function __call($method, $parameters)
100100
*/
101101
protected function writeServerRunningMessage()
102102
{
103-
$this->info('Server running…');
103+
$this->components->info('Server running…');
104104

105105
$this->output->writeln([
106106
'',

src/Commands/InstallCommand.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Symfony\Component\Console\Attribute\AsCommand;
99
use Throwable;
1010

11+
use function Laravel\Prompts\select;
12+
1113
#[AsCommand(name: 'octane:install')]
1214
class InstallCommand extends Command
1315
{
@@ -36,9 +38,10 @@ class InstallCommand extends Command
3638
*/
3739
public function handle()
3840
{
39-
$server = $this->option('server') ?: $this->choice(
40-
'Which application server you would like to use?',
41-
['roadrunner', 'swoole', 'frankenphp'],
41+
$server = $this->option('server') ?: select(
42+
label: 'Which application server you would like to use?',
43+
options: ['frankenphp', 'roadrunner', 'swoole'],
44+
default: 'frankenphp'
4245
);
4346

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

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

55-
$this->info('Octane installed successfully.');
58+
$this->components->info('Octane installed successfully.');
5659
$this->newLine();
5760
}
5861
});
@@ -75,7 +78,7 @@ public function updateEnvironmentFile($server)
7578
PHP_EOL.'OCTANE_SERVER='.$server.PHP_EOL,
7679
);
7780
} else {
78-
$this->warn('Please adjust the `OCTANE_SERVER` environment variable.');
81+
$this->components->warn('Please adjust the `OCTANE_SERVER` environment variable.');
7982
}
8083
}
8184
}
@@ -115,7 +118,7 @@ public function installRoadRunnerServer()
115118
public function installSwooleServer()
116119
{
117120
if (! resolve(SwooleExtension::class)->isInstalled()) {
118-
$this->warn('The Swoole extension is missing.');
121+
$this->components->warn('The Swoole extension is missing.');
119122
}
120123

121124
return true;
@@ -147,7 +150,7 @@ public function installFrankenPhpServer()
147150
try {
148151
$this->ensureFrankenPhpBinaryIsInstalled();
149152
} catch (Throwable $e) {
150-
$this->error($e->getMessage());
153+
$this->components->error($e->getMessage());
151154

152155
return false;
153156
}
@@ -162,7 +165,7 @@ public function installFrankenPhpServer()
162165
*/
163166
protected function invalidServer(string $server)
164167
{
165-
$this->error("Invalid server: {$server}.");
168+
$this->components->error("Invalid server: {$server}.");
166169

167170
return false;
168171
}

src/Commands/ReloadCommand.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ protected function reloadSwooleServer()
5151
$inspector = app(SwooleServerProcessInspector::class);
5252

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

5656
return 1;
5757
}
5858

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

6161
$inspector->reloadServer();
6262

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

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

7878
return 1;
7979
}
8080

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

8383
$inspector->reloadServer();
8484

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

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

100100
return 1;
101101
}
102102

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

105105
$inspector->reloadServer();
106106

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

119119
return 1;
120120
}

src/Commands/StartCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ protected function startFrankenPhpServer()
125125
*/
126126
protected function invalidServer(string $server)
127127
{
128-
$this->error("Invalid server: {$server}.");
128+
$this->components->error("Invalid server: {$server}.");
129129

130130
return 1;
131131
}

src/Commands/StartFrankenPhpCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function handle(ServerProcessInspector $inspector, ServerStateFile $serve
6565
$frankenphpBinary = $this->ensureFrankenPhpBinaryIsInstalled();
6666

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

7070
return 1;
7171
}
@@ -274,7 +274,7 @@ protected function writeServerOutput($server)
274274

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

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

313313
if (isset($debug['level'])) {
314314
if ($debug['level'] === 'warn') {
315-
return $this->warn($message);
315+
return $this->components->warn($message);
316316
}
317317

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

324-
return $this->error($message);
324+
return $this->components->error($message);
325325
}
326326
}
327327
});

src/Commands/StartRoadRunnerCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class StartRoadRunnerCommand extends Command implements SignalableCommandInterfa
5757
public function handle(ServerProcessInspector $inspector, ServerStateFile $serverStateFile)
5858
{
5959
if (! $this->isRoadRunnerInstalled()) {
60-
$this->error('RoadRunner not installed. Please execute the `octane:install` Artisan command.');
60+
$this->components->error('RoadRunner not installed. Please execute the `octane:install` Artisan command.');
6161

6262
return 1;
6363
}
@@ -67,7 +67,7 @@ public function handle(ServerProcessInspector $inspector, ServerStateFile $serve
6767
$this->ensurePortIsAvailable();
6868

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

7272
return 1;
7373
}
@@ -204,7 +204,7 @@ protected function writeServerOutput($server)
204204
->filter()
205205
->each(function ($output) {
206206
if (! is_array($debug = json_decode($output, true))) {
207-
return $this->info($output);
207+
return $this->components->info($output);
208208
}
209209

210210
if (is_array($stream = json_decode($debug['msg'], true))) {
@@ -240,7 +240,7 @@ protected function writeServerOutput($server)
240240
->filter()
241241
->each(function ($output) {
242242
if (! Str::contains($output, ['DEBUG', 'INFO', 'WARN'])) {
243-
$this->error($output);
243+
$this->components->error($output);
244244
}
245245
});
246246
}

0 commit comments

Comments
 (0)