1313
1414namespace CodeIgniter \Commands \Utilities ;
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 \Argument ;
1820use CodeIgniter \Config \DotEnv ;
1921use Config \Paths ;
2022
2123/**
2224 * Command to display the current environment,
2325 * or set a new one in the `.env` file.
2426 */
25- final class Environment extends BaseCommand
27+ #[Command(name: 'env ' , description: 'Retrieves the current environment, or set a new one. ' , group: 'CodeIgniter ' )]
28+ class Environment extends AbstractCommand
2629{
27- /**
28- * The group the command is lumped under
29- * when listing commands.
30- *
31- * @var string
32- */
33- protected $ group = 'CodeIgniter ' ;
34-
35- /**
36- * The Command's name
37- *
38- * @var string
39- */
40- protected $ name = 'env ' ;
41-
42- /**
43- * The Command's short description
44- *
45- * @var string
46- */
47- protected $ description = 'Retrieves the current environment, or set a new one. ' ;
48-
49- /**
50- * The Command's usage
51- *
52- * @var string
53- */
54- protected $ usage = 'env [<environment>] ' ;
55-
56- /**
57- * The Command's arguments
58- *
59- * @var array<string, string>
60- */
61- protected $ arguments = [
62- 'environment ' => '[Optional] The new environment to set. If none is provided, this will print the current environment. ' ,
63- ];
64-
65- /**
66- * The Command's options
67- *
68- * @var array<string, string>
69- */
70- protected $ options = [];
71-
7230 /**
7331 * Allowed values for environment. `testing` is excluded
7432 * since spark won't work on it.
@@ -80,52 +38,63 @@ final class Environment extends BaseCommand
8038 'development ' ,
8139 ];
8240
83- /**
84- * @return int
85- */
86- public function run (array $ params )
41+ protected function configure (): void
8742 {
88- if (! isset ($ params [0 ])) {
89- CLI ::write (sprintf ('Your environment is currently set as %s. ' , CLI ::color (service ('superglobals ' )->server ('CI_ENVIRONMENT ' , ENVIRONMENT ), 'green ' )));
90- CLI ::newLine ();
43+ $ this ->addArgument (new Argument (
44+ name: 'environment ' ,
45+ description: 'The new environment to set. If none is provided, the current environment is printed. ' ,
46+ default: '' ,
47+ ));
48+ }
49+
50+ protected function execute (array $ arguments , array $ options ): int
51+ {
52+ $ env = $ arguments ['environment ' ];
53+ assert (is_string ($ env ));
54+
55+ if ($ env === '' ) {
56+ CLI ::write (sprintf (
57+ 'Your environment is currently set as %s. ' ,
58+ CLI ::color (service ('superglobals ' )->server ('CI_ENVIRONMENT ' , ENVIRONMENT ), 'green ' ),
59+ ));
9160
9261 return EXIT_SUCCESS ;
9362 }
9463
95- $ env = strtolower (array_shift ( $ params ) );
64+ $ env = strtolower ($ env );
9665
9766 if ($ env === 'testing ' ) {
9867 CLI ::error ('The "testing" environment is reserved for PHPUnit testing. ' , 'light_gray ' , 'red ' );
9968 CLI ::error ('You will not be able to run spark under a "testing" environment. ' , 'light_gray ' , 'red ' );
100- CLI ::newLine ();
10169
10270 return EXIT_ERROR ;
10371 }
10472
10573 if (! in_array ($ env , self ::$ knownTypes , true )) {
106- CLI ::error (sprintf ('Invalid environment type "%s". Expected one of "%s". ' , $ env , implode ('" and " ' , self ::$ knownTypes )), 'light_gray ' , 'red ' );
107- CLI ::newLine ();
74+ CLI ::error (sprintf (
75+ 'Invalid environment type "%s". Expected one of "%s". ' ,
76+ $ env ,
77+ implode ('" and " ' , self ::$ knownTypes ),
78+ ), 'light_gray ' , 'red ' );
10879
10980 return EXIT_ERROR ;
11081 }
11182
11283 if (! $ this ->writeNewEnvironmentToEnvFile ($ env )) {
11384 CLI ::error ('Error in writing new environment to .env file. ' , 'light_gray ' , 'red ' );
114- CLI ::newLine ();
11585
11686 return EXIT_ERROR ;
11787 }
11888
119- // force DotEnv to reload the new environment
120- // however we cannot redefine the ENVIRONMENT constant
89+ // Reload DotEnv with the new environment. The ENVIRONMENT constant
90+ // only takes the new value on the next script execution.
12191 putenv ('CI_ENVIRONMENT ' );
12292 unset($ _ENV ['CI_ENVIRONMENT ' ]);
12393 service ('superglobals ' )->unsetServer ('CI_ENVIRONMENT ' );
12494 (new DotEnv ((new Paths ())->envDirectory ?? ROOTPATH ))->load (); // @phpstan-ignore nullCoalesce.property
12595
12696 CLI ::write (sprintf ('Environment is successfully changed to "%s". ' , $ env ), 'green ' );
12797 CLI ::write ('The ENVIRONMENT constant will be changed in the next script execution. ' );
128- CLI ::newLine ();
12998
13099 return EXIT_SUCCESS ;
131100 }
@@ -142,7 +111,6 @@ private function writeNewEnvironmentToEnvFile(string $newEnv): bool
142111 if (! is_file ($ baseEnv )) {
143112 CLI ::write ('Both default shipped `env` file and custom `.env` are missing. ' , 'yellow ' );
144113 CLI ::write ('It is impossible to write the new environment type. ' , 'yellow ' );
145- CLI ::newLine ();
146114
147115 return false ;
148116 }
0 commit comments