Mercurial > packages > magicforger
annotate 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 |
| 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 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
3 namespace Wizzard\MagicForger; |
|
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. |
| 20 * @return void | |
| 10 | 21 */ |
| 11 | 22 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
|
23 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
24 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
|
25 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
26 |
| 10 | 27 /** |
| 11 | 28 * Get configuration path. |
| 10 | 29 * |
| 30 * @return string | |
| 31 */ | |
| 11 | 32 protected static function get_config_path(): string |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
33 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
34 return self::$config_path; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
35 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
36 |
| 10 | 37 /** |
| 11 | 38 * Write configuration into a file. |
| 39 * @return void | |
| 10 | 40 */ |
| 11 | 41 public static function write_config(): void |
|
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 $path = self::get_config_path(); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
44 $str = '<?php |
| 10 | 45 return '.self::varexport(self::$config, true).';'; |
|
8
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 file_put_contents($path, $str); |
| 10 | 48 // After writing the file, format it |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
49 self::format_file($path); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
50 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
51 |
| 10 | 52 /** |
| 11 | 53 * Read configuration from a file. |
| 54 * @return void | |
| 10 | 55 */ |
| 11 | 56 public static function read_config(): void |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
57 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
58 $path = self::get_config_path(); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
59 self::$config = include $path; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
60 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
61 |
| 10 | 62 /** |
| 11 | 63 * Print configuration. |
| 64 * @return void | |
| 10 | 65 */ |
| 11 | 66 public static function print_config(): void |
|
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 self::varexport(self::$config); |
|
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 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
71 /** |
| 10 | 72 * 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
|
73 * |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
74 * 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
|
75 * |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
76 * @see https://www.php.net/manual/en/function.var-export.php |
| 10 | 77 * |
| 11 | 78 * @param bool $return |
| 79 * @return string|string[]|null | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
80 */ |
| 11 | 81 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
|
82 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
83 $export = var_export($expression, true); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
84 $patterns = [ |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
85 "/array \(/" => '[', |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
86 "/^([ ]*)\)(,?)$/m" => '$1]$2', |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
87 "/=>[ ]?\n[ ]+\[/" => '=> [', |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
88 "/([ ]*)(\'[^\']+\') => ([\[\'])/" => '$1$2 => $3', |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
89 ]; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
90 $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
|
91 if ((bool) $return) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
92 return $export; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
93 } else { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
94 echo $export; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
95 } |
| 11 | 96 return null; |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
97 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
98 |
| 10 | 99 /** |
| 11 | 100 * Format the given file. |
| 101 * @return void | |
| 10 | 102 */ |
| 11 | 103 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
|
104 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
105 exec('php-cs-fixer fix '.$path); |
|
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 |
| 10 | 108 /** |
| 11 | 109 * Set up tables. |
| 10 | 110 */ |
| 11 | 111 public static function set_up_tables(): Collection |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
112 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
113 $schema = \DB::connection()->getDoctrineSchemaManager(); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
114 $tables = collect($schema->listTableNames())->all(); |
| 11 | 115 $table_foreign_keys = []; |
| 116 foreach ($tables as $table) { | |
| 117 $table_foreign_keys[$table] = $schema->listTableForeignKeys($table); | |
| 118 } | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
119 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
120 $insert_tables = []; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
121 foreach ($tables as $table) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
122 $columns = []; |
| 11 | 123 $table_columns = $schema->listTableColumns($table); |
| 124 | |
| 125 // Initiate new arrays for foreign keys | |
| 126 $foreign_keys = []; | |
| 127 $foreign_keys_reverse = []; | |
| 128 | |
| 129 // Check foreign key references from this table | |
| 130 $foreign_keys_list = $table_foreign_keys[$table]; | |
| 131 foreach ($foreign_keys_list as $fk) { | |
| 132 $foreign_keys[$fk->getLocalColumns()[0]] = [ | |
| 133 'foreign_table' => $fk->getForeignTableName(), | |
| 134 'foreign_column' => $fk->getForeignColumns()[0], | |
| 135 ]; | |
| 136 } | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
137 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
138 foreach ($table_columns as $column) { |
| 9 | 139 $full_class = get_class($column->getType()); |
| 140 $class_parts = explode('\\', $full_class); | |
| 141 $class_name = end($class_parts); | |
| 142 | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
143 $columns[$column->getName()] = [ |
| 9 | 144 'type' => $class_name, |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
145 'should_insert' => [ |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
146 'controller' => true, |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
147 'model' => true, |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
148 'requests' => true, |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
149 'views' => true, |
| 9 | 150 ], |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
151 ]; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
152 } |
| 11 | 153 // Check foreign key references to this table |
| 154 foreach ($tables as $other_table) { | |
| 155 if ($other_table != $table) { | |
| 156 $foreign_keys_list = $table_foreign_keys[$other_table]; | |
| 157 foreach ($foreign_keys_list as $fk) { | |
| 158 if ($fk->getForeignTableName() == $table) { | |
| 159 $foreign_keys_reverse[] = [ | |
| 160 'table' => $other_table, | |
| 161 'column' => $fk->getLocalColumns()[0], | |
| 162 ]; | |
| 163 } | |
| 164 } | |
| 165 } | |
| 166 } | |
| 9 | 167 $insert_tables[$table] = []; |
| 168 $insert_tables[$table]['columns'] = $columns; | |
| 11 | 169 $insert_tables[$table]['foreign_keys'] = $foreign_keys; // Foreign keys FROM this table |
| 170 $insert_tables[$table]['foreign_keys_reverse'] = $foreign_keys_reverse; // Foreign keys TO this table | |
| 9 | 171 $insert_tables[$table]['type'] = 'default'; |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
172 } |
| 11 | 173 |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
174 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
|
175 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
176 return $tables; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
177 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
178 |
| 10 | 179 /** |
| 11 | 180 * Merge two arrays and ensure priority values do not get overwritten. |
| 10 | 181 * |
| 182 * @param array $priority | |
| 183 * @param array $merged | |
| 11 | 184 * @return void |
| 10 | 185 */ |
| 11 | 186 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
|
187 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
188 foreach ($merged as $key => $value) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
189 // if the priority key is not set, automatically add the merged values |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
190 if (!isset($priority[$key])) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
191 $priority[$key] = $value; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
192 } else { |
| 10 | 193 // 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
|
194 if (is_array($value) && is_array($priority[$key])) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
195 self::merge_array_priority($priority[$key], $value); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
196 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
197 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
198 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
199 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
200 } |
