Mercurial > packages > magicforger
comparison src/ConfigHelper.php @ 14:c969ed13c570 main-dev
Changes for various files
| author | luka |
|---|---|
| date | Mon, 23 Sep 2024 20:35:14 -0400 |
| parents | 3426c7e91c24 |
| children | 19b7a8de0019 |
comparison
equal
deleted
inserted
replaced
| 13:7ee152c22478 | 14:c969ed13c570 |
|---|---|
| 15 // Config path variable | 15 // Config path variable |
| 16 public static string $config_path; | 16 public static string $config_path; |
| 17 | 17 |
| 18 /** | 18 /** |
| 19 * Set up configuration path. | 19 * Set up configuration path. |
| 20 * @return void | |
| 21 */ | 20 */ |
| 22 public static function setup_config_path(string $base_path): void | 21 public static function setup_config_path(string $base_path): void |
| 23 { | 22 { |
| 24 self::$config_path = $base_path.'/'.self::CONFIG_FILE_NAME; | 23 self::$config_path = $base_path.'/'.self::CONFIG_FILE_NAME; |
| 25 } | 24 } |
| 26 | 25 |
| 27 /** | 26 /** |
| 28 * Get configuration path. | 27 * Get configuration path. |
| 29 * | |
| 30 * @return string | |
| 31 */ | 28 */ |
| 32 protected static function get_config_path(): string | 29 protected static function get_config_path(): string |
| 33 { | 30 { |
| 34 return self::$config_path; | 31 return self::$config_path; |
| 35 } | 32 } |
| 36 | 33 |
| 37 /** | 34 /** |
| 38 * Write configuration into a file. | 35 * Write configuration into a file. |
| 39 * @return void | |
| 40 */ | 36 */ |
| 41 public static function write_config(): void | 37 public static function write_config(): void |
| 42 { | 38 { |
| 43 $path = self::get_config_path(); | 39 $path = self::get_config_path(); |
| 44 $str = '<?php | 40 $str = '<?php |
| 49 self::format_file($path); | 45 self::format_file($path); |
| 50 } | 46 } |
| 51 | 47 |
| 52 /** | 48 /** |
| 53 * Read configuration from a file. | 49 * Read configuration from a file. |
| 54 * @return void | |
| 55 */ | 50 */ |
| 56 public static function read_config(): void | 51 public static function read_config(): void |
| 57 { | 52 { |
| 58 $path = self::get_config_path(); | 53 $path = self::get_config_path(); |
| 59 self::$config = include $path; | 54 self::$config = include $path; |
| 60 } | 55 } |
| 61 | 56 |
| 62 /** | 57 /** |
| 63 * Print configuration. | 58 * Print configuration. |
| 64 * @return void | |
| 65 */ | 59 */ |
| 66 public static function print_config(): void | 60 public static function print_config(): void |
| 67 { | 61 { |
| 68 self::varexport(self::$config); | 62 self::varexport(self::$config); |
| 69 } | 63 } |
| 74 * 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 `=> [` |
| 75 * | 69 * |
| 76 * @see https://www.php.net/manual/en/function.var-export.php | 70 * @see https://www.php.net/manual/en/function.var-export.php |
| 77 * | 71 * |
| 78 * @param bool $return | 72 * @param bool $return |
| 73 * | |
| 79 * @return string|string[]|null | 74 * @return string|string[]|null |
| 80 */ | 75 */ |
| 81 public static function varexport(mixed $expression, $return = false): string|array|null | 76 public static function varexport(mixed $expression, $return = false): string|array|null |
| 82 { | 77 { |
| 83 $export = var_export($expression, true); | 78 $export = var_export($expression, true); |
| 91 if ((bool) $return) { | 86 if ((bool) $return) { |
| 92 return $export; | 87 return $export; |
| 93 } else { | 88 } else { |
| 94 echo $export; | 89 echo $export; |
| 95 } | 90 } |
| 96 return null; | 91 |
| 92 return null; | |
| 97 } | 93 } |
| 98 | 94 |
| 99 /** | 95 /** |
| 100 * Format the given file. | 96 * Format the given file. |
| 101 * @return void | |
| 102 */ | 97 */ |
| 103 protected static function format_file(string $path): void | 98 protected static function format_file(string $path): void |
| 104 { | 99 { |
| 105 exec('php-cs-fixer fix '.$path); | 100 exec('php-cs-fixer fix '.$path); |
| 106 } | 101 } |
| 107 | 102 |
| 108 /** | 103 /** |
| 109 * Set up tables. | 104 * Set up tables. |
| 110 */ | 105 */ |
| 111 public static function set_up_tables(): Collection | 106 public static function set_up_tables(): Collection |
| 112 { | 107 { |
| 113 $schema = \DB::connection()->getDoctrineSchemaManager(); | 108 $schema = \DB::connection()->getDoctrineSchemaManager(); |
| 114 $tables = collect($schema->listTableNames())->all(); | 109 $tables = collect($schema->listTableNames())->all(); |
| 115 $table_foreign_keys = []; | 110 $table_foreign_keys = []; |
| 116 foreach ($tables as $table) { | 111 foreach ($tables as $table) { |
| 179 /** | 174 /** |
| 180 * Merge two arrays and ensure priority values do not get overwritten. | 175 * Merge two arrays and ensure priority values do not get overwritten. |
| 181 * | 176 * |
| 182 * @param array $priority | 177 * @param array $priority |
| 183 * @param array $merged | 178 * @param array $merged |
| 184 * @return void | |
| 185 */ | 179 */ |
| 186 private static function merge_array_priority(&$priority, $merged): void | 180 private static function merge_array_priority(&$priority, $merged): void |
| 187 { | 181 { |
| 188 foreach ($merged as $key => $value) { | 182 foreach ($merged as $key => $value) { |
| 189 // if the priority key is not set, automatically add the merged values | 183 // if the priority key is not set, automatically add the merged values |
