Skip to content

Commit e9fef7f

Browse files
author
Jordan Hoff
committed
Recursively search for models when defaulting to app
1 parent ee2d74e commit e9fef7f

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![Quality Score](https://img.shields.io/scrutinizer/g/beyondcode/laravel-er-diagram-generator.svg?style=flat-square)](https://scrutinizer-ci.com/g/beyondcode/laravel-er-diagram-generator)
66
[![Total Downloads](https://img.shields.io/packagist/dt/beyondcode/laravel-er-diagram-generator.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-er-diagram-generator)
77

8-
This package lets you generate entity relation diagrams by inspecting the relationships defined in your model files.
8+
This package lets you generate entity relation diagrams by inspecting the relationships defined in your model files.
99
It is highly customizable.
1010
Behind the scenes, it uses [GraphViz](https://www.graphviz.org) to generate the graph.
1111

@@ -37,29 +37,27 @@ If you are using Laravel 5.5+, the package will automatically register the servi
3737

3838
## Usage
3939

40-
Once the package is installed, publish the configuration file using
40+
By default, the package will automatically detect all models in your `app` directory that extend the Eloquent Model class. If you would like you explicitly define where your models are located, you can publish the configuration file using the following command.
4141

4242
```bash
4343
php artisan vendor:publish --provider=BeyondCode\\ErdGenerator\\ErdGeneratorServiceProvider
4444
```
4545

46-
Open the configuration file and add all folders, that contain your model files.
47-
4846
## Generating Diagrams
4947

5048
You can generate entity relation diagrams using the provided artisan command:
5149

5250
```bash
5351
php artisan generate:erd
54-
```
52+
```
5553

56-
This will generate a file called `graph.png`.
54+
This will generate a file called `graph.png`.
5755

5856
You can also specify a custom filename:
5957

6058
```bash
6159
php artisan generate:erd output.png
62-
```
60+
```
6361

6462
Or use one of the other [output formats](https://www.graphviz.org/doc/info/output.html), like SVG:
6563

@@ -71,7 +69,7 @@ php artisan generate:erd output.svg --format=svg
7169

7270
Please take a look at the published `erd-generator.php` configuration file for all available customization options.
7371

74-
## Examples
72+
## Examples
7573

7674
Here are some examples taken from the [Laravel.io](https://laravel.io) codebase.
7775

src/GenerateDiagramCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ protected function getModelsThatShouldBeInspected(): Collection
8282
{
8383
$directories = config('erd-generator.directories');
8484

85-
$modelsFromDirectories = $this->getAllModelsFromEachDirectory($directories);
85+
$modelsFromDirectories = empty($directories) ?
86+
$this->modelFinder->getModelsInDirectory(app_path(), true) :
87+
$this->getAllModelsFromEachDirectory($directories);
8688

8789
return $modelsFromDirectories;
8890
}

src/ModelFinder.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ public function __construct(Filesystem $filesystem)
2222
$this->filesystem = $filesystem;
2323
}
2424

25-
public function getModelsInDirectory(string $directory): Collection
25+
public function getModelsInDirectory(string $directory, bool $recursive = false): Collection
2626
{
27-
return Collection::make($this->filesystem->files($directory))->map(function ($path) {
27+
$files = $recursive ?
28+
$this->filesystem->allFiles($directory) :
29+
$this->filesystem->files($directory);
30+
31+
return Collection::make($files)->map(function ($path) {
2832
return $this->getFullyQualifiedClassNameFromFile($path);
2933
})->filter(function (string $className) {
3034
return !empty($className);
@@ -44,7 +48,7 @@ protected function getFullyQualifiedClassNameFromFile(string $path): string
4448

4549
$statements = $parser->parse($code);
4650
$statements = $traverser->traverse($statements);
47-
51+
4852
// get the first namespace declaration in the file
4953
$root_statement = collect($statements)->filter(function ($statement) {
5054
return $statement instanceof Namespace_;
@@ -59,5 +63,5 @@ protected function getFullyQualifiedClassNameFromFile(string $path): string
5963
})
6064
->first() ?? '';
6165
}
62-
66+
6367
}

0 commit comments

Comments
 (0)