Skip to content

Commit 63a8c9c

Browse files
committed
refactor: write the Worker commands through $this->getOutput()
1 parent 58efb74 commit 63a8c9c

2 files changed

Lines changed: 36 additions & 34 deletions

File tree

‎system/Commands/Worker/WorkerInstall.php‎

Lines changed: 22 additions & 20 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
/**
@@ -49,10 +48,11 @@ protected function configure(): void
4948

5049
protected function execute(array $arguments, array $options): int
5150
{
52-
$force = $options['force'] === true;
51+
$force = $options['force'] === true;
52+
$output = $this->getOutput();
5353

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

5757
helper('filesystem');
5858

@@ -71,36 +71,36 @@ protected function execute(array $arguments, array $options): int
7171
$content = file_get_contents($source);
7272

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

7676
return EXIT_ERROR;
7777
}
7878

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

8282
return EXIT_ERROR;
8383
}
8484

8585
if ($force && $isFile) {
86-
CLI::write(sprintf(' File overwritten: %s', clean_path($target)), 'yellow');
86+
$output->write(sprintf(' File overwritten: %s', clean_path($target)), 'yellow');
8787
} else {
88-
CLI::write(sprintf(' File created: %s', clean_path($target)), 'green');
88+
$output->write(sprintf(' File created: %s', clean_path($target)), 'green');
8989
}
9090

9191
$created[] = $destination;
9292
}
9393

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

9898
return EXIT_ERROR;
9999
}
100100

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

105105
$this->showNextSteps();
106106

@@ -109,14 +109,16 @@ protected function execute(array $arguments, array $options): int
109109

110110
private function showNextSteps(): void
111111
{
112-
CLI::write('Next Steps:', 'yellow');
113-
CLI::newLine();
112+
$output = $this->getOutput();
114113

115-
CLI::write('1. Start FrankenPHP:', 'white');
116-
CLI::write(' frankenphp run', 'green');
117-
CLI::newLine();
114+
$output->write('Next Steps:', 'yellow');
115+
$output->newLine();
118116

119-
CLI::write('2. Test your application:', 'white');
120-
CLI::write(' curl http://localhost:8080/', 'green');
117+
$output->write('1. Start FrankenPHP:', 'white');
118+
$output->write(' frankenphp run', 'green');
119+
$output->newLine();
120+
121+
$output->write('2. Test your application:', 'white');
122+
$output->write(' curl http://localhost:8080/', 'green');
121123
}
122124
}

‎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)