Skip to content

Commit 2556a57

Browse files
authored
refactor: write the Worker commands through $this->getOutput() (#10676)
1 parent 3ac37dc commit 2556a57

2 files changed

Lines changed: 37 additions & 36 deletions

File tree

‎system/Commands/Worker/WorkerInstall.php‎

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
use CodeIgniter\CLI\AbstractCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
18-
use CodeIgniter\CLI\CLI;
1918
use CodeIgniter\CLI\Input\Option;
19+
use CodeIgniter\CLI\Output;
2020

2121
/**
2222
* Installs the files needed to run CodeIgniter 4 in FrankenPHP worker mode.
@@ -49,10 +49,11 @@ protected function configure(): void
4949

5050
protected function execute(array $arguments, array $options): int
5151
{
52-
$force = $options['force'] === true;
52+
$force = $options['force'] === true;
53+
$output = $this->getOutput();
5354

54-
CLI::write('Setting up FrankenPHP Worker Mode', 'yellow');
55-
CLI::newLine();
55+
$output->write('Setting up FrankenPHP Worker Mode', 'yellow');
56+
$output->newLine();
5657

5758
helper('filesystem');
5859

@@ -71,52 +72,52 @@ protected function execute(array $arguments, array $options): int
7172
$content = file_get_contents($source);
7273

7374
if ($content === false) {
74-
CLI::error(sprintf('Failed to read template: %s', $template), 'light_gray', 'red');
75+
$output->error(sprintf('Failed to read template: %s', $template), 'light_gray', 'red');
7576

7677
return EXIT_ERROR;
7778
}
7879

7980
if (! write_file($target, $content)) {
80-
CLI::error(sprintf('Failed to create file: %s', clean_path($target)), 'light_gray', 'red');
81+
$output->error(sprintf('Failed to create file: %s', clean_path($target)), 'light_gray', 'red');
8182

8283
return EXIT_ERROR;
8384
}
8485

8586
if ($force && $isFile) {
86-
CLI::write(sprintf(' File overwritten: %s', clean_path($target)), 'yellow');
87+
$output->write(sprintf(' File overwritten: %s', clean_path($target)), 'yellow');
8788
} else {
88-
CLI::write(sprintf(' File created: %s', clean_path($target)), 'green');
89+
$output->write(sprintf(' File created: %s', clean_path($target)), 'green');
8990
}
9091

9192
$created[] = $destination;
9293
}
9394

9495
if ($created === []) {
95-
CLI::write('Worker mode files already exist.', 'yellow');
96-
CLI::write('Use --force to overwrite existing files.', 'yellow');
96+
$output->write('Worker mode files already exist.', 'yellow');
97+
$output->write('Use --force to overwrite existing files.', 'yellow');
9798

9899
return EXIT_ERROR;
99100
}
100101

101-
CLI::newLine();
102-
CLI::write('Worker mode files created successfully!', 'green');
103-
CLI::newLine();
102+
$output->newLine();
103+
$output->write('Worker mode files created successfully!', 'green');
104+
$output->newLine();
104105

105-
$this->showNextSteps();
106+
$this->showNextSteps($output);
106107

107108
return EXIT_SUCCESS;
108109
}
109110

110-
private function showNextSteps(): void
111+
private function showNextSteps(Output $output): void
111112
{
112-
CLI::write('Next Steps:', 'yellow');
113-
CLI::newLine();
113+
$output->write('Next Steps:', 'yellow');
114+
$output->newLine();
114115

115-
CLI::write('1. Start FrankenPHP:', 'white');
116-
CLI::write(' frankenphp run', 'green');
117-
CLI::newLine();
116+
$output->write('1. Start FrankenPHP:', 'white');
117+
$output->write(' frankenphp run', 'green');
118+
$output->newLine();
118119

119-
CLI::write('2. Test your application:', 'white');
120-
CLI::write(' curl http://localhost:8080/', 'green');
120+
$output->write('2. Test your application:', 'white');
121+
$output->write(' curl http://localhost:8080/', 'green');
121122
}
122123
}

‎system/Commands/Worker/WorkerUninstall.php‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\CLI\AbstractCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
18-
use CodeIgniter\CLI\CLI;
1918
use CodeIgniter\CLI\Input\Option;
2019

2120
/**
@@ -57,67 +56,68 @@ protected function interact(array &$arguments, array &$options): void
5756
return;
5857
}
5958

60-
if (CLI::prompt('Remove the FrankenPHP worker mode files?', ['y', 'n']) === 'y') {
59+
if ($this->getPrompt()->ask('Remove the FrankenPHP worker mode files?', ['y', 'n']) === 'y') {
6160
$options['force'] = null; // simulate the presence of the --force option
6261
}
6362
}
6463

6564
protected function execute(array $arguments, array $options): int
6665
{
6766
$existing = $this->existingFiles();
67+
$output = $this->getOutput();
6868

6969
if ($existing === []) {
70-
CLI::write('No worker mode files found to remove.', 'yellow');
70+
$output->write('No worker mode files found to remove.', 'yellow');
7171

7272
return EXIT_SUCCESS;
7373
}
7474

7575
if ($options['force'] === false) {
7676
if ($this->isInteractive()) {
77-
CLI::write('Uninstall cancelled.', 'yellow');
77+
$output->write('Uninstall cancelled.', 'yellow');
7878

7979
return EXIT_SUCCESS;
8080
}
8181

82-
CLI::error('Uninstall aborted: pass --force to remove worker mode files in non-interactive mode.', 'light_gray', 'red');
82+
$output->error('Uninstall aborted: pass --force to remove worker mode files in non-interactive mode.', 'light_gray', 'red');
8383

8484
return EXIT_ERROR;
8585
}
8686

87-
CLI::newLine();
88-
CLI::write('The following files will be removed:', 'yellow');
87+
$output->newLine();
88+
$output->write('The following files will be removed:', 'yellow');
8989

9090
foreach ($existing as $file) {
91-
CLI::write(sprintf(' - %s', $file), 'white');
91+
$output->write(sprintf(' - %s', $file), 'white');
9292
}
9393

94-
CLI::newLine();
94+
$output->newLine();
9595

9696
$removed = [];
9797

9898
foreach ($existing as $file) {
9999
$path = ROOTPATH . $file;
100100

101101
if (! @unlink($path)) {
102-
CLI::error(sprintf('Failed to remove file: %s', clean_path($path)), 'light_gray', 'red');
102+
$output->error(sprintf('Failed to remove file: %s', clean_path($path)), 'light_gray', 'red');
103103

104104
continue;
105105
}
106106

107-
CLI::write(sprintf(' File removed: %s', clean_path($path)), 'green');
107+
$output->write(sprintf(' File removed: %s', clean_path($path)), 'green');
108108

109109
$removed[] = $file;
110110
}
111111

112-
CLI::newLine();
112+
$output->newLine();
113113

114114
if ($removed === []) {
115-
CLI::error('No files were removed.', 'light_gray', 'red');
115+
$output->error('No files were removed.', 'light_gray', 'red');
116116

117117
return EXIT_ERROR;
118118
}
119119

120-
CLI::write('Worker mode files removed successfully!', 'green');
120+
$output->write('Worker mode files removed successfully!', 'green');
121121

122122
return EXIT_SUCCESS;
123123
}

0 commit comments

Comments
 (0)