comparison src/ConfigHelper.php @ 10:a9ff874afdbd

better comments for config helper
author luka
date Sat, 02 Dec 2023 10:20:32 -0500
parents d4730c14806d
children 3426c7e91c24
comparison
equal deleted inserted replaced
9:d4730c14806d 10:a9ff874afdbd
2 2
3 namespace Wizzard\MagicForger; 3 namespace Wizzard\MagicForger;
4 4
5 class ConfigHelper 5 class ConfigHelper
6 { 6 {
7 // Config array
7 public static $config = []; 8 public static $config = [];
9
10 // Config file name constant
8 public const CONFIG_FILE_NAME = 'mf_config.php'; 11 public const CONFIG_FILE_NAME = 'mf_config.php';
12
13 // Config path variable
9 public static $config_path; 14 public static $config_path;
10 15
16 /**
17 * Set up configuration path
18 *
19 * @param string $base_path
20 */
11 public static function setup_config_path(string $base_path) 21 public static function setup_config_path(string $base_path)
12 { 22 {
13 self::$config_path = $base_path.'/'.self::CONFIG_FILE_NAME; 23 self::$config_path = $base_path.'/'.self::CONFIG_FILE_NAME;
14 } 24 }
15 25
26 /**
27 * Get configuration path
28 *
29 * @return string
30 */
16 protected static function get_config_path() 31 protected static function get_config_path()
17 { 32 {
18 return self::$config_path; 33 return self::$config_path;
19 } 34 }
20 35
36 /**
37 * Write configuration into a file
38 */
21 public static function write_config() 39 public static function write_config()
22 { 40 {
23 $path = self::get_config_path(); 41 $path = self::get_config_path();
24 $str = '<?php 42 $str = '<?php
25 return '.self::varexport(self::$config, true).';'; 43 return '.self::varexport(self::$config, true).';';
26 44
27 file_put_contents($path, $str); 45 file_put_contents($path, $str);
46 // After writing the file, format it
28 self::format_file($path); 47 self::format_file($path);
29 } 48 }
30 49
50 /**
51 * Read configuration from a file
52 */
31 public static function read_config() 53 public static function read_config()
32 { 54 {
33 $path = self::get_config_path(); 55 $path = self::get_config_path();
34 self::$config = include $path; 56 self::$config = include $path;
35 } 57 }
36 58
59 /**
60 * Print configuration
61 */
37 public static function print_config() 62 public static function print_config()
38 { 63 {
39 self::varexport(self::$config); 64 self::varexport(self::$config);
40 } 65 }
41 66
42 /** 67 /**
43 * PHP var_export() with short array syntax (square brackets) indented 2 spaces. 68 * PHP var_export() function with short array syntax (square brackets) indented 2 spaces.
44 * 69 *
45 * NOTE: The only issue is when a string value has `=>\n[`, it will get converted to `=> [` 70 * NOTE: The only issue is when a string value has `=>\n[`, it will get converted to `=> [`
46 * 71 *
47 * @see https://www.php.net/manual/en/function.var-export.php 72 * @see https://www.php.net/manual/en/function.var-export.php
73 *
74 * @param mixed $expression
75 * @param boolean $return
76 * @return mixed
48 */ 77 */
49 public static function varexport($expression, $return = false) 78 public static function varexport($expression, $return = false)
50 { 79 {
51 $export = var_export($expression, true); 80 $export = var_export($expression, true);
52 $patterns = [ 81 $patterns = [
61 } else { 90 } else {
62 echo $export; 91 echo $export;
63 } 92 }
64 } 93 }
65 94
95 /**
96 * Format the given file
97 *
98 * @param string $path
99 */
66 protected static function format_file(string $path) 100 protected static function format_file(string $path)
67 { 101 {
68 exec('php-cs-fixer fix '.$path); 102 exec('php-cs-fixer fix '.$path);
69 } 103 }
70 104
105 /**
106 * Set up tables
107 */
71 public static function set_up_tables() 108 public static function set_up_tables()
72 { 109 {
73 $schema = \DB::connection()->getDoctrineSchemaManager(); 110 $schema = \DB::connection()->getDoctrineSchemaManager();
74 // get all the tables available in the db 111 // get all the tables available in the database
75 $tables = collect($schema->listTableNames())->all(); 112 $tables = collect($schema->listTableNames())->all();
76 113
77 $insert_tables = []; 114 $insert_tables = [];
78 foreach ($tables as $table) { 115 foreach ($tables as $table) {
79 $columns = []; 116 $columns = [];
97 134
98 $insert_tables[$table] = []; 135 $insert_tables[$table] = [];
99 $insert_tables[$table]['columns'] = $columns; 136 $insert_tables[$table]['columns'] = $columns;
100 $insert_tables[$table]['type'] = 'default'; 137 $insert_tables[$table]['type'] = 'default';
101 } 138 }
139 // Merge the new tables configuration into the initial config
102 self::merge_array_priority(self::$config['tables'], $insert_tables); 140 self::merge_array_priority(self::$config['tables'], $insert_tables);
103 141
104 return $tables; 142 return $tables;
105 } 143 }
106 144
145 /**
146 * Merge two arrays and ensure priority values do not get overwritten
147 *
148 * @param array $priority
149 * @param array $merged
150 */
107 private static function merge_array_priority(&$priority, $merged) 151 private static function merge_array_priority(&$priority, $merged)
108 { 152 {
109 foreach ($merged as $key => $value) { 153 foreach ($merged as $key => $value) {
110 // if the priority key is not set, automatically add the merged values 154 // if the priority key is not set, automatically add the merged values
111 if (!isset($priority[$key])) { 155 if (!isset($priority[$key])) {
112 $priority[$key] = $value; 156 $priority[$key] = $value;
113 } else { 157 } else {
114 // if the value is an array recursively merge 158 // if the value is an array recursively merge with priority
115 if (is_array($value) && is_array($priority[$key])) { 159 if (is_array($value) && is_array($priority[$key])) {
116 self::merge_array_priority($priority[$key], $value); 160 self::merge_array_priority($priority[$key], $value);
117 } 161 }
118 } 162 }
119 } 163 }