Mercurial > packages > magicforger
comparison src/ConfigHelper.php @ 8:4216c0dc638c
Added base php cs fixer added config helper to maintain config
| author | luka |
|---|---|
| date | Wed, 18 Oct 2023 21:04:32 -0400 |
| parents | |
| children | d4730c14806d |
comparison
equal
deleted
inserted
replaced
| 7:769a17898cc0 | 8:4216c0dc638c |
|---|---|
| 1 <?php | |
| 2 | |
| 3 namespace Wizzard\MagicForger; | |
| 4 | |
| 5 class ConfigHelper | |
| 6 { | |
| 7 public static $config = []; | |
| 8 public const CONFIG_FILE_NAME = 'mf_config.php'; | |
| 9 public static $config_path; | |
| 10 | |
| 11 public static function setup_config_path(string $base_path) | |
| 12 { | |
| 13 self::$config_path = $base_path.'/'.self::CONFIG_FILE_NAME; | |
| 14 } | |
| 15 | |
| 16 protected static function get_config_path() | |
| 17 { | |
| 18 return self::$config_path; | |
| 19 } | |
| 20 | |
| 21 public static function write_config() | |
| 22 { | |
| 23 $path = self::get_config_path(); | |
| 24 $str = '<?php | |
| 25 return '.self::varexport(self::$config, true).';'; | |
| 26 | |
| 27 file_put_contents($path, $str); | |
| 28 self::format_file($path); | |
| 29 } | |
| 30 | |
| 31 public static function read_config() | |
| 32 { | |
| 33 $path = self::get_config_path(); | |
| 34 self::$config = include $path; | |
| 35 } | |
| 36 | |
| 37 public static function print_config() | |
| 38 { | |
| 39 self::varexport(self::$config); | |
| 40 } | |
| 41 | |
| 42 /** | |
| 43 * PHP var_export() with short array syntax (square brackets) indented 2 spaces. | |
| 44 * | |
| 45 * NOTE: The only issue is when a string value has `=>\n[`, it will get converted to `=> [` | |
| 46 * | |
| 47 * @see https://www.php.net/manual/en/function.var-export.php | |
| 48 */ | |
| 49 public static function varexport($expression, $return = false) | |
| 50 { | |
| 51 $export = var_export($expression, true); | |
| 52 $patterns = [ | |
| 53 "/array \(/" => '[', | |
| 54 "/^([ ]*)\)(,?)$/m" => '$1]$2', | |
| 55 "/=>[ ]?\n[ ]+\[/" => '=> [', | |
| 56 "/([ ]*)(\'[^\']+\') => ([\[\'])/" => '$1$2 => $3', | |
| 57 ]; | |
| 58 $export = preg_replace(array_keys($patterns), array_values($patterns), $export); | |
| 59 if ((bool) $return) { | |
| 60 return $export; | |
| 61 } else { | |
| 62 echo $export; | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 protected static function format_file(string $path) | |
| 67 { | |
| 68 exec('php-cs-fixer fix '.$path); | |
| 69 } | |
| 70 | |
| 71 public static function set_up_tables() | |
| 72 { | |
| 73 $schema = \DB::connection()->getDoctrineSchemaManager(); | |
| 74 // get all the tables available in the db | |
| 75 $tables = collect($schema->listTableNames())->all(); | |
| 76 | |
| 77 $insert_tables = []; | |
| 78 foreach ($tables as $table) { | |
| 79 $columns = []; | |
| 80 $table_columns = $schema->introspectTable($table)->getColumns(); | |
| 81 | |
| 82 foreach ($table_columns as $column) { | |
| 83 $columns[$column->getName()] = [ | |
| 84 'should_insert' => [ | |
| 85 'controller' => true, | |
| 86 'model' => true, | |
| 87 'requests' => true, | |
| 88 'views' => true, | |
| 89 ], | |
| 90 ]; | |
| 91 } | |
| 92 | |
| 93 $insert_tables[$table] = $columns; | |
| 94 | |
| 95 } | |
| 96 self::merge_array_priority(self::$config['tables'], $insert_tables); | |
| 97 | |
| 98 return $tables; | |
| 99 } | |
| 100 | |
| 101 private static function merge_array_priority(&$priority, $merged) | |
| 102 { | |
| 103 foreach ($merged as $key => $value) { | |
| 104 // if the priority key is not set, automatically add the merged values | |
| 105 if (!isset($priority[$key])) { | |
| 106 $priority[$key] = $value; | |
| 107 } else { | |
| 108 // if the value is an array recursively merge | |
| 109 if (is_array($value) && is_array($priority[$key])) { | |
| 110 self::merge_array_priority($priority[$key], $value); | |
| 111 } | |
| 112 } | |
| 113 } | |
| 114 } | |
| 115 } |
