Mercurial > packages > magicforger
annotate src/ConfigHelper.php @ 9:d4730c14806d
Small changes to config generation and table name input
| author | luka |
|---|---|
| date | Sat, 02 Dec 2023 10:18:52 -0500 |
| parents | 4216c0dc638c |
| children | a9ff874afdbd |
| 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 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
7 public static $config = []; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
8 public const CONFIG_FILE_NAME = 'mf_config.php'; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
9 public static $config_path; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
10 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
11 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
|
12 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
13 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
|
14 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
15 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
16 protected static function get_config_path() |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
17 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
18 return self::$config_path; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
19 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
20 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
21 public static function write_config() |
|
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 $path = self::get_config_path(); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
24 $str = '<?php |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
25 return '.self::varexport(self::$config, true).';'; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
26 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
27 file_put_contents($path, $str); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
28 self::format_file($path); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
29 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
30 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
31 public static function read_config() |
|
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 $path = self::get_config_path(); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
34 self::$config = include $path; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
35 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
36 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
37 public static function print_config() |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
38 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
39 self::varexport(self::$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 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
42 /** |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
43 * PHP var_export() with short array syntax (square brackets) indented 2 spaces. |
|
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 * 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
|
46 * |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
47 * @see https://www.php.net/manual/en/function.var-export.php |
|
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 public static function varexport($expression, $return = false) |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
50 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
51 $export = var_export($expression, true); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
52 $patterns = [ |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
53 "/array \(/" => '[', |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
54 "/^([ ]*)\)(,?)$/m" => '$1]$2', |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
55 "/=>[ ]?\n[ ]+\[/" => '=> [', |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
56 "/([ ]*)(\'[^\']+\') => ([\[\'])/" => '$1$2 => $3', |
|
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 $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
|
59 if ((bool) $return) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
60 return $export; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
61 } else { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
62 echo $export; |
|
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 } |
|
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 protected static function format_file(string $path) |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
67 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
68 exec('php-cs-fixer fix '.$path); |
|
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 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
71 public static function set_up_tables() |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
72 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
73 $schema = \DB::connection()->getDoctrineSchemaManager(); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
74 // get all the tables available in the db |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
75 $tables = collect($schema->listTableNames())->all(); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
76 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
77 $insert_tables = []; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
78 foreach ($tables as $table) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
79 $columns = []; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
80 $table_columns = $schema->introspectTable($table)->getColumns(); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
81 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
82 foreach ($table_columns as $column) { |
| 9 | 83 $full_class = get_class($column->getType()); |
| 84 $class_parts = explode('\\', $full_class); | |
| 85 $class_name = end($class_parts); | |
| 86 | |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
87 $columns[$column->getName()] = [ |
| 9 | 88 'type' => $class_name, |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
89 'should_insert' => [ |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
90 'controller' => true, |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
91 'model' => true, |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
92 'requests' => true, |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
93 'views' => true, |
| 9 | 94 ], |
|
8
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
95 ]; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
96 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
97 |
| 9 | 98 $insert_tables[$table] = []; |
| 99 $insert_tables[$table]['columns'] = $columns; | |
| 100 $insert_tables[$table]['type'] = 'default'; | |
|
8
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 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
|
103 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
104 return $tables; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
105 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
106 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
107 private static function merge_array_priority(&$priority, $merged) |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
108 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
109 foreach ($merged as $key => $value) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
110 // 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
|
111 if (!isset($priority[$key])) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
112 $priority[$key] = $value; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
113 } else { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
114 // if the value is an array recursively merge |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
115 if (is_array($value) && is_array($priority[$key])) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
116 self::merge_array_priority($priority[$key], $value); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
117 } |
|
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 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
120 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
121 } |
