Mercurial > packages > framework
comparison src/Database/SchemaDrawer.php @ 2:b44434aaa767
Moving around the components.
Made a big step in the right direction with the Builder and named joins being accessible.
| author | luka |
|---|---|
| date | Wed, 18 Jun 2025 22:28:47 -0400 |
| parents | |
| children | 84c75d9d90be |
comparison
equal
deleted
inserted
replaced
| 1:56d9c64d64aa | 2:b44434aaa767 |
|---|---|
| 1 <?php | |
| 2 | |
| 3 namespace Libraries; | |
| 4 | |
| 5 use Illuminate\Support\Facades\Schema; | |
| 6 | |
| 7 class SchemaDrawer | |
| 8 { | |
| 9 public $schema; | |
| 10 | |
| 11 public $table; | |
| 12 | |
| 13 public $tables; | |
| 14 | |
| 15 public function __construct() | |
| 16 { | |
| 17 $this->tables = $this->getTables(); | |
| 18 } | |
| 19 | |
| 20 public static function createSchema($file_name = null) | |
| 21 { | |
| 22 $sd = new self; | |
| 23 | |
| 24 $schema = $sd->list_tables_and_fks(); | |
| 25 if (is_null($file_name)) { | |
| 26 return $schema; | |
| 27 } else { | |
| 28 $f = fopen($file_name, 'w+'); | |
| 29 fwrite($f, $schema); | |
| 30 fclose($f); | |
| 31 } | |
| 32 | |
| 33 return true; | |
| 34 } | |
| 35 | |
| 36 protected static function getTableColumns(string $table_name) | |
| 37 { | |
| 38 return Schema::getColumns($table_name); | |
| 39 } | |
| 40 | |
| 41 protected static function getTableForeignKeys(string $table_name) | |
| 42 { | |
| 43 return Schema::getForeignKeys($table_name); | |
| 44 } | |
| 45 | |
| 46 /** | |
| 47 * Get the tables in the schema. | |
| 48 */ | |
| 49 protected function getTables() | |
| 50 { | |
| 51 if (is_null($this->tables)) { | |
| 52 $key = 'DATABASE()'; | |
| 53 $schema = \DB::select('SELECT DATABASE()')[0]->$key; | |
| 54 $this->tables = Schema::getTables(schema: [$schema]); | |
| 55 } | |
| 56 | |
| 57 return $this->tables; | |
| 58 } | |
| 59 | |
| 60 public function list_tables_and_fks(): string | |
| 61 { | |
| 62 $fk_labels = false; | |
| 63 $str = ' | |
| 64 digraph mydb { | |
| 65 fontname="Helvetica,Arial,sans-serif" | |
| 66 graph [layout="circo"] | |
| 67 node [fontname="Helvetica,Arial,sans-serif", shape="record"] | |
| 68 edge [fontname="Helvetica,Arial,sans-serif"] | |
| 69 '; | |
| 70 foreach ($this->tables as $table) { | |
| 71 $table_name = $table['name']; | |
| 72 $str .= "\t".$table_name.'[label="{'.$table_name; | |
| 73 foreach (self::getTableColumns($table_name) as $column) { | |
| 74 $col_name = $column['name']; | |
| 75 $str .= ' | <'.$col_name.'> '.$col_name; | |
| 76 // TODO: should we show the type? | |
| 77 } | |
| 78 $str .= '}"]'."\n"; | |
| 79 | |
| 80 $fks = self::getTableForeignKeys($table_name); | |
| 81 foreach ($fks as $fk) { | |
| 82 $name = $fk['name']; | |
| 83 | |
| 84 if (! (str_contains($name, 'created_by') || str_contains($name, 'updated_by'))) { | |
| 85 $local_column = $fk['columns']; | |
| 86 $local_column = (isset($local_column[0])) ? ':'.$local_column[0] : ''; | |
| 87 $foreign_column = $fk['foreign_columns']; | |
| 88 $foreign_column = (isset($foreign_column[0])) ? ':'.$foreign_column[0] : ''; | |
| 89 $str .= "\t".$table_name.$local_column.' -> '.$fk['foreign_table'].$foreign_column; | |
| 90 if ($fk_labels) { | |
| 91 $str .= '[label="'.$name."\"];\n"; | |
| 92 } else { | |
| 93 $str .= ";\n"; | |
| 94 } | |
| 95 } | |
| 96 } | |
| 97 $str .= "\n"; | |
| 98 } | |
| 99 $str .= '}'; | |
| 100 | |
| 101 return $str; | |
| 102 } | |
| 103 } |
