Mercurial > packages > magicforger
annotate src/ConfigHelper.php @ 40:2cf26b593f4a ls_dev_2025_09 tip
better support for different column types
| author | Luka Sitas <sitas.luka.97@gmail.com> |
|---|---|
| date | Thu, 16 Oct 2025 10:54:04 -0400 |
| parents | 827efbf4d73c |
| children |
| rev | line source |
|---|---|
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
1 <?php |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
2 |
|
19
19b7a8de0019
updating namespace from typo
Luka Sitas <sitas.luka.97@gmail.com>
parents:
14
diff
changeset
|
3 namespace Wizard\MagicForger; |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
4 |
| 11 | 5 use Illuminate\Support\Collection; |
| 6 | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
7 class ConfigHelper |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
8 { |
| 10 | 9 // Config array |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
10 public static $config = []; |
| 10 | 11 |
| 12 // Config file name constant | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
13 public const CONFIG_FILE_NAME = 'mf_config.php'; |
| 10 | 14 |
| 15 // Config path variable | |
| 11 | 16 public static string $config_path; |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
17 |
| 10 | 18 /** |
| 11 | 19 * Set up configuration path. |
| 10 | 20 */ |
| 11 | 21 public static function setup_config_path(string $base_path): void |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
22 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
23 self::$config_path = $base_path.'/'.self::CONFIG_FILE_NAME; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
24 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
25 |
| 10 | 26 /** |
| 11 | 27 * Get configuration path. |
| 10 | 28 */ |
| 11 | 29 protected static function get_config_path(): string |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
30 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
31 return self::$config_path; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
32 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
33 |
| 10 | 34 /** |
| 11 | 35 * Write configuration into a file. |
| 10 | 36 */ |
| 11 | 37 public static function write_config(): void |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
38 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
39 $path = self::get_config_path(); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
40 $str = '<?php |
| 10 | 41 return '.self::varexport(self::$config, true).';'; |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
42 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
43 file_put_contents($path, $str); |
| 10 | 44 // After writing the file, format it |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
45 self::format_file($path); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
46 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
47 |
| 10 | 48 /** |
| 11 | 49 * Read configuration from a file. |
| 10 | 50 */ |
| 11 | 51 public static function read_config(): void |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
52 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
53 $path = self::get_config_path(); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
54 self::$config = include $path; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
55 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
56 |
| 10 | 57 /** |
| 11 | 58 * Print configuration. |
| 10 | 59 */ |
| 11 | 60 public static function print_config(): void |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
61 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
62 self::varexport(self::$config); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
63 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
64 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
65 /** |
| 10 | 66 * PHP var_export() function with short array syntax (square brackets) indented 2 spaces. |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
67 * |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
68 * NOTE: The only issue is when a string value has `=>\n[`, it will get converted to `=> [` |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
69 * |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
70 * @see https://www.php.net/manual/en/function.var-export.php |
| 10 | 71 * |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
72 * @param bool $return |
| 11 | 73 * @return string|string[]|null |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
74 */ |
| 11 | 75 public static function varexport(mixed $expression, $return = false): string|array|null |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
76 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
77 $export = var_export($expression, true); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
78 $patterns = [ |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
79 "/array \(/" => '[', |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
80 "/^([ ]*)\)(,?)$/m" => '$1]$2', |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
81 "/=>[ ]?\n[ ]+\[/" => '=> [', |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
82 "/([ ]*)(\'[^\']+\') => ([\[\'])/" => '$1$2 => $3', |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
83 ]; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
84 $export = preg_replace(array_keys($patterns), array_values($patterns), $export); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
85 if ((bool) $return) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
86 return $export; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
87 } else { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
88 echo $export; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
89 } |
| 14 | 90 |
| 91 return null; | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
92 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
93 |
| 10 | 94 /** |
| 11 | 95 * Format the given file. |
| 10 | 96 */ |
| 11 | 97 protected static function format_file(string $path): void |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
98 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
99 exec('php-cs-fixer fix '.$path); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
100 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
101 |
| 10 | 102 /** |
| 11 | 103 * Set up tables. |
| 10 | 104 */ |
| 14 | 105 public static function set_up_tables(): Collection |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
106 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
107 $schema = \DB::connection()->getDoctrineSchemaManager(); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
108 $tables = collect($schema->listTableNames())->all(); |
| 11 | 109 $table_foreign_keys = []; |
| 110 foreach ($tables as $table) { | |
| 111 $table_foreign_keys[$table] = $schema->listTableForeignKeys($table); | |
| 112 } | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
113 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
114 $insert_tables = []; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
115 foreach ($tables as $table) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
116 $columns = []; |
| 11 | 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]] = [ | |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
127 'foreign_table' => $fk->getForeignTableName(), |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
128 'foreign_column' => $fk->getForeignColumns()[0], |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
129 ]; |
| 11 | 130 } |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
131 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
132 foreach ($table_columns as $column) { |
| 9 | 133 $full_class = get_class($column->getType()); |
| 134 $class_parts = explode('\\', $full_class); | |
| 135 $class_name = end($class_parts); | |
| 136 | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
137 $columns[$column->getName()] = [ |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
138 'type' => $class_name, |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
139 'should_insert' => [ |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
140 'controller' => true, |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
141 'model' => true, |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
142 'requests' => true, |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
143 'views' => true, |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
144 ], |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
145 ]; |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
146 } |
| 11 | 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[] = [ | |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
154 'table' => $other_table, |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
155 'column' => $fk->getLocalColumns()[0], |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
156 ]; |
| 11 | 157 } |
| 158 } | |
| 159 } | |
| 160 } | |
| 9 | 161 $insert_tables[$table] = []; |
| 162 $insert_tables[$table]['columns'] = $columns; | |
| 11 | 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 | |
| 9 | 165 $insert_tables[$table]['type'] = 'default'; |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
166 } |
| 11 | 167 |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
168 self::merge_array_priority(self::$config['tables'], $insert_tables); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
169 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
170 return $tables; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
171 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
172 |
| 10 | 173 /** |
| 11 | 174 * Merge two arrays and ensure priority values do not get overwritten. |
| 10 | 175 * |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
176 * @param array $priority |
|
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
177 * @param array $merged |
| 10 | 178 */ |
| 11 | 179 private static function merge_array_priority(&$priority, $merged): void |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
180 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
181 foreach ($merged as $key => $value) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
182 // if the priority key is not set, automatically add the merged values |
|
23
827efbf4d73c
Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents:
19
diff
changeset
|
183 if (! isset($priority[$key])) { |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
184 $priority[$key] = $value; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
185 } else { |
| 10 | 186 // if the value is an array recursively merge with priority |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
187 if (is_array($value) && is_array($priority[$key])) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
188 self::merge_array_priority($priority[$key], $value); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
189 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
190 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
191 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
192 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
193 } |
