Skip to content

Commit 2578ea9

Browse files
authored
Merge pull request #87 from iforwms/feature/whitelist
Allow white-listing models
2 parents b4819e5 + ba7abaa commit 2578ea9

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

config/config.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,18 @@
2323
// 'user'
2424
// ]
2525
],
26-
26+
27+
/*
28+
* If you want to see only specific models, specify them here using fully qualified
29+
* classnames.
30+
*
31+
* Note: that if this array is filled, the 'ignore' array will not be used.
32+
*/
33+
'whitelist' => [
34+
// App\User::class,
35+
// App\Post::class,
36+
],
37+
2738
/*
2839
* If true, all directories specified will be scanned recursively for models.
2940
* Set this to false if you prefer to explicitly define each directory that should

src/ModelFinder.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,25 @@ public function getModelsInDirectory(string $directory): Collection
3131
$this->filesystem->files($directory);
3232

3333
$ignoreModels = array_filter(config('erd-generator.ignore', []), 'is_string');
34+
$whitelistModels = array_filter(config('erd-generator.whitelist', []), 'is_string');
3435

35-
return Collection::make($files)->filter(function ($path) {
36+
$collection = Collection::make($files)->filter(function ($path) {
3637
return Str::endsWith($path, '.php');
3738
})->map(function ($path) {
3839
return $this->getFullyQualifiedClassNameFromFile($path);
3940
})->filter(function (string $className) {
4041
return !empty($className)
4142
&& is_subclass_of($className, EloquentModel::class)
4243
&& ! (new ReflectionClass($className))->isAbstract();
43-
})->diff($ignoreModels)->sort();
44+
});
45+
46+
if(!count($whitelistModels)) {
47+
return $collection->diff($ignoreModels)->sort();
48+
}
49+
50+
return $collection->filter(function (string $className) use ($whitelistModels) {
51+
return in_array($className, $whitelistModels);
52+
});
4453
}
4554

4655
protected function getFullyQualifiedClassNameFromFile(string $path): string

tests/FindModelsFromConfigTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,22 @@ public function it_will_ignore_a_model_if_it_is_excluded_on_config()
4646
$classNames->values()->all()
4747
);
4848
}
49+
50+
/** @test */
51+
public function it_will_only_return_models_in_whitelist_if_present()
52+
{
53+
$this->app['config']->set('erd-generator.whitelist', [
54+
Avatar::class,
55+
]);
56+
57+
$finder = new ModelFinder(app()->make('files'));
58+
59+
$classNames = $finder->getModelsInDirectory(__DIR__ . "/Models");
60+
61+
$this->assertCount(1, $classNames);
62+
$this->assertEquals(
63+
[Avatar::class],
64+
$classNames->values()->all()
65+
);
66+
}
4967
}

0 commit comments

Comments
 (0)