annotate src/ConfigHelper.php @ 19:19b7a8de0019 main-dev

updating namespace from typo
author Luka Sitas <sitas.luka.97@gmail.com>
date Wed, 26 Feb 2025 19:26:08 -0500
parents c969ed13c570
children 827efbf4d73c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
19
19b7a8de0019 updating namespace from typo
Luka Sitas <sitas.luka.97@gmail.com>
parents: 14
diff changeset
3 namespace Wizard\MagicForger;
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
4
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
5 use Illuminate\Support\Collection;
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
6
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
7 class ConfigHelper
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
8 {
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
9 // Config array
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
10 public static $config = [];
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
11
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
12 // Config file name constant
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
13 public const CONFIG_FILE_NAME = 'mf_config.php';
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
14
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
15 // Config path variable
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
16 public static string $config_path;
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
17
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
18 /**
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
19 * Set up configuration path.
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
20 */
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
21 public static function setup_config_path(string $base_path): void
8
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
26 /**
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
27 * Get configuration path.
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
28 */
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
29 protected static function get_config_path(): string
8
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 return self::$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
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
34 /**
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
35 * Write configuration into a file.
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
36 */
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
37 public static function write_config(): void
8
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 $path = self::get_config_path();
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
40 $str = '<?php
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
41 return '.self::varexport(self::$config, true).';';
8
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 file_put_contents($path, $str);
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
44 // After writing the file, format it
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
45 self::format_file($path);
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
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
48 /**
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
49 * Read configuration from a file.
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
50 */
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
51 public static function read_config(): void
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
52 {
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
53 $path = self::get_config_path();
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
54 self::$config = include $path;
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
55 }
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
56
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
57 /**
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
58 * Print configuration.
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
59 */
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
60 public static function print_config(): void
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
61 {
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
62 self::varexport(self::$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
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
65 /**
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
66 * 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
67 *
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
68 * 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
69 *
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
70 * @see https://www.php.net/manual/en/function.var-export.php
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
71 *
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
72 * @param bool $return
14
c969ed13c570 Changes for various files
luka
parents: 11
diff changeset
73 *
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
74 * @return string|string[]|null
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
75 */
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
76 public static function varexport(mixed $expression, $return = false): string|array|null
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 $export = var_export($expression, true);
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
79 $patterns = [
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
80 "/array \(/" => '[',
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
81 "/^([ ]*)\)(,?)$/m" => '$1]$2',
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
82 "/=>[ ]?\n[ ]+\[/" => '=> [',
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
83 "/([ ]*)(\'[^\']+\') => ([\[\'])/" => '$1$2 => $3',
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
84 ];
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
85 $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
86 if ((bool) $return) {
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
87 return $export;
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
88 } else {
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
89 echo $export;
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
90 }
14
c969ed13c570 Changes for various files
luka
parents: 11
diff changeset
91
c969ed13c570 Changes for various files
luka
parents: 11
diff changeset
92 return null;
8
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
95 /**
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
96 * Format the given file.
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
97 */
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
98 protected static function format_file(string $path): void
8
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 exec('php-cs-fixer fix '.$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
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
103 /**
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
104 * Set up tables.
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
105 */
14
c969ed13c570 Changes for various files
luka
parents: 11
diff changeset
106 public static function set_up_tables(): Collection
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
107 {
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
108 $schema = \DB::connection()->getDoctrineSchemaManager();
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
109 $tables = collect($schema->listTableNames())->all();
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
110 $table_foreign_keys = [];
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
111 foreach ($tables as $table) {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
112 $table_foreign_keys[$table] = $schema->listTableForeignKeys($table);
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
113 }
8
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 $insert_tables = [];
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
116 foreach ($tables as $table) {
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
117 $columns = [];
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
118 $table_columns = $schema->listTableColumns($table);
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
119
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
120 // Initiate new arrays for foreign keys
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
121 $foreign_keys = [];
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
122 $foreign_keys_reverse = [];
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
123
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
124 // Check foreign key references from this table
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
125 $foreign_keys_list = $table_foreign_keys[$table];
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
126 foreach ($foreign_keys_list as $fk) {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
127 $foreign_keys[$fk->getLocalColumns()[0]] = [
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
128 'foreign_table' => $fk->getForeignTableName(),
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
129 'foreign_column' => $fk->getForeignColumns()[0],
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
130 ];
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
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 foreach ($table_columns as $column) {
9
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
134 $full_class = get_class($column->getType());
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
135 $class_parts = explode('\\', $full_class);
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
136 $class_name = end($class_parts);
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
137
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
138 $columns[$column->getName()] = [
9
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
139 'type' => $class_name,
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
140 'should_insert' => [
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
141 'controller' => true,
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
142 'model' => true,
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
143 'requests' => true,
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
144 'views' => true,
9
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
145 ],
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
146 ];
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
147 }
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
148 // Check foreign key references to this table
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
149 foreach ($tables as $other_table) {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
150 if ($other_table != $table) {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
151 $foreign_keys_list = $table_foreign_keys[$other_table];
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
152 foreach ($foreign_keys_list as $fk) {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
153 if ($fk->getForeignTableName() == $table) {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
154 $foreign_keys_reverse[] = [
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
155 'table' => $other_table,
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
156 'column' => $fk->getLocalColumns()[0],
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
157 ];
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
158 }
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
159 }
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
160 }
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
161 }
9
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
162 $insert_tables[$table] = [];
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
163 $insert_tables[$table]['columns'] = $columns;
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
164 $insert_tables[$table]['foreign_keys'] = $foreign_keys; // Foreign keys FROM this table
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
165 $insert_tables[$table]['foreign_keys_reverse'] = $foreign_keys_reverse; // Foreign keys TO this table
9
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
166 $insert_tables[$table]['type'] = 'default';
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
167 }
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
168
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
169 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
170
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
171 return $tables;
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
172 }
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
173
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
174 /**
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
175 * Merge two arrays and ensure priority values do not get overwritten.
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
176 *
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
177 * @param array $priority
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
178 * @param array $merged
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
179 */
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
180 private static function merge_array_priority(&$priority, $merged): void
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
181 {
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
182 foreach ($merged as $key => $value) {
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
183 // 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
184 if (!isset($priority[$key])) {
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
185 $priority[$key] = $value;
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
186 } else {
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
187 // 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
188 if (is_array($value) && is_array($priority[$key])) {
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
189 self::merge_array_priority($priority[$key], $value);
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
190 }
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
191 }
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
192 }
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
193 }
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
194 }