annotate src/Database/SchemaDrawer.php @ 4:84c75d9d90be

Changing usage to be bootstrap 5, not everything is reviewed but it's been started
author luka
date Tue, 19 Aug 2025 20:33:35 -0400
parents b44434aaa767
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
1 <?php
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
2
4
84c75d9d90be Changing usage to be bootstrap 5, not everything is reviewed but it's been started
luka
parents: 2
diff changeset
3 namespace Wizard\Framework\Database;
2
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
4
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
5 use Illuminate\Support\Facades\Schema;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
6
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
7 class SchemaDrawer
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
8 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
9 public $schema;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
10
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
11 public $table;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
12
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
13 public $tables;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
14
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
15 public function __construct()
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
16 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
17 $this->tables = $this->getTables();
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
18 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
19
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
20 public static function createSchema($file_name = null)
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
21 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
22 $sd = new self;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
23
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
24 $schema = $sd->list_tables_and_fks();
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
25 if (is_null($file_name)) {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
26 return $schema;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
27 } else {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
28 $f = fopen($file_name, 'w+');
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
29 fwrite($f, $schema);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
30 fclose($f);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
31 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
32
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
33 return true;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
34 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
35
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
36 protected static function getTableColumns(string $table_name)
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
37 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
38 return Schema::getColumns($table_name);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
39 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
40
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
41 protected static function getTableForeignKeys(string $table_name)
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
42 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
43 return Schema::getForeignKeys($table_name);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
44 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
45
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
46 /**
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
47 * Get the tables in the schema.
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
48 */
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
49 protected function getTables()
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
50 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
51 if (is_null($this->tables)) {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
52 $key = 'DATABASE()';
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
53 $schema = \DB::select('SELECT DATABASE()')[0]->$key;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
54 $this->tables = Schema::getTables(schema: [$schema]);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
55 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
56
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
57 return $this->tables;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
58 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
59
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
60 public function list_tables_and_fks(): string
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
61 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
62 $fk_labels = false;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
63 $str = '
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
64 digraph mydb {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
65 fontname="Helvetica,Arial,sans-serif"
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
66 graph [layout="circo"]
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
67 node [fontname="Helvetica,Arial,sans-serif", shape="record"]
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
68 edge [fontname="Helvetica,Arial,sans-serif"]
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
69 ';
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
70 foreach ($this->tables as $table) {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
71 $table_name = $table['name'];
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
72 $str .= "\t".$table_name.'[label="{'.$table_name;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
73 foreach (self::getTableColumns($table_name) as $column) {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
74 $col_name = $column['name'];
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
75 $str .= ' | <'.$col_name.'> '.$col_name;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
76 // TODO: should we show the type?
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
77 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
78 $str .= '}"]'."\n";
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
79
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
80 $fks = self::getTableForeignKeys($table_name);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
81 foreach ($fks as $fk) {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
82 $name = $fk['name'];
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
83
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
84 if (! (str_contains($name, 'created_by') || str_contains($name, 'updated_by'))) {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
85 $local_column = $fk['columns'];
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
86 $local_column = (isset($local_column[0])) ? ':'.$local_column[0] : '';
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
87 $foreign_column = $fk['foreign_columns'];
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
88 $foreign_column = (isset($foreign_column[0])) ? ':'.$foreign_column[0] : '';
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
89 $str .= "\t".$table_name.$local_column.' -> '.$fk['foreign_table'].$foreign_column;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
90 if ($fk_labels) {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
91 $str .= '[label="'.$name."\"];\n";
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
92 } else {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
93 $str .= ";\n";
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
94 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
95 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
96 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
97 $str .= "\n";
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
98 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
99 $str .= '}';
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
100
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
101 return $str;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
102 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
103 }