Mercurial > packages > magicforger
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 10:a9ff874afdbd | 11:3426c7e91c24 |
|---|---|
| 1 <?php | 1 <?php |
| 2 | 2 |
| 3 namespace Wizzard\MagicForger; | 3 namespace Wizzard\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 * | 20 * @return void |
| 19 * @param string $base_path | 21 */ |
| 20 */ | 22 public static function setup_config_path(string $base_path): void |
| 21 public static function setup_config_path(string $base_path) | |
| 22 { | 23 { |
| 23 self::$config_path = $base_path.'/'.self::CONFIG_FILE_NAME; | 24 self::$config_path = $base_path.'/'.self::CONFIG_FILE_NAME; |
| 24 } | 25 } |
| 25 | 26 |
| 26 /** | 27 /** |
| 27 * Get configuration path | 28 * Get configuration path. |
| 28 * | 29 * |
| 29 * @return string | 30 * @return string |
| 30 */ | 31 */ |
| 31 protected static function get_config_path() | 32 protected static function get_config_path(): string |
| 32 { | 33 { |
| 33 return self::$config_path; | 34 return self::$config_path; |
| 34 } | 35 } |
| 35 | 36 |
| 36 /** | 37 /** |
| 37 * Write configuration into a file | 38 * Write configuration into a file. |
| 38 */ | 39 * @return void |
| 39 public static function write_config() | 40 */ |
| 41 public static function write_config(): void | |
| 40 { | 42 { |
| 41 $path = self::get_config_path(); | 43 $path = self::get_config_path(); |
| 42 $str = '<?php | 44 $str = '<?php |
| 43 return '.self::varexport(self::$config, true).';'; | 45 return '.self::varexport(self::$config, true).';'; |
| 44 | 46 |
| 46 // After writing the file, format it | 48 // After writing the file, format it |
| 47 self::format_file($path); | 49 self::format_file($path); |
| 48 } | 50 } |
| 49 | 51 |
| 50 /** | 52 /** |
| 51 * Read configuration from a file | 53 * Read configuration from a file. |
| 52 */ | 54 * @return void |
| 53 public static function read_config() | 55 */ |
| 56 public static function read_config(): void | |
| 54 { | 57 { |
| 55 $path = self::get_config_path(); | 58 $path = self::get_config_path(); |
| 56 self::$config = include $path; | 59 self::$config = include $path; |
| 57 } | 60 } |
| 58 | 61 |
| 59 /** | 62 /** |
| 60 * Print configuration | 63 * Print configuration. |
| 61 */ | 64 * @return void |
| 62 public static function print_config() | 65 */ |
| 66 public static function print_config(): void | |
| 63 { | 67 { |
| 64 self::varexport(self::$config); | 68 self::varexport(self::$config); |
| 65 } | 69 } |
| 66 | 70 |
| 67 /** | 71 /** |
| 69 * | 73 * |
| 70 * NOTE: The only issue is when a string value has `=>\n[`, it will get converted to `=> [` | 74 * NOTE: The only issue is when a string value has `=>\n[`, it will get converted to `=> [` |
| 71 * | 75 * |
| 72 * @see https://www.php.net/manual/en/function.var-export.php | 76 * @see https://www.php.net/manual/en/function.var-export.php |
| 73 * | 77 * |
| 74 * @param mixed $expression | 78 * @param bool $return |
| 75 * @param boolean $return | 79 * @return string|string[]|null |
| 76 * @return mixed | 80 */ |
| 77 */ | 81 public static function varexport(mixed $expression, $return = false): string|array|null |
| 78 public static function varexport($expression, $return = false) | |
| 79 { | 82 { |
| 80 $export = var_export($expression, true); | 83 $export = var_export($expression, true); |
| 81 $patterns = [ | 84 $patterns = [ |
| 82 "/array \(/" => '[', | 85 "/array \(/" => '[', |
| 83 "/^([ ]*)\)(,?)$/m" => '$1]$2', | 86 "/^([ ]*)\)(,?)$/m" => '$1]$2', |
| 88 if ((bool) $return) { | 91 if ((bool) $return) { |
| 89 return $export; | 92 return $export; |
| 90 } else { | 93 } else { |
| 91 echo $export; | 94 echo $export; |
| 92 } | 95 } |
| 93 } | 96 return null; |
| 94 | 97 } |
| 95 /** | 98 |
| 96 * Format the given file | 99 /** |
| 97 * | 100 * Format the given file. |
| 98 * @param string $path | 101 * @return void |
| 99 */ | 102 */ |
| 100 protected static function format_file(string $path) | 103 protected static function format_file(string $path): void |
| 101 { | 104 { |
| 102 exec('php-cs-fixer fix '.$path); | 105 exec('php-cs-fixer fix '.$path); |
| 103 } | 106 } |
| 104 | 107 |
| 105 /** | 108 /** |
| 106 * Set up tables | 109 * Set up tables. |
| 107 */ | 110 */ |
| 108 public static function set_up_tables() | 111 public static function set_up_tables(): Collection |
| 109 { | 112 { |
| 110 $schema = \DB::connection()->getDoctrineSchemaManager(); | 113 $schema = \DB::connection()->getDoctrineSchemaManager(); |
| 111 // get all the tables available in the database | |
| 112 $tables = collect($schema->listTableNames())->all(); | 114 $tables = collect($schema->listTableNames())->all(); |
| 115 $table_foreign_keys = []; | |
| 116 foreach ($tables as $table) { | |
| 117 $table_foreign_keys[$table] = $schema->listTableForeignKeys($table); | |
| 118 } | |
| 113 | 119 |
| 114 $insert_tables = []; | 120 $insert_tables = []; |
| 115 foreach ($tables as $table) { | 121 foreach ($tables as $table) { |
| 116 $columns = []; | 122 $columns = []; |
| 117 $table_columns = $schema->introspectTable($table)->getColumns(); | 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 } | |
| 118 | 137 |
| 119 foreach ($table_columns as $column) { | 138 foreach ($table_columns as $column) { |
| 120 $full_class = get_class($column->getType()); | 139 $full_class = get_class($column->getType()); |
| 121 $class_parts = explode('\\', $full_class); | 140 $class_parts = explode('\\', $full_class); |
| 122 $class_name = end($class_parts); | 141 $class_name = end($class_parts); |
| 129 'requests' => true, | 148 'requests' => true, |
| 130 'views' => true, | 149 'views' => true, |
| 131 ], | 150 ], |
| 132 ]; | 151 ]; |
| 133 } | 152 } |
| 134 | 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 } | |
| 135 $insert_tables[$table] = []; | 167 $insert_tables[$table] = []; |
| 136 $insert_tables[$table]['columns'] = $columns; | 168 $insert_tables[$table]['columns'] = $columns; |
| 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 | |
| 137 $insert_tables[$table]['type'] = 'default'; | 171 $insert_tables[$table]['type'] = 'default'; |
| 138 } | 172 } |
| 139 // Merge the new tables configuration into the initial config | 173 |
| 140 self::merge_array_priority(self::$config['tables'], $insert_tables); | 174 self::merge_array_priority(self::$config['tables'], $insert_tables); |
| 141 | 175 |
| 142 return $tables; | 176 return $tables; |
| 143 } | 177 } |
| 144 | 178 |
| 145 /** | 179 /** |
| 146 * Merge two arrays and ensure priority values do not get overwritten | 180 * Merge two arrays and ensure priority values do not get overwritten. |
| 147 * | 181 * |
| 148 * @param array $priority | 182 * @param array $priority |
| 149 * @param array $merged | 183 * @param array $merged |
| 150 */ | 184 * @return void |
| 151 private static function merge_array_priority(&$priority, $merged) | 185 */ |
| 186 private static function merge_array_priority(&$priority, $merged): void | |
| 152 { | 187 { |
| 153 foreach ($merged as $key => $value) { | 188 foreach ($merged as $key => $value) { |
| 154 // if the priority key is not set, automatically add the merged values | 189 // if the priority key is not set, automatically add the merged values |
| 155 if (!isset($priority[$key])) { | 190 if (!isset($priority[$key])) { |
| 156 $priority[$key] = $value; | 191 $priority[$key] = $value; |
