1313
1414namespace CodeIgniter \Commands \Database ;
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 ;
20+ use CodeIgniter \CLI \Input \Option ;
1821use CodeIgniter \Config \Factories ;
19- use CodeIgniter \Database \SQLite3 \Connection ;
22+ use CodeIgniter \Database \SQLite3 \Connection as SQLite3Connection ;
2023use Config \Database ;
2124use Throwable ;
2225
2326/**
2427 * Creates a new database.
2528 */
26- class CreateDatabase extends BaseCommand
29+ #[Command(name: 'db:create ' , description: 'Create a new database schema. ' , group: 'Database ' )]
30+ class CreateDatabase extends AbstractCommand
2731{
2832 /**
29- * The group the command is lumped under
30- * when listing commands.
31- *
32- * @var string
33+ * @var list<string>
3334 */
34- protected $ group = ' Database ' ;
35+ private const VALID_EXTENSIONS = [ ' db ' , ' sqlite ' ] ;
3536
36- /**
37- * The Command's name
38- *
39- * @var string
40- */
41- protected $ name = 'db:create ' ;
37+ protected function configure (): void
38+ {
39+ $ this
40+ ->addArgument (new Argument (
41+ name: 'db_name ' ,
42+ description: 'The database name to use. ' ,
43+ required: true ,
44+ ))
45+ ->addOption (new Option (
46+ name: 'ext ' ,
47+ description: 'File extension of the database file for SQLite3. Can be `db` or `sqlite`. ' ,
48+ requiresValue: true ,
49+ default: 'db ' ,
50+ ));
51+ }
4252
43- /**
44- * the Command's short description
45- *
46- * @var string
47- */
48- protected $ description = 'Create a new database schema. ' ;
53+ protected function interact (array &$ arguments , array &$ options ): void
54+ {
55+ if ($ arguments === []) {
56+ $ arguments [] = CLI ::prompt ('Database name ' , null , 'required ' );
57+ }
4958
50- /**
51- * the Command's usage
52- *
53- * @var string
54- */
55- protected $ usage = 'db:create <db_name> [options] ' ;
59+ $ ext = $ this ->getUnboundOption ('ext ' , $ options );
5660
57- /**
58- * The Command's arguments
59- *
60- * @var array<string, string>
61- */
62- protected $ arguments = [
63- 'db_name ' => 'The database name to use ' ,
64- ];
65-
66- /**
67- * The Command's options
68- *
69- * @var array<string, string>
70- */
71- protected $ options = [
72- '--ext ' => 'File extension of the database file for SQLite3. Can be `db` or `sqlite`. Defaults to `db`. ' ,
73- ];
61+ if (is_string ($ ext ) && ! in_array ($ ext , self ::VALID_EXTENSIONS , true )) {
62+ $ options ['ext ' ] = CLI ::prompt ('Please choose a valid file extension ' , self ::VALID_EXTENSIONS , 'required ' );
63+ }
64+ }
7465
75- /**
76- * Creates a new database.
77- */
78- public function run (array $ params )
66+ protected function execute (array $ arguments , array $ options ): int
7967 {
80- $ name = array_shift ($ params );
81-
82- if (empty ($ name )) {
83- $ name = CLI ::prompt ('Database name ' , null , 'required ' ); // @codeCoverageIgnore
84- }
68+ $ name = $ arguments ['db_name ' ];
69+ assert (is_string ($ name ));
8570
8671 try {
8772 $ config = config (Database::class);
@@ -93,12 +78,14 @@ public function run(array $params)
9378
9479 $ db = Database ::connect ();
9580
96- // Special SQLite3 handling
97- if ( $ db instanceof Connection) {
98- $ ext = $ params [ ' ext ' ] ?? CLI :: getOption ( ' ext ' ) ?? ' db ' ;
81+ if ( $ db instanceof SQLite3Connection) {
82+ $ ext = $ options [ ' ext ' ];
83+ assert ( is_string ( $ ext)) ;
9984
100- if (! in_array ($ ext , ['db ' , 'sqlite ' ], true )) {
101- $ ext = CLI ::prompt ('Please choose a valid file extension ' , ['db ' , 'sqlite ' ]); // @codeCoverageIgnore
85+ if (! in_array ($ ext , self ::VALID_EXTENSIONS , true )) {
86+ CLI ::error (sprintf ('Invalid file extension "%s". Use either `db` or `sqlite`. ' , $ ext ), 'light_gray ' , 'red ' );
87+
88+ return EXIT_ERROR ;
10289 }
10390
10491 if ($ name !== ':memory: ' ) {
@@ -113,7 +100,6 @@ public function run(array $params)
113100
114101 if (is_file ($ dbName )) {
115102 CLI ::error ("Database \"{$ dbName }\" already exists. " , 'light_gray ' , 'red ' );
116- CLI ::newLine ();
117103
118104 return EXIT_ERROR ;
119105 }
@@ -128,26 +114,21 @@ public function run(array $params)
128114 if (! is_file ($ db ->getDatabase ()) && $ name !== ':memory: ' ) {
129115 // @codeCoverageIgnoreStart
130116 CLI ::error ('Database creation failed. ' , 'light_gray ' , 'red ' );
131- CLI ::newLine ();
132117
133118 return EXIT_ERROR ;
134119 // @codeCoverageIgnoreEnd
135120 }
136121 } elseif (! Database ::forge ()->createDatabase ($ name )) {
137- // @codeCoverageIgnoreStart
138122 CLI ::error ('Database creation failed. ' , 'light_gray ' , 'red ' );
139- CLI ::newLine ();
140123
141124 return EXIT_ERROR ;
142- // @codeCoverageIgnoreEnd
143125 }
144126
145127 CLI ::write ("Database \"{$ name }\" successfully created. " , 'green ' );
146- CLI ::newLine ();
147128
148129 return EXIT_SUCCESS ;
149130 } catch (Throwable $ e ) {
150- $ this ->showError ($ e );
131+ $ this ->renderThrowable ($ e );
151132
152133 return EXIT_ERROR ;
153134 } finally {
0 commit comments