comparison src/Generator/BaseGenerator.php @ 21:f0b0d014e448 main-dev

Cleaning up code based on AI overlord review
author Luka Sitas <sitas.luka.97@gmail.com>
date Wed, 26 Feb 2025 19:45:08 -0500
parents 19b7a8de0019
children 827efbf4d73c
comparison
equal deleted inserted replaced
20:81a76472ba21 21:f0b0d014e448
1 <?php 1 <?php
2 2
3 namespace Wizard\MagicForger\Generator; 3 namespace Wizard\MagicForger\Generator;
4 4
5 use DB;
6 use Illuminate\Console\GeneratorCommand; 5 use Illuminate\Console\GeneratorCommand;
7 use Symfony\Component\Console\Input\InputInterface; 6 use Symfony\Component\Console\Input\InputInterface;
8 use Symfony\Component\Console\Input\InputOption; 7 use Symfony\Component\Console\Input\InputOption;
9 use Symfony\Component\Console\Output\OutputInterface; 8 use Symfony\Component\Console\Output\OutputInterface;
10 use Wizard\MagicForger\Replacer\Replacer; 9 use Wizard\MagicForger\Replacer\Replacer;
11 use Wizard\MagicForger\Replacer\TableReplacer; 10 use Wizard\MagicForger\Replacer\TableReplacer;
11 use Illuminate\Support\Facades\DB;
12 use InvalidArgumentException;
12 13
13 abstract class BaseGenerator extends GeneratorCommand 14 abstract class BaseGenerator extends GeneratorCommand
14 { 15 {
15 use Replacer; 16 use Replacer;
16 use TableReplacer; 17 use TableReplacer;
17 18
18 /** 19 protected string $schema;
19 * The schema of the database. 20 protected array $tables;
20 *
21 * @var string
22 */
23 protected $schema;
24
25 /**
26 * The tables available in the schema.
27 *
28 * @var array
29 */
30 protected $tables;
31
32 /**
33 * The current Table being used.
34 *
35 * @var table
36 */
37 protected $currentTable; 21 protected $currentTable;
38 22
39 /**
40 * Execute the console command.
41 */
42 public function handle() 23 public function handle()
43 { 24 {
44 // First we need to ensure that the table exists, then we can
45 if (!$this->tableExists($this->getTableInput())) { 25 if (!$this->tableExists($this->getTableInput())) {
46 $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.'); 26 $this->components->error('The table: "' . $this->getTableInput() . '" does not exist in the database.');
47
48 return false; 27 return false;
49 } 28 }
50 29
51 $this->setCurrentTable($this->getTableInput()); 30 $this->setCurrentTable($this->getTableInput());
52
53 $path = $this->getPath(); 31 $path = $this->getPath();
54
55 $file = $this->getFile($path); 32 $file = $this->getFile($path);
56
57 $file = $this->apply_replacements($file); 33 $file = $this->apply_replacements($file);
58
59 $file = $this->apply_inserts($file); 34 $file = $this->apply_inserts($file);
60
61 $this->makeDirectory($path); 35 $this->makeDirectory($path);
62
63 $this->files->put($path, $this->sortImports($file)); 36 $this->files->put($path, $this->sortImports($file));
64
65 $this->format_file($path); 37 $this->format_file($path);
66 38 $this->components->info(sprintf('%s [%s] created successfully.', $this->type, $path));
67 $info = $this->type;
68
69 $this->components->info(sprintf('%s [%s] created successfully.', $info, $path));
70 } 39 }
71 40
72 /** 41 protected function promptForMissingArguments(InputInterface $input, OutputInterface $output): void
73 * Override the original so that we can prompt for a table with autocomplete.
74 */
75 protected function promptForMissingArguments(InputInterface $input, OutputInterface $output)
76 { 42 {
77 $prompted = false; 43 $prompted = false;
78 if (is_null($input->getArgument('table'))) { 44 if (is_null($input->getArgument('table'))) {
79 $prompted = true; 45 $prompted = true;
80 $table = null; 46 $table = null;
82 $table = $this->components->askWithCompletion( 48 $table = $this->components->askWithCompletion(
83 'What Table should we use?', 49 'What Table should we use?',
84 $this->possibleTables() 50 $this->possibleTables()
85 ); 51 );
86 } 52 }
87
88 $input->setArgument('table', $table); 53 $input->setArgument('table', $table);
89 } 54 }
90 55
91 parent::promptForMissingArguments($input, $output); 56 parent::promptForMissingArguments($input, $output);
92 57
93 // This will get missed if we prompt here but not in the parent
94 if ($prompted) { 58 if ($prompted) {
95 $this->afterPromptingForMissingArguments($input, $output); 59 $this->afterPromptingForMissingArguments($input, $output);
96 } 60 }
97 } 61 }
98 62
99 /** 63 protected function getArguments(): array
100 * Get the console command arguments.
101 *
102 * @return array
103 */
104 protected function getArguments()
105 { 64 {
106 return [ 65 return [
107 ['table', InputOption::VALUE_REQUIRED, 'The table to generate files for.'], 66 ['table', InputOption::VALUE_REQUIRED, 'The table to generate files for.'],
108 ]; 67 ];
109 } 68 }
110 69
111 /** 70 protected function promptForMissingArgumentsUsing(): array
112 * Prompt for missing input arguments using the returned questions.
113 *
114 * @return array
115 */
116 protected function promptForMissingArgumentsUsing()
117 { 71 {
118 return [ 72 return [];
119 ];
120 } 73 }
121 74
122 /** 75 protected function getOptions(): array
123 * Get the console command options.
124 *
125 * @return array
126 */
127 protected function getOptions()
128 { 76 {
129 return [ 77 return [
130 ['fresh', 'f', InputOption::VALUE_NONE, 'Start from the stub or use existing if possible.'], 78 ['fresh', 'f', InputOption::VALUE_NONE, 'Start from the stub or use existing if possible.'],
131 ]; 79 ];
132 } 80 }
133 81
134 /** 82 protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output): void
135 * Interact further with the user if they were prompted for missing arguments.
136 *
137 * @return void
138 */
139 protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output)
140 { 83 {
84 // Additional logic after prompting goes here
141 } 85 }
142 86
143 /**
144 * Determines if the file exists.
145 */
146 protected function fileExists(string $path): bool 87 protected function fileExists(string $path): bool
147 { 88 {
148 return $this->files->exists($path); 89 return $this->files->exists($path);
149 } 90 }
150 91
151 /** 92 protected function getFile($name): string
152 * Gets the file that will be worked on. If there is already an existing file
153 * then we can open that. However if we are forcing the operation, then we
154 * will start with an empty stub.
155 */
156 protected function getFile($name)
157 { 93 {
158 if (!($this->hasOption('fresh') 94 if (!($this->hasOption('fresh') && $this->option('fresh')) && $this->fileExists($name)) {
159 && $this->option('fresh'))
160 && $this->fileExists($name)) {
161 // Working with an existing file
162 return $this->files->get($name); 95 return $this->files->get($name);
163 } 96 }
164 97
165 // Working with a stub
166 return $this->files->get($this->getStub()); 98 return $this->files->get($this->getStub());
167 } 99 }
168 100
169 /** 101 protected function getTableInput(): string
170 * Get the desired class table from the input.
171 *
172 * @return string
173 */
174 protected function getTableInput()
175 { 102 {
176 return trim($this->argument('table')); 103 return trim($this->argument('table'));
177 } 104 }
178 105
179 /**
180 * Determines if the table exists in the current database.
181 */
182 protected function tableExists(string $table_name): bool 106 protected function tableExists(string $table_name): bool
183 { 107 {
184 return in_array($table_name, $this->getTables()); 108 return in_array($table_name, $this->getTables());
185 } 109 }
186 110
187 /** 111 protected function possibleTables(): array
188 * Get a list of possible table names.
189 */
190 protected function possibleTables()
191 { 112 {
192 return $this->getTables(); 113 return $this->getTables();
193 } 114 }
194 115
195 /** 116 protected function getTables(): array
196 * Get the tables in the schema.
197 */
198 protected function getTables()
199 { 117 {
200 if (is_null($this->tables)) { 118 if (is_null($this->tables)) {
201 $this->tables = collect($this->getSchema()->listTableNames())->all(); 119 $this->tables = DB::connection()->getDoctrineSchemaManager()->listTableNames();
202 } 120 }
203 121
204 return $this->tables; 122 return $this->tables;
205 } 123 }
206 124
207 /**
208 * Get the database schema for DB interactions.
209 */
210 protected function getSchema() 125 protected function getSchema()
211 { 126 {
212 if (is_null($this->schema)) { 127 if (is_null($this->schema)) {
213 $this->schema = \DB::connection()->getDoctrineSchemaManager(); 128 $this->schema = DB::connection()->getDoctrineSchemaManager();
214 } 129 }
215 130
216 return $this->schema; 131 return $this->schema;
217 } 132 }
218 133
224 protected function getCurrentTable() 139 protected function getCurrentTable()
225 { 140 {
226 return $this->currentTable; 141 return $this->currentTable;
227 } 142 }
228 143
229 protected function setCurrentTable(string $table_name) 144 protected function setCurrentTable(string $table_name): void
230 { 145 {
231 $table = null; 146 $this->currentTable = !empty(trim($table_name)) ? $this->getTable($table_name) : null;
232 if (!is_null($table_name) && '' !== trim($table_name)) {
233 $table = $this->getTable($table_name);
234 }
235 $this->currentTable = $table;
236 } 147 }
237 148
238 protected function format_file(string $path) 149 protected function format_file(string $path): void
239 { 150 {
240 exec('php-cs-fixer fix '.$path); 151 exec('php-cs-fixer fix ' . escapeshellarg($path));
241 } 152 }
242 } 153 }