Mercurial > packages > magicforger
annotate 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 |
| rev | line source |
|---|---|
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
1 <?php |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
2 |
|
19
19b7a8de0019
updating namespace from typo
Luka Sitas <sitas.luka.97@gmail.com>
parents:
9
diff
changeset
|
3 namespace Wizard\MagicForger\Generator; |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
4 |
|
3
6468684362c2
It works! Created a controller, no update insert but it works
luka
parents:
2
diff
changeset
|
5 use Illuminate\Console\GeneratorCommand; |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
6 use Symfony\Component\Console\Input\InputInterface; |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
7 use Symfony\Component\Console\Input\InputOption; |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
8 use Symfony\Component\Console\Output\OutputInterface; |
|
19
19b7a8de0019
updating namespace from typo
Luka Sitas <sitas.luka.97@gmail.com>
parents:
9
diff
changeset
|
9 use Wizard\MagicForger\Replacer\Replacer; |
|
19b7a8de0019
updating namespace from typo
Luka Sitas <sitas.luka.97@gmail.com>
parents:
9
diff
changeset
|
10 use Wizard\MagicForger\Replacer\TableReplacer; |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
11 use Illuminate\Support\Facades\DB; |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
12 use InvalidArgumentException; |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
13 |
|
3
6468684362c2
It works! Created a controller, no update insert but it works
luka
parents:
2
diff
changeset
|
14 abstract class BaseGenerator extends GeneratorCommand |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
15 { |
|
3
6468684362c2
It works! Created a controller, no update insert but it works
luka
parents:
2
diff
changeset
|
16 use Replacer; |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
17 use TableReplacer; |
| 2 | 18 |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
19 protected string $schema; |
|
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
20 protected array $tables; |
| 5 | 21 protected $currentTable; |
| 22 | |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
23 public function handle() |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
24 { |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
25 if (!$this->tableExists($this->getTableInput())) { |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
26 $this->components->error('The table: "' . $this->getTableInput() . '" does not exist in the database.'); |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
27 return false; |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
28 } |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
29 |
| 9 | 30 $this->setCurrentTable($this->getTableInput()); |
| 4 | 31 $path = $this->getPath(); |
| 32 $file = $this->getFile($path); | |
| 33 $file = $this->apply_replacements($file); | |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
34 $file = $this->apply_inserts($file); |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
35 $this->makeDirectory($path); |
| 4 | 36 $this->files->put($path, $this->sortImports($file)); |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
37 $this->format_file($path); |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
38 $this->components->info(sprintf('%s [%s] created successfully.', $this->type, $path)); |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
39 } |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
40 |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
41 protected function promptForMissingArguments(InputInterface $input, OutputInterface $output): void |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
42 { |
|
3
6468684362c2
It works! Created a controller, no update insert but it works
luka
parents:
2
diff
changeset
|
43 $prompted = false; |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
44 if (is_null($input->getArgument('table'))) { |
|
3
6468684362c2
It works! Created a controller, no update insert but it works
luka
parents:
2
diff
changeset
|
45 $prompted = true; |
| 2 | 46 $table = null; |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
47 while (null === $table) { |
| 2 | 48 $table = $this->components->askWithCompletion( |
| 49 'What Table should we use?', | |
| 50 $this->possibleTables() | |
| 51 ); | |
| 52 } | |
| 53 $input->setArgument('table', $table); | |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
54 } |
|
3
6468684362c2
It works! Created a controller, no update insert but it works
luka
parents:
2
diff
changeset
|
55 |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
56 parent::promptForMissingArguments($input, $output); |
|
3
6468684362c2
It works! Created a controller, no update insert but it works
luka
parents:
2
diff
changeset
|
57 |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
58 if ($prompted) { |
|
3
6468684362c2
It works! Created a controller, no update insert but it works
luka
parents:
2
diff
changeset
|
59 $this->afterPromptingForMissingArguments($input, $output); |
|
6468684362c2
It works! Created a controller, no update insert but it works
luka
parents:
2
diff
changeset
|
60 } |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
61 } |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
62 |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
63 protected function getArguments(): array |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
64 { |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
65 return [ |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
66 ['table', InputOption::VALUE_REQUIRED, 'The table to generate files for.'], |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
67 ]; |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
68 } |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
69 |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
70 protected function promptForMissingArgumentsUsing(): array |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
71 { |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
72 return []; |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
73 } |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
74 |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
75 protected function getOptions(): array |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
76 { |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
77 return [ |
| 4 | 78 ['fresh', 'f', InputOption::VALUE_NONE, 'Start from the stub or use existing if possible.'], |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
79 ]; |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
80 } |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
81 |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
82 protected function afterPromptingForMissingArguments(InputInterface $input, OutputInterface $output): void |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
83 { |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
84 // Additional logic after prompting goes here |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
85 } |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
86 |
| 4 | 87 protected function fileExists(string $path): bool |
| 88 { | |
| 89 return $this->files->exists($path); | |
| 90 } | |
| 91 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
92 protected function getFile($name): string |
| 2 | 93 { |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
94 if (!($this->hasOption('fresh') && $this->option('fresh')) && $this->fileExists($name)) { |
| 4 | 95 return $this->files->get($name); |
| 2 | 96 } |
| 97 | |
| 98 return $this->files->get($this->getStub()); | |
| 99 } | |
| 100 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
101 protected function getTableInput(): string |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
102 { |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
103 return trim($this->argument('table')); |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
104 } |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
105 |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
106 protected function tableExists(string $table_name): bool |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
107 { |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
108 return in_array($table_name, $this->getTables()); |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
109 } |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
110 |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
111 protected function possibleTables(): array |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
112 { |
| 2 | 113 return $this->getTables(); |
| 114 } | |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
115 |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
116 protected function getTables(): array |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
117 { |
| 2 | 118 if (is_null($this->tables)) { |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
119 $this->tables = DB::connection()->getDoctrineSchemaManager()->listTableNames(); |
| 2 | 120 } |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
121 |
| 2 | 122 return $this->tables; |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
123 } |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
124 |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
125 protected function getSchema() |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
126 { |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
127 if (is_null($this->schema)) { |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
128 $this->schema = DB::connection()->getDoctrineSchemaManager(); |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
129 } |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
130 |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
131 return $this->schema; |
|
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
132 } |
| 5 | 133 |
| 134 protected function getTable(string $table_name) | |
| 135 { | |
| 136 return $this->getSchema()->introspectTable($table_name); | |
| 137 } | |
| 138 | |
| 139 protected function getCurrentTable() | |
| 140 { | |
| 141 return $this->currentTable; | |
| 142 } | |
| 143 | |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
144 protected function setCurrentTable(string $table_name): void |
| 5 | 145 { |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
146 $this->currentTable = !empty(trim($table_name)) ? $this->getTable($table_name) : null; |
| 5 | 147 } |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
148 |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
149 protected function format_file(string $path): void |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
150 { |
|
21
f0b0d014e448
Cleaning up code based on AI overlord review
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
151 exec('php-cs-fixer fix ' . escapeshellarg($path)); |
|
7
769a17898cc0
Various changes to the generators and replacers - probably mostly just formatting
luka
parents:
5
diff
changeset
|
152 } |
|
1
ca36acd2bef2
Have a base going, there is definitly a lot wrong with some of the files and the general structure but overall, it's a starting point
luka
parents:
diff
changeset
|
153 } |
