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
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
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
9
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
12
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
16 /**
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
17 * Set up configuration path
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
18 *
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
19 * @param string $base_path
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
26 /**
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
27 * Get configuration path
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
28 *
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
29 * @return string
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
36 /**
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
37 * Write configuration into a file
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
50 /**
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
51 * Read configuration from a file
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
59 /**
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
60 * Print configuration
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
73 *
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
74 * @param mixed $expression
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
75 * @param boolean $return
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
95 /**
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
96 * Format the given file
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
97 *
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
98 * @param string $path
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
105 /**
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
106 * Set up tables
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
120 $full_class = get_class($column->getType());
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
121 $class_parts = explode('\\', $full_class);
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
122 $class_name = end($class_parts);
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
123
8
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
124 $columns[$column->getName()] = [
9
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
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
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
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 }
4216c0dc638c Added base php cs fixer added config helper to maintain config
luka
parents:
diff changeset
134
9
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
135 $insert_tables[$table] = [];
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
136 $insert_tables[$table]['columns'] = $columns;
d4730c14806d Small changes to config generation and table name input
luka
parents: 8
diff changeset
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
145 /**
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
146 * Merge two arrays and ensure priority values do not get overwritten
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
147 *
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
148 * @param array $priority
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
149 * @param array $merged
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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
a9ff874afdbd better comments for config helper
luka
parents: 9
diff changeset
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 }