1313
1414namespace CodeIgniter \Commands \Worker ;
1515
16- use CodeIgniter \CLI \BaseCommand ;
16+ use CodeIgniter \CLI \AbstractCommand ;
17+ use CodeIgniter \CLI \Attributes \Command ;
1718use 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}
0 commit comments