|
13 | 13 |
|
14 | 14 | namespace CodeIgniter\Commands\Generators; |
15 | 15 |
|
16 | | -use CodeIgniter\CLI\BaseCommand; |
17 | | -use CodeIgniter\CLI\CLI; |
18 | | -use CodeIgniter\CLI\GeneratorTrait; |
| 16 | +use CodeIgniter\CLI\AbstractGeneratorCommand; |
| 17 | +use CodeIgniter\CLI\Attributes\Command; |
| 18 | +use CodeIgniter\CLI\Attributes\GeneratorCommand; |
| 19 | +use CodeIgniter\CLI\Input\Option; |
19 | 20 | use Config\Database; |
20 | 21 | use Config\Migrations; |
21 | 22 | use Config\Session as SessionConfig; |
22 | 23 |
|
23 | | -/** |
24 | | - * Generates a skeleton migration file. |
25 | | - */ |
26 | | -class MigrationGenerator extends BaseCommand |
| 24 | +#[Command(name: 'make:migration', description: 'Generates a new migration file.', group: 'Generators')] |
| 25 | +#[GeneratorCommand( |
| 26 | + component: 'Migration', |
| 27 | + template: 'migration.tpl.php', |
| 28 | + directory: 'Database\Migrations', |
| 29 | + classNameLang: 'CLI.generator.className.migration', |
| 30 | +)] |
| 31 | +class MigrationGenerator extends AbstractGeneratorCommand |
27 | 32 | { |
28 | | - use GeneratorTrait; |
29 | | - |
30 | | - /** |
31 | | - * The Command's Group |
32 | | - * |
33 | | - * @var string |
34 | | - */ |
35 | | - protected $group = 'Generators'; |
36 | | - |
37 | | - /** |
38 | | - * The Command's Name |
39 | | - * |
40 | | - * @var string |
41 | | - */ |
42 | | - protected $name = 'make:migration'; |
43 | | - |
44 | | - /** |
45 | | - * The Command's Description |
46 | | - * |
47 | | - * @var string |
48 | | - */ |
49 | | - protected $description = 'Generates a new migration file.'; |
50 | | - |
51 | | - /** |
52 | | - * The Command's Usage |
53 | | - * |
54 | | - * @var string |
55 | | - */ |
56 | | - protected $usage = 'make:migration <name> [options]'; |
57 | | - |
58 | | - /** |
59 | | - * The Command's Arguments |
60 | | - * |
61 | | - * @var array<string, string> |
62 | | - */ |
63 | | - protected $arguments = [ |
64 | | - 'name' => 'The migration class name.', |
65 | | - ]; |
66 | | - |
67 | | - /** |
68 | | - * The Command's Options |
69 | | - * |
70 | | - * @var array<string, string> |
71 | | - */ |
72 | | - protected $options = [ |
73 | | - '--session' => 'Generates the migration file for database sessions.', |
74 | | - '--table' => 'Table name to use for database sessions. Default: "ci_sessions".', |
75 | | - '--dbgroup' => 'Database group to use for database sessions. Default: "default".', |
76 | | - '--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".', |
77 | | - '--suffix' => 'Append the component title to the class name (e.g. User => UserMigration).', |
78 | | - ]; |
| 33 | + protected function configure(): void |
| 34 | + { |
| 35 | + parent::configure(); |
| 36 | + |
| 37 | + $this |
| 38 | + ->addOption(new Option( |
| 39 | + name: 'session', |
| 40 | + description: 'Generate the migration file for database sessions.', |
| 41 | + )) |
| 42 | + ->addOption(new Option( |
| 43 | + name: 'table', |
| 44 | + shortcut: 't', |
| 45 | + description: 'Table name to use for database sessions.', |
| 46 | + requiresValue: true, |
| 47 | + default: 'ci_sessions', |
| 48 | + )) |
| 49 | + ->addOption(new Option( |
| 50 | + name: 'dbgroup', |
| 51 | + shortcut: 'g', |
| 52 | + description: 'Database group to use for database sessions.', |
| 53 | + requiresValue: true, |
| 54 | + valueLabel: 'group', |
| 55 | + default: 'default', |
| 56 | + )); |
| 57 | + } |
79 | 58 |
|
80 | | - /** |
81 | | - * Actually execute a command. |
82 | | - */ |
83 | | - public function run(array $params) |
| 59 | + protected function provideGeneratorOptions(): void |
84 | 60 | { |
85 | | - $this->component = 'Migration'; |
86 | | - $this->directory = 'Database\Migrations'; |
87 | | - $this->template = 'migration.tpl.php'; |
| 61 | + $this->addNamespaceOption()->addSuffixOption(); |
| 62 | + } |
88 | 63 |
|
89 | | - if (array_key_exists('session', $params) || CLI::getOption('session')) { |
90 | | - $table = $params['table'] ?? CLI::getOption('table') ?? 'ci_sessions'; |
91 | | - $params[0] = "_create_{$table}_table"; |
| 64 | + protected function initialize(array &$arguments, array &$options): void |
| 65 | + { |
| 66 | + if (! $this->hasUnboundOption('session', $options)) { |
| 67 | + return; |
92 | 68 | } |
93 | 69 |
|
94 | | - $this->classNameLang = 'CLI.generator.className.migration'; |
95 | | - $this->generateClass($params); |
| 70 | + $table = $this->getUnboundOption('table', $options); |
96 | 71 |
|
97 | | - return EXIT_SUCCESS; |
| 72 | + $arguments[0] = sprintf('_create_%s_table', is_string($table) ? $table : 'ci_sessions'); |
98 | 73 | } |
99 | 74 |
|
100 | | - /** |
101 | | - * Prepare options and do the necessary replacements. |
102 | | - */ |
103 | | - protected function prepare(string $class): string |
| 75 | + protected function getTemplateData(string $class): array |
104 | 76 | { |
105 | | - $data = []; |
106 | | - $data['session'] = false; |
107 | | - |
108 | | - if ($this->getOption('session')) { |
109 | | - $table = $this->getOption('table'); |
110 | | - $DBGroup = $this->getOption('dbgroup'); |
111 | | - |
112 | | - $data['session'] = true; |
113 | | - $data['table'] = is_string($table) ? $table : 'ci_sessions'; |
114 | | - $data['DBGroup'] = is_string($DBGroup) ? $DBGroup : 'default'; |
115 | | - $data['DBDriver'] = config(Database::class)->{$data['DBGroup']}['DBDriver']; |
116 | | - |
117 | | - $data['matchIP'] = config(SessionConfig::class)->matchIP; |
| 77 | + if ($this->getValidatedOption('session') !== true) { |
| 78 | + return ['session' => false]; |
118 | 79 | } |
119 | 80 |
|
120 | | - return $this->parseTemplate($class, [], [], $data); |
| 81 | + $group = $this->getValidatedOption('dbgroup'); |
| 82 | + assert(is_string($group)); |
| 83 | + |
| 84 | + return [ |
| 85 | + 'session' => true, |
| 86 | + 'table' => $this->getValidatedOption('table'), |
| 87 | + 'DBGroup' => $group, |
| 88 | + 'DBDriver' => config(Database::class)->{$group}['DBDriver'], |
| 89 | + 'matchIP' => config(SessionConfig::class)->matchIP, |
| 90 | + ]; |
121 | 91 | } |
122 | 92 |
|
123 | | - /** |
124 | | - * Change file basename before saving. |
125 | | - */ |
126 | 93 | protected function basename(string $filename): string |
127 | 94 | { |
128 | 95 | return gmdate(config(Migrations::class)->timestampFormat) . basename($filename); |
|
0 commit comments