Skip to content

Commit 24667b8

Browse files
committed
added support for text output so it can be used as context in an ai model
1 parent 07254be commit 24667b8

File tree

3 files changed

+111
-4
lines changed

3 files changed

+111
-4
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,26 @@ Or use one of the other [output formats](https://www.graphviz.org/doc/info/outpu
8484
php artisan generate:erd output.svg --format=svg
8585
```
8686

87+
### Text Output
88+
89+
If you want to generate a text representation of the ER diagram instead of an image, you can use the `--text-output` option:
90+
91+
```bash
92+
php artisan generate:erd output.txt --text-output
93+
```
94+
95+
This will generate a text file with the GraphViz DOT representation of the ER diagram.
96+
97+
### Structured Text Output for AI Models
98+
99+
If you want to generate a structured text representation of the ER diagram that is more suitable for AI models, you can use the `--structured` option:
100+
101+
```bash
102+
php artisan generate:erd output.txt --structured
103+
```
104+
105+
This will generate a Markdown file with a structured representation of the entities and their relationships, which can be used as context for AI models.
106+
87107
## Customization
88108

89109
Please take a look at the published `erd-generator.php` configuration file for all available customization options.
@@ -121,4 +141,4 @@ If you discover any security related issues, please email [email protected] ins
121141

122142
## License
123143

124-
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
144+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

src/GenerateDiagramCommand.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class GenerateDiagramCommand extends Command
1919
*
2020
* @var string
2121
*/
22-
protected $signature = 'generate:erd {filename?} {--format=png}';
22+
protected $signature = 'generate:erd {filename?} {--format=png} {--text-output : Output as text file instead of image} {--structured : Generate structured text output for AI models}';
2323

2424
/**
2525
* The console command description.
@@ -70,10 +70,32 @@ public function handle()
7070
);
7171
});
7272

73+
// If structured text output is requested, generate it
74+
if ($this->option('structured')) {
75+
$textOutput = $this->graphBuilder->generateStructuredTextRepresentation($models);
76+
$outputFileName = $this->getTextOutputFileName();
77+
file_put_contents($outputFileName, $textOutput);
78+
$this->info(PHP_EOL);
79+
$this->info('Wrote structured text diagram to ' . $outputFileName);
80+
return;
81+
}
82+
7383
$graph = $this->graphBuilder->buildGraph($models);
7484

75-
if ($this->option('format') === self::FORMAT_TEXT) {
76-
$this->info($graph->__toString());
85+
if ($this->option('text-output') || $this->option('format') === self::FORMAT_TEXT) {
86+
$textOutput = $graph->__toString();
87+
88+
// If text-output option is set, write to file
89+
if ($this->option('text-output')) {
90+
$outputFileName = $this->getTextOutputFileName();
91+
file_put_contents($outputFileName, $textOutput);
92+
$this->info(PHP_EOL);
93+
$this->info('Wrote text diagram to ' . $outputFileName);
94+
return;
95+
}
96+
97+
// Otherwise just output to console
98+
$this->info($textOutput);
7799
return;
78100
}
79101

@@ -89,6 +111,11 @@ protected function getOutputFileName(): string
89111
static::DEFAULT_FILENAME . '.' . $this->option('format');
90112
}
91113

114+
protected function getTextOutputFileName(): string
115+
{
116+
return $this->argument('filename') ?: static::DEFAULT_FILENAME . '.txt';
117+
}
118+
92119
protected function getModelsThatShouldBeInspected(): Collection
93120
{
94121
$directories = config('erd-generator.directories');

src/GraphBuilder.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,66 @@ public function buildGraph(Collection $models) : Graph
3030
return $this->graph;
3131
}
3232

33+
/**
34+
* Generate a structured text representation of the ER diagram
35+
*
36+
* @param Collection $models
37+
* @return string
38+
*/
39+
public function generateStructuredTextRepresentation(Collection $models) : string
40+
{
41+
$output = "# Entity Relationship Diagram\n\n";
42+
43+
// First list all models/entities with their attributes
44+
$output .= "## Entities\n\n";
45+
46+
foreach ($models as $model) {
47+
/** @var Model $model */
48+
$eloquentModel = app($model->getModel());
49+
$output .= "### " . $model->getLabel() . " (`" . $model->getModel() . "`)\n\n";
50+
51+
// Add table columns if available
52+
if (config('erd-generator.use_db_schema')) {
53+
$columns = $this->getTableColumnsFromModel($eloquentModel);
54+
if (count($columns) > 0) {
55+
$output .= "#### Attributes:\n\n";
56+
foreach ($columns as $column) {
57+
$columnType = config('erd-generator.use_column_types') ? ' (' . $column->getType()->getName() . ')' : '';
58+
$output .= "- `" . $column->getName() . "`" . $columnType . "\n";
59+
}
60+
$output .= "\n";
61+
}
62+
}
63+
}
64+
65+
// Then list all relationships
66+
$output .= "## Relationships\n\n";
67+
68+
foreach ($models as $model) {
69+
/** @var Model $model */
70+
if (count($model->getRelations()) > 0) {
71+
$output .= "### " . $model->getLabel() . " Relationships\n\n";
72+
73+
foreach ($model->getRelations() as $relation) {
74+
/** @var ModelRelation $relation */
75+
$relatedModel = $models->first(function ($m) use ($relation) {
76+
return $m->getModel() === $relation->getRelatedModel();
77+
});
78+
79+
if ($relatedModel) {
80+
$output .= "- **" . $relation->getType() . "** `" . $relation->getName() . "` to " .
81+
$relatedModel->getLabel() . " (Local Key: `" . $relation->getLocalKey() .
82+
"`, Foreign Key: `" . $relation->getForeignKey() . "`)\n";
83+
}
84+
}
85+
86+
$output .= "\n";
87+
}
88+
}
89+
90+
return $output;
91+
}
92+
3393
protected function getTableColumnsFromModel(EloquentModel $model)
3494
{
3595
try {

0 commit comments

Comments
 (0)