[DOCUMENTATION] Add database diagram to developer documentation

This commit is contained in:
Hugo Sales 2021-05-01 12:47:29 +00:00
parent 06b236374d
commit cfe842b487
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
6 changed files with 1551 additions and 16 deletions

View File

@ -1,4 +1,4 @@
#!/usr/local/bin/php #!/usr/bin/env php
<?php <?php
@ -6,7 +6,7 @@ define('INSTALL_DIR', dirname(__DIR__));
require INSTALL_DIR . '/vendor/autoload.php'; require INSTALL_DIR . '/vendor/autoload.php';
use App\Util\Common; use App\Util\Formatting;
use App\Util\HTML as H; use App\Util\HTML as H;
use Functional as F; use Functional as F;
@ -24,13 +24,22 @@ $files = glob(INSTALL_DIR . '/src/Entity/*.php');
$tables = []; $tables = [];
$edges = []; $edges = [];
$classes = [];
foreach ($files as $file) { foreach ($files as $file) {
require_once $file; require_once $file;
$class = '';
$declared = get_declared_classes(); $declared = get_declared_classes();
$class = end($declared); foreach ($declared as $dc) {
if (preg_match('/^(App|(Component|Plugin)\\\\[^\\\\]+)\\\\Entity/', $dc) && !in_array($dc, $classes)) {
$class = $dc;
$classes[] = $class;
break;
}
}
$schema = $class::schemaDef(); $schema = $class::schemaDef();
$table = $schema['name']; $table = preg_replace(',`?([^`]+)`?,', '$1', $schema['name']);
$fields = [['name' => $table, 'type' => '']]; $fields = [['name' => $table, 'type' => '']];
foreach ($schema['fields'] as $name => $opts) { foreach ($schema['fields'] as $name => $opts) {
$fields[] = [ $fields[] = [
@ -39,14 +48,11 @@ foreach ($files as $file) {
]; ];
} }
if (isset($schema['foreign keys'])) { foreach ($schema['fields'] as $field => $opts) {
foreach ($schema['foreign keys'] as $name => $map) { if (isset($opts['foreign key'])) {
// Patern matching like above would be nice [$foreign_entity, $foreign_key] = explode('.', $opts['target']);
list($foreign_table, $foreign_table = Formatting::camelCaseToSnakeCase(preg_replace('/GSActor/', 'gsactor', $foreign_entity));
$keys) = $map; $edges[] = "{$table}:{$field} -- {$foreign_table}:{$foreign_key}";
$local_key = array_keys($keys)[0];
$foreign_key = $keys[$local_key];
$edges[] = "{$table}:{$local_key} -- {$foreign_table}:{$foreign_key}";
} }
} }
@ -59,12 +65,12 @@ foreach ($files as $file) {
F\map($fields, $cell)), F\map($fields, $cell)),
]; ];
$tables[] = Common::indent("{$table} [shape=none, label=<\n" . Common::indent(H::html($html)) . "\n>]"); $tables[] = Formatting::indent("{$table} [shape=none, label=<\n" . Formatting::indent(H::html($html)) . "\n>]");
} }
$replace = [ $replace = [
'/%tables%/' => Common::indent(implode("\n", $tables)), '/%tables%/' => Formatting::indent(implode("\n", $tables)),
'/%edges%/' => Common::indent(implode("\n", $edges)), '/%edges%/' => Formatting::indent(implode("\n", $edges)),
// '/_/' => '\textunderscore ', // '/_/' => '\textunderscore ',
]; ];
@ -73,7 +79,7 @@ foreach ($replace as $from => $to) {
$out = preg_replace($from, $to, $out); $out = preg_replace($from, $to, $out);
} }
$path = dirname(__DIR__) . '/DOCUMENTATION/database'; $path = dirname(__DIR__) . '/docs/developer/src/database';
$outfile = $path . '/database.dot'; $outfile = $path . '/database.dot';
file_put_contents($outfile, $out); file_put_contents($outfile, $out);

View File

@ -2,6 +2,7 @@
- [High level view](./high_level.md) - [High level view](./high_level.md)
- [Architecture and Paradigms](./architecture.md) - [Architecture and Paradigms](./architecture.md)
- [Database](./database.md)
- [Plugins](./plugins.md) - [Plugins](./plugins.md)
- [Event Handlers](./plugins/no_docker_shell.md) - [Event Handlers](./plugins/no_docker_shell.md)
- [Installation](./plugins/docker_web.md) - [Installation](./plugins/docker_web.md)

View File

@ -0,0 +1,3 @@
# Database
Checkout a visual diagram of the database tables at docs/developer/src/database/database.pdf

File diff suppressed because it is too large Load Diff

Binary file not shown.