Skip to content

Commit 40f8b48

Browse files
committed
refactor: migrate worker:* commands as modern commands
1 parent 874a820 commit 40f8b48

4 files changed

Lines changed: 171 additions & 93 deletions

File tree

system/Commands/Worker/WorkerInstall.php

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,23 @@
1313

1414
namespace CodeIgniter\Commands\Worker;
1515

16-
use CodeIgniter\CLI\BaseCommand;
16+
use CodeIgniter\CLI\AbstractCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
1718
use CodeIgniter\CLI\CLI;
19+
use CodeIgniter\CLI\Input\Option;
1820

1921
/**
20-
* Install Worker Mode for FrankenPHP.
21-
*
22-
* This command sets up the necessary files to run CodeIgniter 4
23-
* in FrankenPHP worker mode for improved performance.
22+
* Installs the files needed to run CodeIgniter 4 in FrankenPHP worker mode.
2423
*/
25-
class WorkerInstall extends BaseCommand
24+
#[Command(
25+
name: 'worker:install',
26+
description: 'Install FrankenPHP worker mode by creating necessary configuration files',
27+
group: 'Worker Mode',
28+
)]
29+
class WorkerInstall extends AbstractCommand
2630
{
27-
protected $group = 'Worker Mode';
28-
protected $name = 'worker:install';
29-
protected $description = 'Install FrankenPHP worker mode by creating necessary configuration files';
30-
protected $usage = 'worker:install [options]';
31-
protected $options = [
32-
'--force' => 'Overwrite existing files',
33-
];
34-
3531
/**
36-
* Template file mappings (template => destination path)
32+
* Template file mappings (template => destination path).
3733
*
3834
* @var array<string, string>
3935
*/
@@ -42,9 +38,18 @@ class WorkerInstall extends BaseCommand
4238
'Caddyfile.tpl' => 'Caddyfile',
4339
];
4440

45-
public function run(array $params)
41+
protected function configure(): void
42+
{
43+
$this->addOption(new Option(
44+
name: 'force',
45+
shortcut: 'f',
46+
description: 'Overwrite existing files.',
47+
));
48+
}
49+
50+
protected function execute(array $arguments, array $options): int
4651
{
47-
$force = array_key_exists('force', $params) || CLI::getOption('force');
52+
$force = $options['force'] === true;
4853

4954
CLI::write('Setting up FrankenPHP Worker Mode', 'yellow');
5055
CLI::newLine();
@@ -53,63 +58,46 @@ public function run(array $params)
5358

5459
$created = [];
5560

56-
// Process each template
5761
foreach ($this->templates as $template => $destination) {
5862
$source = SYSTEMPATH . 'Commands/Worker/Views/' . $template;
5963
$target = ROOTPATH . $destination;
6064

6165
$isFile = is_file($target);
6266

63-
// Skip if file exists and not forcing overwrite
6467
if (! $force && $isFile) {
6568
continue;
6669
}
6770

68-
// Read template content
6971
$content = file_get_contents($source);
72+
7073
if ($content === false) {
71-
CLI::error(
72-
"Failed to read template: {$template}",
73-
'light_gray',
74-
'red',
75-
);
76-
CLI::newLine();
74+
CLI::error(sprintf('Failed to read template: %s', $template), 'light_gray', 'red');
7775

7876
return EXIT_ERROR;
7977
}
8078

81-
// Write file to destination
8279
if (! write_file($target, $content)) {
83-
CLI::error(
84-
'Failed to create file: ' . clean_path($target),
85-
'light_gray',
86-
'red',
87-
);
88-
CLI::newLine();
80+
CLI::error(sprintf('Failed to create file: %s', clean_path($target)), 'light_gray', 'red');
8981

9082
return EXIT_ERROR;
9183
}
9284

9385
if ($force && $isFile) {
94-
CLI::write(' File overwritten: ' . clean_path($target), 'yellow');
86+
CLI::write(sprintf(' File overwritten: %s', clean_path($target)), 'yellow');
9587
} else {
96-
CLI::write(' File created: ' . clean_path($target), 'green');
88+
CLI::write(sprintf(' File created: %s', clean_path($target)), 'green');
9789
}
9890

9991
$created[] = $destination;
10092
}
10193

102-
// No files were created
10394
if ($created === []) {
104-
CLI::newLine();
10595
CLI::write('Worker mode files already exist.', 'yellow');
10696
CLI::write('Use --force to overwrite existing files.', 'yellow');
107-
CLI::newLine();
10897

10998
return EXIT_ERROR;
11099
}
111100

112-
// Success message
113101
CLI::newLine();
114102
CLI::write('Worker mode files created successfully!', 'green');
115103
CLI::newLine();
@@ -119,10 +107,7 @@ public function run(array $params)
119107
return EXIT_SUCCESS;
120108
}
121109

122-
/**
123-
* Display next steps to the user
124-
*/
125-
protected function showNextSteps(): void
110+
private function showNextSteps(): void
126111
{
127112
CLI::write('Next Steps:', 'yellow');
128113
CLI::newLine();
@@ -133,6 +118,5 @@ protected function showNextSteps(): void
133118

134119
CLI::write('2. Test your application:', 'white');
135120
CLI::write(' curl http://localhost:8080/', 'green');
136-
CLI::newLine();
137121
}
138122
}

system/Commands/Worker/WorkerUninstall.php

Lines changed: 69 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,23 @@
1313

1414
namespace CodeIgniter\Commands\Worker;
1515

16-
use CodeIgniter\CLI\BaseCommand;
16+
use CodeIgniter\CLI\AbstractCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
1718
use CodeIgniter\CLI\CLI;
19+
use CodeIgniter\CLI\Input\Option;
1820

1921
/**
20-
* Uninstall Worker Mode for FrankenPHP.
21-
*
22-
* This command removes the files created by the worker:install command.
22+
* Removes the files created by the worker:install command.
2323
*/
24-
class WorkerUninstall extends BaseCommand
24+
#[Command(
25+
name: 'worker:uninstall',
26+
description: 'Remove FrankenPHP worker mode configuration files',
27+
group: 'Worker Mode',
28+
)]
29+
class WorkerUninstall extends AbstractCommand
2530
{
26-
protected $group = 'Worker Mode';
27-
protected $name = 'worker:uninstall';
28-
protected $description = 'Remove FrankenPHP worker mode configuration files';
29-
protected $usage = 'worker:uninstall [options]';
30-
protected $options = [
31-
'--force' => 'Skip confirmation prompt',
32-
];
33-
3431
/**
35-
* Files to remove (must match Install command)
32+
* Files to remove (must match the worker:install command).
3633
*
3734
* @var list<string>
3835
*/
@@ -41,80 +38,103 @@ class WorkerUninstall extends BaseCommand
4138
'Caddyfile',
4239
];
4340

44-
public function run(array $params)
41+
protected function configure(): void
4542
{
46-
$force = array_key_exists('force', $params) || CLI::getOption('force');
43+
$this->addOption(new Option(
44+
name: 'force',
45+
shortcut: 'f',
46+
description: 'Skip the confirmation prompt.',
47+
));
48+
}
4749

48-
CLI::write('Uninstalling FrankenPHP Worker Mode', 'yellow');
49-
CLI::newLine();
50+
protected function interact(array &$arguments, array &$options): void
51+
{
52+
if ($this->hasUnboundOption('force', $options)) {
53+
return;
54+
}
5055

51-
// Find existing files
52-
$existing = [];
56+
if ($this->existingFiles() === []) {
57+
return;
58+
}
5359

54-
foreach ($this->files as $file) {
55-
$path = ROOTPATH . $file;
56-
if (is_file($path)) {
57-
$existing[] = $file;
58-
}
60+
if (CLI::prompt('Remove the FrankenPHP worker mode files?', ['y', 'n']) === 'y') {
61+
$options['force'] = null; // simulate the presence of the --force option
5962
}
63+
}
64+
65+
protected function execute(array $arguments, array $options): int
66+
{
67+
$existing = $this->existingFiles();
6068

61-
// No files to remove
6269
if ($existing === []) {
6370
CLI::write('No worker mode files found to remove.', 'yellow');
64-
CLI::newLine();
6571

6672
return EXIT_SUCCESS;
6773
}
6874

69-
// Show files that will be removed
70-
CLI::write('The following files will be removed:', 'yellow');
75+
if ($options['force'] === false) {
76+
if ($this->isInteractive()) {
77+
CLI::write('Uninstall cancelled.', 'yellow');
7178

72-
foreach ($existing as $file) {
73-
CLI::write(' - ' . $file, 'white');
74-
}
75-
CLI::newLine();
79+
return EXIT_SUCCESS;
80+
}
7681

77-
// Confirm deletion unless --force is used
78-
if (! $force) {
79-
$confirm = CLI::prompt('Are you sure you want to remove these files?', ['y', 'n']);
80-
CLI::newLine();
82+
CLI::error('Uninstall aborted: pass --force to remove worker mode files in non-interactive mode.', 'light_gray', 'red');
8183

82-
if ($confirm !== 'y') {
83-
CLI::write('Uninstall cancelled.', 'yellow');
84-
CLI::newLine();
84+
return EXIT_ERROR;
85+
}
8586

86-
return EXIT_ERROR;
87-
}
87+
CLI::newLine();
88+
CLI::write('The following files will be removed:', 'yellow');
89+
90+
foreach ($existing as $file) {
91+
CLI::write(sprintf(' - %s', $file), 'white');
8892
}
8993

94+
CLI::newLine();
95+
9096
$removed = [];
9197

92-
// Remove each file
9398
foreach ($existing as $file) {
9499
$path = ROOTPATH . $file;
95100

96101
if (! @unlink($path)) {
97-
CLI::error('Failed to remove file: ' . clean_path($path), 'light_gray', 'red');
102+
CLI::error(sprintf('Failed to remove file: %s', clean_path($path)), 'light_gray', 'red');
98103

99104
continue;
100105
}
101106

102-
CLI::write(' File removed: ' . clean_path($path), 'green');
107+
CLI::write(sprintf(' File removed: %s', clean_path($path)), 'green');
108+
103109
$removed[] = $file;
104110
}
105111

106-
// Summary
107112
CLI::newLine();
113+
108114
if ($removed === []) {
109-
CLI::error('No files were removed.');
110-
CLI::newLine();
115+
CLI::error('No files were removed.', 'light_gray', 'red');
111116

112117
return EXIT_ERROR;
113118
}
114119

115120
CLI::write('Worker mode files removed successfully!', 'green');
116-
CLI::newLine();
117121

118122
return EXIT_SUCCESS;
119123
}
124+
125+
/**
126+
* @return list<string>
127+
*/
128+
private function existingFiles(): array
129+
{
130+
$existing = [];
131+
132+
foreach ($this->files as $file) {
133+
if (is_file(ROOTPATH . $file)) {
134+
$existing[] = $file;
135+
}
136+
}
137+
138+
return $existing;
139+
}
120140
}

0 commit comments

Comments
 (0)