Mercurial > packages > magicforger
annotate 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 |
| 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) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
83 $columns[$column->getName()] = [ |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
84 'should_insert' => [ |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
85 'controller' => true, |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
86 'model' => true, |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
87 'requests' => true, |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
88 'views' => true, |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
89 ], |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
90 ]; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
91 } |
|
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 $insert_tables[$table] = $columns; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
94 |
|
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 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
|
97 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
98 return $tables; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
99 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
100 |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
101 private static function merge_array_priority(&$priority, $merged) |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
102 { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
103 foreach ($merged as $key => $value) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
104 // 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
|
105 if (!isset($priority[$key])) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
106 $priority[$key] = $value; |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
107 } else { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
108 // if the value is an array recursively merge |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
109 if (is_array($value) && is_array($priority[$key])) { |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
110 self::merge_array_priority($priority[$key], $value); |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
111 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
112 } |
|
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 } |
|
4216c0dc638c
Added base php cs fixer added config helper to maintain config
luka
parents:
diff
changeset
|
115 } |
