diff src/ConfigHelper.php @ 11:3426c7e91c24 main-dev

Modifying generaters and replacers
author luka
date Wed, 24 Apr 2024 19:53:42 -0400
parents a9ff874afdbd
children c969ed13c570
line wrap: on
line diff
--- a/src/ConfigHelper.php	Sat Dec 02 10:20:32 2023 -0500
+++ b/src/ConfigHelper.php	Wed Apr 24 19:53:42 2024 -0400
@@ -2,6 +2,8 @@
 
 namespace Wizzard\MagicForger;
 
+use Illuminate\Support\Collection;
+
 class ConfigHelper
 {
     // Config array
@@ -11,32 +13,32 @@
     public const CONFIG_FILE_NAME = 'mf_config.php';
 
     // Config path variable
-    public static $config_path;
+    public static string $config_path;
 
     /**
-     * Set up configuration path
-     *
-     * @param string $base_path
+     * Set up configuration path.
+     * @return void
      */
-    public static function setup_config_path(string $base_path)
+    public static function setup_config_path(string $base_path): void
     {
         self::$config_path = $base_path.'/'.self::CONFIG_FILE_NAME;
     }
 
     /**
-     * Get configuration path
+     * Get configuration path.
      *
      * @return string
      */
-    protected static function get_config_path()
+    protected static function get_config_path(): string
     {
         return self::$config_path;
     }
 
     /**
-     * Write configuration into a file
+     * Write configuration into a file.
+     * @return void
      */
-    public static function write_config()
+    public static function write_config(): void
     {
         $path = self::get_config_path();
         $str = '<?php
@@ -48,18 +50,20 @@
     }
 
     /**
-     * Read configuration from a file
+     * Read configuration from a file.
+     * @return void
      */
-    public static function read_config()
+    public static function read_config(): void
     {
         $path = self::get_config_path();
         self::$config = include $path;
     }
 
     /**
-     * Print configuration
+     * Print configuration.
+     * @return void
      */
-    public static function print_config()
+    public static function print_config(): void
     {
         self::varexport(self::$config);
     }
@@ -71,11 +75,10 @@
      *
      * @see https://www.php.net/manual/en/function.var-export.php
      *
-     * @param mixed $expression
-     * @param boolean $return
-     * @return mixed
+     * @param bool $return
+     * @return string|string[]|null
      */
-    public static function varexport($expression, $return = false)
+    public static function varexport(mixed $expression, $return = false): string|array|null
     {
         $export = var_export($expression, true);
         $patterns = [
@@ -90,31 +93,47 @@
         } else {
             echo $export;
         }
+				return null;
     }
 
     /**
-     * Format the given file
-     *
-     * @param string $path
+     * Format the given file.
+     * @return void
      */
-    protected static function format_file(string $path)
+    protected static function format_file(string $path): void
     {
         exec('php-cs-fixer fix '.$path);
     }
 
     /**
-     * Set up tables
+     * Set up tables.
      */
-    public static function set_up_tables()
+		public static function set_up_tables(): Collection	
     {
         $schema = \DB::connection()->getDoctrineSchemaManager();
-        // get all the tables available in the database
         $tables = collect($schema->listTableNames())->all();
+        $table_foreign_keys = [];
+        foreach ($tables as $table) {
+            $table_foreign_keys[$table] = $schema->listTableForeignKeys($table);
+        }
 
         $insert_tables = [];
         foreach ($tables as $table) {
             $columns = [];
-            $table_columns = $schema->introspectTable($table)->getColumns();
+            $table_columns = $schema->listTableColumns($table);
+
+            // Initiate new arrays for foreign keys
+            $foreign_keys = [];
+            $foreign_keys_reverse = [];
+
+            // Check foreign key references from this table
+            $foreign_keys_list = $table_foreign_keys[$table];
+            foreach ($foreign_keys_list as $fk) {
+                $foreign_keys[$fk->getLocalColumns()[0]] = [
+                            'foreign_table' => $fk->getForeignTableName(),
+                            'foreign_column' => $fk->getForeignColumns()[0],
+                          ];
+            }
 
             foreach ($table_columns as $column) {
                 $full_class = get_class($column->getType());
@@ -131,24 +150,40 @@
                                       ],
                                 ];
             }
-
+            // Check foreign key references to this table
+            foreach ($tables as $other_table) {
+                if ($other_table != $table) {
+                    $foreign_keys_list = $table_foreign_keys[$other_table];
+                    foreach ($foreign_keys_list as $fk) {
+                        if ($fk->getForeignTableName() == $table) {
+                            $foreign_keys_reverse[] = [
+                                               'table' => $other_table,
+                                               'column' => $fk->getLocalColumns()[0],
+                                          ];
+                        }
+                    }
+                }
+            }
             $insert_tables[$table] = [];
             $insert_tables[$table]['columns'] = $columns;
+            $insert_tables[$table]['foreign_keys'] = $foreign_keys; // Foreign keys FROM this table
+            $insert_tables[$table]['foreign_keys_reverse'] = $foreign_keys_reverse; // Foreign keys TO this table
             $insert_tables[$table]['type'] = 'default';
         }
-        // Merge the new tables configuration into the initial config
+
         self::merge_array_priority(self::$config['tables'], $insert_tables);
 
         return $tables;
     }
 
     /**
-     * Merge two arrays and ensure priority values do not get overwritten
+     * Merge two arrays and ensure priority values do not get overwritten.
      *
      * @param array $priority
      * @param array $merged
+     * @return void
      */
-    private static function merge_array_priority(&$priority, $merged)
+    private static function merge_array_priority(&$priority, $merged): void
     {
         foreach ($merged as $key => $value) {
             // if the priority key is not set, automatically add the merged values