Mercurial > packages > magicforger
annotate 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 |
| 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 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
5 class ConfigHelper |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
6 { |
| 10 | 7 // Config array |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
8 public static $config = []; |
| 10 | 9 |
| 10 // Config file name constant | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
11 public const CONFIG_FILE_NAME = 'mf_config.php'; |
| 10 | 12 |
| 13 // Config path variable | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
14 public static $config_path; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
15 |
| 10 | 16 /** |
| 17 * Set up configuration path | |
| 18 * | |
| 19 * @param string $base_path | |
| 20 */ | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
21 public static function setup_config_path(string $base_path) |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
22 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
23 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
|
24 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
25 |
| 10 | 26 /** |
| 27 * Get configuration path | |
| 28 * | |
| 29 * @return string | |
| 30 */ | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
31 protected static function get_config_path() |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
32 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
33 return self::$config_path; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
34 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
35 |
| 10 | 36 /** |
| 37 * Write configuration into a file | |
| 38 */ | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
39 public static function write_config() |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
40 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
41 $path = self::get_config_path(); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
42 $str = '<?php |
| 10 | 43 return '.self::varexport(self::$config, true).';'; |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
44 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
45 file_put_contents($path, $str); |
| 10 | 46 // After writing the file, format it |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
47 self::format_file($path); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
48 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
49 |
| 10 | 50 /** |
| 51 * Read configuration from a file | |
| 52 */ | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
53 public static function read_config() |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
54 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
55 $path = self::get_config_path(); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
56 self::$config = include $path; |
|
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 |
| 10 | 59 /** |
| 60 * Print configuration | |
| 61 */ | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
62 public static function print_config() |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
63 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
64 self::varexport(self::$config); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
65 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
66 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
67 /** |
| 10 | 68 * 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
|
69 * |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
70 * 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
|
71 * |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
72 * @see https://www.php.net/manual/en/function.var-export.php |
| 10 | 73 * |
| 74 * @param mixed $expression | |
| 75 * @param boolean $return | |
| 76 * @return mixed | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
77 */ |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
78 public static function varexport($expression, $return = false) |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
79 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
80 $export = var_export($expression, true); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
81 $patterns = [ |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
82 "/array \(/" => '[', |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
83 "/^([ ]*)\)(,?)$/m" => '$1]$2', |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
84 "/=>[ ]?\n[ ]+\[/" => '=> [', |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
85 "/([ ]*)(\'[^\']+\') => ([\[\'])/" => '$1$2 => $3', |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
86 ]; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
87 $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
|
88 if ((bool) $return) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
89 return $export; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
90 } else { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
91 echo $export; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
92 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
93 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
94 |
| 10 | 95 /** |
| 96 * Format the given file | |
| 97 * | |
| 98 * @param string $path | |
| 99 */ | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
100 protected static function format_file(string $path) |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
101 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
102 exec('php-cs-fixer fix '.$path); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
103 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
104 |
| 10 | 105 /** |
| 106 * Set up tables | |
| 107 */ | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
108 public static function set_up_tables() |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
109 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
110 $schema = \DB::connection()->getDoctrineSchemaManager(); |
| 10 | 111 // get all the tables available in the database |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
112 $tables = collect($schema->listTableNames())->all(); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
113 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
114 $insert_tables = []; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
115 foreach ($tables as $table) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
116 $columns = []; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
117 $table_columns = $schema->introspectTable($table)->getColumns(); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
118 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
119 foreach ($table_columns as $column) { |
| 9 | 120 $full_class = get_class($column->getType()); |
| 121 $class_parts = explode('\\', $full_class); | |
| 122 $class_name = end($class_parts); | |
| 123 | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
124 $columns[$column->getName()] = [ |
| 9 | 125 'type' => $class_name, |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
126 'should_insert' => [ |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
127 'controller' => true, |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
128 'model' => true, |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
129 'requests' => true, |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
130 'views' => true, |
| 9 | 131 ], |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
132 ]; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
133 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
134 |
| 9 | 135 $insert_tables[$table] = []; |
| 136 $insert_tables[$table]['columns'] = $columns; | |
| 137 $insert_tables[$table]['type'] = 'default'; | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
138 } |
| 10 | 139 // Merge the new tables configuration into the initial config |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
140 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
|
141 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
142 return $tables; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
143 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
144 |
| 10 | 145 /** |
| 146 * Merge two arrays and ensure priority values do not get overwritten | |
| 147 * | |
| 148 * @param array $priority | |
| 149 * @param array $merged | |
| 150 */ | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
151 private static function merge_array_priority(&$priority, $merged) |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
152 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
153 foreach ($merged as $key => $value) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
154 // 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
|
155 if (!isset($priority[$key])) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
156 $priority[$key] = $value; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
157 } else { |
| 10 | 158 // 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
|
159 if (is_array($value) && is_array($priority[$key])) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
160 self::merge_array_priority($priority[$key], $value); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
161 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
162 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
163 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
164 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
165 } |
