comparison src/Generator/BaseGenerator.php @ 23:827efbf4d73c main-dev

Huge changes to the relationships for models and more complex
author Luka Sitas <sitas.luka.97@gmail.com>
date Fri, 11 Apr 2025 20:50:20 -0400
parents f0b0d014e448
children 555bfaa500ac
comparison
equal deleted inserted replaced
22:ee8ef14e158d 23:827efbf4d73c
1 <?php 1 <?php
2 2
3 namespace Wizard\MagicForger\Generator; 3 namespace Wizard\MagicForger\Generator;
4 4
5 use Illuminate\Console\GeneratorCommand; 5 use Illuminate\Console\GeneratorCommand;
6 use Illuminate\Support\Facades\Schema;
6 use Symfony\Component\Console\Input\InputInterface; 7 use Symfony\Component\Console\Input\InputInterface;
7 use Symfony\Component\Console\Input\InputOption; 8 use Symfony\Component\Console\Input\InputOption;
8 use Symfony\Component\Console\Output\OutputInterface; 9 use Symfony\Component\Console\Output\OutputInterface;
9 use Wizard\MagicForger\Replacer\Replacer; 10 use Wizard\MagicForger\Replacer\Replacer;
10 use Wizard\MagicForger\Replacer\TableReplacer; 11 use Wizard\MagicForger\Replacer\TableReplacer;
11 use Illuminate\Support\Facades\DB;
12 use InvalidArgumentException;
13 12
14 abstract class BaseGenerator extends GeneratorCommand 13 abstract class BaseGenerator extends GeneratorCommand
15 { 14 {
16 use Replacer; 15 use Replacer;
17 use TableReplacer; 16 use TableReplacer;
18 17
19 protected string $schema; 18 protected $schema = null;
20 protected array $tables; 19
21 protected $currentTable; 20 protected $tables = null;
21
22 protected $currentTable = null;
22 23
23 public function handle() 24 public function handle()
24 { 25 {
25 if (!$this->tableExists($this->getTableInput())) { 26
26 $this->components->error('The table: "' . $this->getTableInput() . '" does not exist in the database.'); 27 if (! $this->tableExists($this->getTableInput())) {
28 $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.');
29
27 return false; 30 return false;
28 } 31 }
29 32
30 $this->setCurrentTable($this->getTableInput()); 33 $this->setCurrentTable($this->getTableInput());
31 $path = $this->getPath(); 34 $path = $this->getPath();
42 { 45 {
43 $prompted = false; 46 $prompted = false;
44 if (is_null($input->getArgument('table'))) { 47 if (is_null($input->getArgument('table'))) {
45 $prompted = true; 48 $prompted = true;
46 $table = null; 49 $table = null;
47 while (null === $table) { 50 while ($table === null) {
48 $table = $this->components->askWithCompletion( 51 $table = $this->components->askWithCompletion(
49 'What Table should we use?', 52 'What Table should we use?',
50 $this->possibleTables() 53 $this->possibleTables()
51 ); 54 );
52 } 55 }
89 return $this->files->exists($path); 92 return $this->files->exists($path);
90 } 93 }
91 94
92 protected function getFile($name): string 95 protected function getFile($name): string
93 { 96 {
94 if (!($this->hasOption('fresh') && $this->option('fresh')) && $this->fileExists($name)) { 97 if (! ($this->hasOption('fresh') && $this->option('fresh')) && $this->fileExists($name)) {
95 return $this->files->get($name); 98 return $this->files->get($name);
96 } 99 }
97 100
98 return $this->files->get($this->getStub()); 101 return $this->files->get($this->getStub());
99 } 102 }
114 } 117 }
115 118
116 protected function getTables(): array 119 protected function getTables(): array
117 { 120 {
118 if (is_null($this->tables)) { 121 if (is_null($this->tables)) {
119 $this->tables = DB::connection()->getDoctrineSchemaManager()->listTableNames(); 122 $this->tables = Schema::getTableListing(schema: config('database.connections.mariadb.database'), schemaQualified: false);
120 } 123 }
121 124
122 return $this->tables; 125 return $this->tables;
123 }
124
125 protected function getSchema()
126 {
127 if (is_null($this->schema)) {
128 $this->schema = DB::connection()->getDoctrineSchemaManager();
129 }
130
131 return $this->schema;
132 } 126 }
133 127
134 protected function getTable(string $table_name) 128 protected function getTable(string $table_name)
135 { 129 {
136 return $this->getSchema()->introspectTable($table_name); 130 return $this->getSchema()->introspectTable($table_name);
131 }
132
133 /*
134 * returns array of columns in the form of:
135 *
136 [
137 "name" => "column_type"
138 "type_name" => "bigint"
139 "type" => "bigint(20) unsigned"
140 "collation" => null
141 "nullable" => true
142 "default" => "NULL"
143 "auto_increment" => false
144 "comment" => null
145 "generation" => null
146 ]
147 */
148 protected static function getTableColumns(string $table_name)
149 {
150 return Schema::getColumns($table_name);
151 }
152
153 /*
154 * returns array of foreign keys in the form of:
155 *
156 [
157 "name" => "foreign_key_name"
158 "columns" => [
159 0 => "local_column_name"
160 ]
161 "foreign_schema" => "schema_name"
162 "foreign_table" => "foreign_table_name"
163 "foreign_columns" => [
164 0 => "foreign_column_name"
165 ]
166 "on_update" => "restrict"
167 "on_delete" => "restrict"
168 ]
169 *
170 */
171 protected static function getTableForeignKeys(string $table_name)
172 {
173 return Schema::getForeignKeys($table_name);
137 } 174 }
138 175
139 protected function getCurrentTable() 176 protected function getCurrentTable()
140 { 177 {
141 return $this->currentTable; 178 return $this->currentTable;
142 } 179 }
143 180
144 protected function setCurrentTable(string $table_name): void 181 protected function setCurrentTable(string $table_name): void
145 { 182 {
146 $this->currentTable = !empty(trim($table_name)) ? $this->getTable($table_name) : null; 183 $this->currentTable = $table_name;
147 } 184 }
148 185
149 protected function format_file(string $path): void 186 protected function format_file(string $path): void
150 { 187 {
151 exec('php-cs-fixer fix ' . escapeshellarg($path)); 188 exec('./vendor/bin/pint '.escapeshellarg($path));
152 } 189 }
153 } 190 }