Skip to content

Commit ac98742

Browse files
authored
Merge pull request #32 from diogogomeswww/enhancement/add-config-ignore-model-or-relation
Add config option to ignore model or just a relation
2 parents 981d387 + 6098253 commit ac98742

File tree

4 files changed

+64
-16
lines changed

4 files changed

+64
-16
lines changed

src/ModelFinder.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
namespace BeyondCode\ErdGenerator;
44

5+
use Illuminate\Database\Eloquent\Model as EloquentModel;
6+
use Illuminate\Filesystem\Filesystem;
7+
use Illuminate\Support\Collection;
58
use Illuminate\Support\Str;
6-
use PhpParser\NodeTraverser;
7-
use PhpParser\ParserFactory;
89
use PhpParser\Node\Stmt\Class_;
9-
use Illuminate\Support\Collection;
1010
use PhpParser\Node\Stmt\Namespace_;
11-
use Illuminate\Filesystem\Filesystem;
11+
use PhpParser\NodeTraverser;
1212
use PhpParser\NodeVisitor\NameResolver;
13-
use Illuminate\Database\Eloquent\Model as EloquentModel;
13+
use PhpParser\ParserFactory;
1414

1515
class ModelFinder
1616
{
@@ -29,13 +29,15 @@ public function getModelsInDirectory(string $directory): Collection
2929
$this->filesystem->allFiles($directory) :
3030
$this->filesystem->files($directory);
3131

32+
$ignoreModels = array_filter(config('erd-generator.ignore', []), 'is_string');
33+
3234
return Collection::make($files)->filter(function ($path) {
3335
return Str::endsWith($path, '.php');
3436
})->map(function ($path) {
3537
return $this->getFullyQualifiedClassNameFromFile($path);
3638
})->filter(function (string $className) {
3739
return !empty($className) && is_subclass_of($className, EloquentModel::class);
38-
});
40+
})->diff($ignoreModels);
3941
}
4042

4143
protected function getFullyQualifiedClassNameFromFile(string $path): string

src/RelationFinder.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace BeyondCode\ErdGenerator;
44

5-
use ReflectionClass;
6-
use ReflectionMethod;
7-
use Illuminate\Support\Collection;
8-
use Illuminate\Database\Eloquent\Relations\Relation;
95
use Illuminate\Database\Eloquent\Relations\BelongsTo;
106
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
7+
use Illuminate\Database\Eloquent\Relations\Relation;
8+
use Illuminate\Support\Collection;
9+
use ReflectionClass;
10+
use ReflectionMethod;
1111

1212
class RelationFinder
1313
{
@@ -40,6 +40,11 @@ public function getModelRelations(string $model)
4040

4141
$relations = $relations->filter();
4242

43+
if ($ignoreRelations = array_get(config('erd-generator.ignore', []),$model))
44+
{
45+
$relations = $relations->diffKeys(array_flip($ignoreRelations));
46+
}
47+
4348
return $relations;
4449
}
4550

tests/FindModelsFromConfigTest.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace BeyondCode\ErdGenerator\Tests;
44

5-
use BeyondCode\ErdGenerator\GraphBuilder;
65
use BeyondCode\ErdGenerator\ModelFinder;
76
use BeyondCode\ErdGenerator\Tests\Models\Avatar;
8-
use BeyondCode\ErdGenerator\GenerateDiagramCommand;
7+
use BeyondCode\ErdGenerator\Tests\Models\Comment;
8+
use BeyondCode\ErdGenerator\Tests\Models\Post;
9+
use BeyondCode\ErdGenerator\Tests\Models\User;
910

1011
class FindModelsFromConfigTest extends TestCase
1112
{
@@ -15,9 +16,31 @@ public function it_can_find_class_names_from_directory()
1516
{
1617
$finder = new ModelFinder(app()->make('files'));
1718

18-
$classNames = $finder->getModelsInDirectory("./tests/Models");
19+
$classNames = $finder->getModelsInDirectory(__DIR__ . "/Models");
1920

2021
$this->assertCount(4, $classNames);
2122
$this->assertSame(Avatar::class, $classNames->first());
2223
}
23-
}
24+
25+
/** @test */
26+
public function it_will_ignore_a_model_if_it_is_excluded_on_config()
27+
{
28+
$this->app['config']->set('erd-generator.ignore', [
29+
Avatar::class,
30+
User::class => [
31+
'posts'
32+
]
33+
]);
34+
35+
$finder = new ModelFinder(app()->make('files'));
36+
37+
$classNames = $finder->getModelsInDirectory(__DIR__ . "/Models");
38+
39+
$this->assertCount(3, $classNames);
40+
$this->assertEquals(
41+
[Comment::class, Post::class, User::class],
42+
$classNames->values()->all()
43+
);
44+
}
45+
46+
}

tests/GetModelRelationsTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
use BeyondCode\ErdGenerator\ModelRelation;
66
use BeyondCode\ErdGenerator\RelationFinder;
7-
use BeyondCode\ErdGenerator\Tests\Models\Post;
8-
use BeyondCode\ErdGenerator\Tests\Models\User;
97
use BeyondCode\ErdGenerator\Tests\Models\Avatar;
108
use BeyondCode\ErdGenerator\Tests\Models\Comment;
9+
use BeyondCode\ErdGenerator\Tests\Models\Post;
10+
use BeyondCode\ErdGenerator\Tests\Models\User;
1111

1212
class GetModelRelationsTest extends TestCase
1313
{
@@ -48,4 +48,22 @@ public function it_can_find_model_relations()
4848
$this->assertSame(null, $avatar->getLocalKey());
4949
$this->assertSame(null, $avatar->getForeignKey());
5050
}
51+
52+
/** @test */
53+
public function it_will_ignore_a_relation_if_it_is_excluded_on_config()
54+
{
55+
$this->app['config']->set('erd-generator.ignore', [
56+
User::class => [
57+
'posts'
58+
]
59+
]);
60+
61+
$finder = new RelationFinder();
62+
63+
$relations = $finder->getModelRelations(User::class);
64+
65+
$this->assertCount(2, $relations);
66+
$this->assertNull(array_get($relations, 'posts'));
67+
}
68+
5169
}

0 commit comments

Comments
 (0)