annotate src/ConfigHelper.php @ 23:827efbf4d73c main-dev

Huge changes to the relationships for models and more complex
author Luka Sitas <sitas.luka.97@gmail.com>
date Fri, 11 Apr 2025 20:50:20 -0400
parents 19b7a8de0019
children
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 *
23
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
72 * @param bool $return
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
73 * @return string|string[]|null
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
74 */
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
75 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
76 {
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
77 $export = var_export($expression, true);
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
78 $patterns = [
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
79 "/array \(/" => '[',
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
80 "/^([ ]*)\)(,?)$/m" => '$1]$2',
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
81 "/=>[ ]?\n[ ]+\[/" => '=> [',
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
82 "/([ ]*)(\'[^\']+\') => ([\[\'])/" => '$1$2 => $3',
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
83 ];
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
84 $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
85 if ((bool) $return) {
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
86 return $export;
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
87 } else {
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
88 echo $export;
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
89 }
14
c969ed13c570 Changes for various files
luka
parents: 11
diff changeset
90
c969ed13c570 Changes for various files
luka
parents: 11
diff changeset
91 return null;
8
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
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
94 /**
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
95 * Format the given file.
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
96 */
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
97 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
98 {
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
99 exec('php-cs-fixer fix '.$path);
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
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
102 /**
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
103 * Set up tables.
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
104 */
14
c969ed13c570 Changes for various files
luka
parents: 11
diff changeset
105 public static function set_up_tables(): Collection
8
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 $schema = \DB::connection()->getDoctrineSchemaManager();
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
108 $tables = collect($schema->listTableNames())->all();
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
109 $table_foreign_keys = [];
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
110 foreach ($tables as $table) {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
111 $table_foreign_keys[$table] = $schema->listTableForeignKeys($table);
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
112 }
8
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 = [];
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
117 $table_columns = $schema->listTableColumns($table);
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
118
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
119 // Initiate new arrays for foreign keys
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
120 $foreign_keys = [];
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
121 $foreign_keys_reverse = [];
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
122
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
123 // Check foreign key references from this table
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
124 $foreign_keys_list = $table_foreign_keys[$table];
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
125 foreach ($foreign_keys_list as $fk) {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
126 $foreign_keys[$fk->getLocalColumns()[0]] = [
23
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
127 'foreign_table' => $fk->getForeignTableName(),
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
128 'foreign_column' => $fk->getForeignColumns()[0],
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
129 ];
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
130 }
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
131
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
132 foreach ($table_columns as $column) {
9
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
133 $full_class = get_class($column->getType());
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
134 $class_parts = explode('\\', $full_class);
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
135 $class_name = end($class_parts);
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
136
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
137 $columns[$column->getName()] = [
23
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
138 'type' => $class_name,
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
139 'should_insert' => [
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
140 'controller' => true,
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
141 'model' => true,
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
142 'requests' => true,
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
143 'views' => true,
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
144 ],
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
145 ];
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
146 }
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
147 // Check foreign key references to this table
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
148 foreach ($tables as $other_table) {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
149 if ($other_table != $table) {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
150 $foreign_keys_list = $table_foreign_keys[$other_table];
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
151 foreach ($foreign_keys_list as $fk) {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
152 if ($fk->getForeignTableName() == $table) {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
153 $foreign_keys_reverse[] = [
23
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
154 'table' => $other_table,
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
155 'column' => $fk->getLocalColumns()[0],
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
156 ];
11
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 }
9
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
161 $insert_tables[$table] = [];
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
162 $insert_tables[$table]['columns'] = $columns;
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
163 $insert_tables[$table]['foreign_keys'] = $foreign_keys; // Foreign keys FROM this table
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
164 $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
165 $insert_tables[$table]['type'] = 'default';
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
166 }
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
167
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
168 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
169
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
170 return $tables;
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
171 }
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
172
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
173 /**
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
174 * Merge two arrays and ensure priority values do not get overwritten.
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
175 *
23
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
176 * @param array $priority
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
177 * @param array $merged
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
178 */
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 10
diff changeset
179 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
180 {
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
181 foreach ($merged as $key => $value) {
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
182 // if the priority key is not set, automatically add the merged values
23
827efbf4d73c Huge changes to the relationships for models and more complex
Luka Sitas <sitas.luka.97@gmail.com>
parents: 19
diff changeset
183 if (! isset($priority[$key])) {
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
184 $priority[$key] = $value;
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
185 } else {
10
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
186 // 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
187 if (is_array($value) && is_array($priority[$key])) {
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
188 self::merge_array_priority($priority[$key], $value);
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
189 }
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 }