Skip to content

Commit

Permalink
Merge pull request #32 from diogogomeswww/enhancement/add-config-igno…
Browse files Browse the repository at this point in the history
…re-model-or-relation

Add config option to ignore model or just a relation
  • Loading branch information
mpociot authored Aug 15, 2018
2 parents 981d387 + 6098253 commit ac98742
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 16 deletions.
14 changes: 8 additions & 6 deletions src/ModelFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace BeyondCode\ErdGenerator;

use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use PhpParser\NodeTraverser;
use PhpParser\ParserFactory;
use PhpParser\Node\Stmt\Class_;
use Illuminate\Support\Collection;
use PhpParser\Node\Stmt\Namespace_;
use Illuminate\Filesystem\Filesystem;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\NameResolver;
use Illuminate\Database\Eloquent\Model as EloquentModel;
use PhpParser\ParserFactory;

class ModelFinder
{
Expand All @@ -29,13 +29,15 @@ public function getModelsInDirectory(string $directory): Collection
$this->filesystem->allFiles($directory) :
$this->filesystem->files($directory);

$ignoreModels = array_filter(config('erd-generator.ignore', []), 'is_string');

return Collection::make($files)->filter(function ($path) {
return Str::endsWith($path, '.php');
})->map(function ($path) {
return $this->getFullyQualifiedClassNameFromFile($path);
})->filter(function (string $className) {
return !empty($className) && is_subclass_of($className, EloquentModel::class);
});
})->diff($ignoreModels);
}

protected function getFullyQualifiedClassNameFromFile(string $path): string
Expand Down
13 changes: 9 additions & 4 deletions src/RelationFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace BeyondCode\ErdGenerator;

use ReflectionClass;
use ReflectionMethod;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;
use ReflectionClass;
use ReflectionMethod;

class RelationFinder
{
Expand Down Expand Up @@ -40,6 +40,11 @@ public function getModelRelations(string $model)

$relations = $relations->filter();

if ($ignoreRelations = array_get(config('erd-generator.ignore', []),$model))
{
$relations = $relations->diffKeys(array_flip($ignoreRelations));
}

return $relations;
}

Expand Down
31 changes: 27 additions & 4 deletions tests/FindModelsFromConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace BeyondCode\ErdGenerator\Tests;

use BeyondCode\ErdGenerator\GraphBuilder;
use BeyondCode\ErdGenerator\ModelFinder;
use BeyondCode\ErdGenerator\Tests\Models\Avatar;
use BeyondCode\ErdGenerator\GenerateDiagramCommand;
use BeyondCode\ErdGenerator\Tests\Models\Comment;
use BeyondCode\ErdGenerator\Tests\Models\Post;
use BeyondCode\ErdGenerator\Tests\Models\User;

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

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

$this->assertCount(4, $classNames);
$this->assertSame(Avatar::class, $classNames->first());
}
}

/** @test */
public function it_will_ignore_a_model_if_it_is_excluded_on_config()
{
$this->app['config']->set('erd-generator.ignore', [
Avatar::class,
User::class => [
'posts'
]
]);

$finder = new ModelFinder(app()->make('files'));

$classNames = $finder->getModelsInDirectory(__DIR__ . "/Models");

$this->assertCount(3, $classNames);
$this->assertEquals(
[Comment::class, Post::class, User::class],
$classNames->values()->all()
);
}

}
22 changes: 20 additions & 2 deletions tests/GetModelRelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use BeyondCode\ErdGenerator\ModelRelation;
use BeyondCode\ErdGenerator\RelationFinder;
use BeyondCode\ErdGenerator\Tests\Models\Post;
use BeyondCode\ErdGenerator\Tests\Models\User;
use BeyondCode\ErdGenerator\Tests\Models\Avatar;
use BeyondCode\ErdGenerator\Tests\Models\Comment;
use BeyondCode\ErdGenerator\Tests\Models\Post;
use BeyondCode\ErdGenerator\Tests\Models\User;

class GetModelRelationsTest extends TestCase
{
Expand Down Expand Up @@ -48,4 +48,22 @@ public function it_can_find_model_relations()
$this->assertSame(null, $avatar->getLocalKey());
$this->assertSame(null, $avatar->getForeignKey());
}

/** @test */
public function it_will_ignore_a_relation_if_it_is_excluded_on_config()
{
$this->app['config']->set('erd-generator.ignore', [
User::class => [
'posts'
]
]);

$finder = new RelationFinder();

$relations = $finder->getModelRelations(User::class);

$this->assertCount(2, $relations);
$this->assertNull(array_get($relations, 'posts'));
}

}

0 comments on commit ac98742

Please sign in to comment.