diff 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
line wrap: on
line diff
--- a/src/Generator/BaseGenerator.php	Wed Feb 26 20:12:17 2025 -0500
+++ b/src/Generator/BaseGenerator.php	Fri Apr 11 20:50:20 2025 -0400
@@ -3,27 +3,30 @@
 namespace Wizard\MagicForger\Generator;
 
 use Illuminate\Console\GeneratorCommand;
+use Illuminate\Support\Facades\Schema;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
 use Wizard\MagicForger\Replacer\Replacer;
 use Wizard\MagicForger\Replacer\TableReplacer;
-use Illuminate\Support\Facades\DB;
-use InvalidArgumentException;
 
 abstract class BaseGenerator extends GeneratorCommand
 {
     use Replacer;
     use TableReplacer;
 
-    protected string $schema;
-    protected array $tables;
-    protected $currentTable;
+    protected $schema = null;
+
+    protected $tables = null;
+
+    protected $currentTable = null;
 
     public function handle()
     {
-        if (!$this->tableExists($this->getTableInput())) {
-            $this->components->error('The table: "' . $this->getTableInput() . '" does not exist in the database.');
+
+        if (! $this->tableExists($this->getTableInput())) {
+            $this->components->error('The table: "'.$this->getTableInput().'" does not exist in the database.');
+
             return false;
         }
 
@@ -44,7 +47,7 @@
         if (is_null($input->getArgument('table'))) {
             $prompted = true;
             $table = null;
-            while (null === $table) {
+            while ($table === null) {
                 $table = $this->components->askWithCompletion(
                     'What Table should we use?',
                     $this->possibleTables()
@@ -91,7 +94,7 @@
 
     protected function getFile($name): string
     {
-        if (!($this->hasOption('fresh') && $this->option('fresh')) && $this->fileExists($name)) {
+        if (! ($this->hasOption('fresh') && $this->option('fresh')) && $this->fileExists($name)) {
             return $this->files->get($name);
         }
 
@@ -116,26 +119,60 @@
     protected function getTables(): array
     {
         if (is_null($this->tables)) {
-            $this->tables = DB::connection()->getDoctrineSchemaManager()->listTableNames();
+            $this->tables = Schema::getTableListing(schema: config('database.connections.mariadb.database'), schemaQualified: false);
         }
 
         return $this->tables;
     }
-    
-    protected function getSchema()
-    {
-        if (is_null($this->schema)) {
-            $this->schema = DB::connection()->getDoctrineSchemaManager();
-        }
-
-        return $this->schema;
-    }
 
     protected function getTable(string $table_name)
     {
         return $this->getSchema()->introspectTable($table_name);
     }
 
+    /*
+     * returns array of columns in the form of:
+     *
+    [
+        "name" => "column_type"
+    "type_name" => "bigint"
+    "type" => "bigint(20) unsigned"
+    "collation" => null
+    "nullable" => true
+    "default" => "NULL"
+    "auto_increment" => false
+    "comment" => null
+    "generation" => null
+    ]
+    */
+    protected static function getTableColumns(string $table_name)
+    {
+        return Schema::getColumns($table_name);
+    }
+
+    /*
+     * returns array of foreign keys in the form of:
+     *
+     [
+        "name" => "foreign_key_name"
+        "columns" => [
+            0 => "local_column_name"
+        ]
+        "foreign_schema" => "schema_name"
+        "foreign_table" => "foreign_table_name"
+        "foreign_columns" => [
+            0 => "foreign_column_name"
+        ]
+        "on_update" => "restrict"
+        "on_delete" => "restrict"
+     ]
+     *
+     */
+    protected static function getTableForeignKeys(string $table_name)
+    {
+        return Schema::getForeignKeys($table_name);
+    }
+
     protected function getCurrentTable()
     {
         return $this->currentTable;
@@ -143,11 +180,11 @@
 
     protected function setCurrentTable(string $table_name): void
     {
-        $this->currentTable = !empty(trim($table_name)) ? $this->getTable($table_name) : null;
+        $this->currentTable = $table_name;
     }
 
     protected function format_file(string $path): void
     {
-        exec('php-cs-fixer fix ' . escapeshellarg($path));
+        exec('./vendor/bin/pint '.escapeshellarg($path));
     }
 }