Mercurial > packages > magicforger
comparison src/ConfigHelper.php @ 34:f65ab84ee47f default
merge with codex
| author | luka |
|---|---|
| date | Wed, 10 Sep 2025 21:00:47 -0400 |
| parents | 827efbf4d73c |
| children |
comparison
equal
deleted
inserted
replaced
| 10:a9ff874afdbd | 34:f65ab84ee47f |
|---|---|
| 1 <?php | 1 <?php |
| 2 | 2 |
| 3 namespace Wizzard\MagicForger; | 3 namespace Wizard\MagicForger; |
| 4 | |
| 5 use Illuminate\Support\Collection; | |
| 4 | 6 |
| 5 class ConfigHelper | 7 class ConfigHelper |
| 6 { | 8 { |
| 7 // Config array | 9 // Config array |
| 8 public static $config = []; | 10 public static $config = []; |
| 9 | 11 |
| 10 // Config file name constant | 12 // Config file name constant |
| 11 public const CONFIG_FILE_NAME = 'mf_config.php'; | 13 public const CONFIG_FILE_NAME = 'mf_config.php'; |
| 12 | 14 |
| 13 // Config path variable | 15 // Config path variable |
| 14 public static $config_path; | 16 public static string $config_path; |
| 15 | 17 |
| 16 /** | 18 /** |
| 17 * Set up configuration path | 19 * Set up configuration path. |
| 18 * | |
| 19 * @param string $base_path | |
| 20 */ | 20 */ |
| 21 public static function setup_config_path(string $base_path) | 21 public static function setup_config_path(string $base_path): void |
| 22 { | 22 { |
| 23 self::$config_path = $base_path.'/'.self::CONFIG_FILE_NAME; | 23 self::$config_path = $base_path.'/'.self::CONFIG_FILE_NAME; |
| 24 } | 24 } |
| 25 | 25 |
| 26 /** | 26 /** |
| 27 * Get configuration path | 27 * Get configuration path. |
| 28 * | |
| 29 * @return string | |
| 30 */ | 28 */ |
| 31 protected static function get_config_path() | 29 protected static function get_config_path(): string |
| 32 { | 30 { |
| 33 return self::$config_path; | 31 return self::$config_path; |
| 34 } | 32 } |
| 35 | 33 |
| 36 /** | 34 /** |
| 37 * Write configuration into a file | 35 * Write configuration into a file. |
| 38 */ | 36 */ |
| 39 public static function write_config() | 37 public static function write_config(): void |
| 40 { | 38 { |
| 41 $path = self::get_config_path(); | 39 $path = self::get_config_path(); |
| 42 $str = '<?php | 40 $str = '<?php |
| 43 return '.self::varexport(self::$config, true).';'; | 41 return '.self::varexport(self::$config, true).';'; |
| 44 | 42 |
| 46 // After writing the file, format it | 44 // After writing the file, format it |
| 47 self::format_file($path); | 45 self::format_file($path); |
| 48 } | 46 } |
| 49 | 47 |
| 50 /** | 48 /** |
| 51 * Read configuration from a file | 49 * Read configuration from a file. |
| 52 */ | 50 */ |
| 53 public static function read_config() | 51 public static function read_config(): void |
| 54 { | 52 { |
| 55 $path = self::get_config_path(); | 53 $path = self::get_config_path(); |
| 56 self::$config = include $path; | 54 self::$config = include $path; |
| 57 } | 55 } |
| 58 | 56 |
| 59 /** | 57 /** |
| 60 * Print configuration | 58 * Print configuration. |
| 61 */ | 59 */ |
| 62 public static function print_config() | 60 public static function print_config(): void |
| 63 { | 61 { |
| 64 self::varexport(self::$config); | 62 self::varexport(self::$config); |
| 65 } | 63 } |
| 66 | 64 |
| 67 /** | 65 /** |
| 69 * | 67 * |
| 70 * NOTE: The only issue is when a string value has `=>\n[`, it will get converted to `=> [` | 68 * NOTE: The only issue is when a string value has `=>\n[`, it will get converted to `=> [` |
| 71 * | 69 * |
| 72 * @see https://www.php.net/manual/en/function.var-export.php | 70 * @see https://www.php.net/manual/en/function.var-export.php |
| 73 * | 71 * |
| 74 * @param mixed $expression | 72 * @param bool $return |
| 75 * @param boolean $return | 73 * @return string|string[]|null |
| 76 * @return mixed | |
| 77 */ | 74 */ |
| 78 public static function varexport($expression, $return = false) | 75 public static function varexport(mixed $expression, $return = false): string|array|null |
| 79 { | 76 { |
| 80 $export = var_export($expression, true); | 77 $export = var_export($expression, true); |
| 81 $patterns = [ | 78 $patterns = [ |
| 82 "/array \(/" => '[', | 79 "/array \(/" => '[', |
| 83 "/^([ ]*)\)(,?)$/m" => '$1]$2', | 80 "/^([ ]*)\)(,?)$/m" => '$1]$2', |
| 88 if ((bool) $return) { | 85 if ((bool) $return) { |
| 89 return $export; | 86 return $export; |
| 90 } else { | 87 } else { |
| 91 echo $export; | 88 echo $export; |
| 92 } | 89 } |
| 90 | |
| 91 return null; | |
| 93 } | 92 } |
| 94 | 93 |
| 95 /** | 94 /** |
| 96 * Format the given file | 95 * Format the given file. |
| 97 * | |
| 98 * @param string $path | |
| 99 */ | 96 */ |
| 100 protected static function format_file(string $path) | 97 protected static function format_file(string $path): void |
| 101 { | 98 { |
| 102 exec('php-cs-fixer fix '.$path); | 99 exec('php-cs-fixer fix '.$path); |
| 103 } | 100 } |
| 104 | 101 |
| 105 /** | 102 /** |
| 106 * Set up tables | 103 * Set up tables. |
| 107 */ | 104 */ |
| 108 public static function set_up_tables() | 105 public static function set_up_tables(): Collection |
| 109 { | 106 { |
| 110 $schema = \DB::connection()->getDoctrineSchemaManager(); | 107 $schema = \DB::connection()->getDoctrineSchemaManager(); |
| 111 // get all the tables available in the database | |
| 112 $tables = collect($schema->listTableNames())->all(); | 108 $tables = collect($schema->listTableNames())->all(); |
| 109 $table_foreign_keys = []; | |
| 110 foreach ($tables as $table) { | |
| 111 $table_foreign_keys[$table] = $schema->listTableForeignKeys($table); | |
| 112 } | |
| 113 | 113 |
| 114 $insert_tables = []; | 114 $insert_tables = []; |
| 115 foreach ($tables as $table) { | 115 foreach ($tables as $table) { |
| 116 $columns = []; | 116 $columns = []; |
| 117 $table_columns = $schema->introspectTable($table)->getColumns(); | 117 $table_columns = $schema->listTableColumns($table); |
| 118 | |
| 119 // Initiate new arrays for foreign keys | |
| 120 $foreign_keys = []; | |
| 121 $foreign_keys_reverse = []; | |
| 122 | |
| 123 // Check foreign key references from this table | |
| 124 $foreign_keys_list = $table_foreign_keys[$table]; | |
| 125 foreach ($foreign_keys_list as $fk) { | |
| 126 $foreign_keys[$fk->getLocalColumns()[0]] = [ | |
| 127 'foreign_table' => $fk->getForeignTableName(), | |
| 128 'foreign_column' => $fk->getForeignColumns()[0], | |
| 129 ]; | |
| 130 } | |
| 118 | 131 |
| 119 foreach ($table_columns as $column) { | 132 foreach ($table_columns as $column) { |
| 120 $full_class = get_class($column->getType()); | 133 $full_class = get_class($column->getType()); |
| 121 $class_parts = explode('\\', $full_class); | 134 $class_parts = explode('\\', $full_class); |
| 122 $class_name = end($class_parts); | 135 $class_name = end($class_parts); |
| 123 | 136 |
| 124 $columns[$column->getName()] = [ | 137 $columns[$column->getName()] = [ |
| 125 'type' => $class_name, | 138 'type' => $class_name, |
| 126 'should_insert' => [ | 139 'should_insert' => [ |
| 127 'controller' => true, | 140 'controller' => true, |
| 128 'model' => true, | 141 'model' => true, |
| 129 'requests' => true, | 142 'requests' => true, |
| 130 'views' => true, | 143 'views' => true, |
| 131 ], | 144 ], |
| 132 ]; | 145 ]; |
| 133 } | 146 } |
| 134 | 147 // Check foreign key references to this table |
| 148 foreach ($tables as $other_table) { | |
| 149 if ($other_table != $table) { | |
| 150 $foreign_keys_list = $table_foreign_keys[$other_table]; | |
| 151 foreach ($foreign_keys_list as $fk) { | |
| 152 if ($fk->getForeignTableName() == $table) { | |
| 153 $foreign_keys_reverse[] = [ | |
| 154 'table' => $other_table, | |
| 155 'column' => $fk->getLocalColumns()[0], | |
| 156 ]; | |
| 157 } | |
| 158 } | |
| 159 } | |
| 160 } | |
| 135 $insert_tables[$table] = []; | 161 $insert_tables[$table] = []; |
| 136 $insert_tables[$table]['columns'] = $columns; | 162 $insert_tables[$table]['columns'] = $columns; |
| 163 $insert_tables[$table]['foreign_keys'] = $foreign_keys; // Foreign keys FROM this table | |
| 164 $insert_tables[$table]['foreign_keys_reverse'] = $foreign_keys_reverse; // Foreign keys TO this table | |
| 137 $insert_tables[$table]['type'] = 'default'; | 165 $insert_tables[$table]['type'] = 'default'; |
| 138 } | 166 } |
| 139 // Merge the new tables configuration into the initial config | 167 |
| 140 self::merge_array_priority(self::$config['tables'], $insert_tables); | 168 self::merge_array_priority(self::$config['tables'], $insert_tables); |
| 141 | 169 |
| 142 return $tables; | 170 return $tables; |
| 143 } | 171 } |
| 144 | 172 |
| 145 /** | 173 /** |
| 146 * Merge two arrays and ensure priority values do not get overwritten | 174 * Merge two arrays and ensure priority values do not get overwritten. |
| 147 * | 175 * |
| 148 * @param array $priority | 176 * @param array $priority |
| 149 * @param array $merged | 177 * @param array $merged |
| 150 */ | 178 */ |
| 151 private static function merge_array_priority(&$priority, $merged) | 179 private static function merge_array_priority(&$priority, $merged): void |
| 152 { | 180 { |
| 153 foreach ($merged as $key => $value) { | 181 foreach ($merged as $key => $value) { |
| 154 // if the priority key is not set, automatically add the merged values | 182 // if the priority key is not set, automatically add the merged values |
| 155 if (!isset($priority[$key])) { | 183 if (! isset($priority[$key])) { |
| 156 $priority[$key] = $value; | 184 $priority[$key] = $value; |
| 157 } else { | 185 } else { |
| 158 // if the value is an array recursively merge with priority | 186 // if the value is an array recursively merge with priority |
| 159 if (is_array($value) && is_array($priority[$key])) { | 187 if (is_array($value) && is_array($priority[$key])) { |
| 160 self::merge_array_priority($priority[$key], $value); | 188 self::merge_array_priority($priority[$key], $value); |
