Skip to content

Commit

Permalink
Merge pull request #5 from Riley19280/feat/recursive-directories
Browse files Browse the repository at this point in the history
Support any level of nested directories
  • Loading branch information
freekmurze authored Sep 13, 2022
2 parents a677928 + b09c591 commit 8e71d10
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
22 changes: 18 additions & 4 deletions src/ModelFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ReflectionClass;
use SplFileInfo;

Expand All @@ -23,9 +24,7 @@ public static function all(
$basePath ??= base_path();
$baseNamespace ??= '';

$globPattern = realpath($directory).'/**/*.php';

return collect(File::glob($globPattern))
return collect(static::getFilesRecursively($directory))
->map(fn (string $class) => new SplFileInfo($class))
->map(fn (SplFileInfo $file) => self::fullQualifiedClassNameFromFile($file, $basePath, $baseNamespace))
->map(function (string $class) {
Expand Down Expand Up @@ -57,4 +56,19 @@ protected static function fullQualifiedClassNameFromFile(
)
->prepend($baseNamespace.'\\');
}

protected static function getFilesRecursively(string $path): array
{
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$files = [];

foreach ($rii as $file) {
if ($file->isDir()) {
continue;
}
$files[] = $file->getPathname();
}

return $files;
}
}
18 changes: 11 additions & 7 deletions tests/ModelFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@

namespace Spatie\ModelInfo\Tests;

use ReflectionClass;
use Spatie\ModelInfo\ModelFinder;
use Spatie\ModelInfo\Tests\TestSupport\Models\ExtraModelInfoModel;
use Spatie\ModelInfo\Tests\TestSupport\Models\Nested\Model\NestedModel;
use Spatie\ModelInfo\Tests\TestSupport\Models\RelationTestModel;
use Spatie\ModelInfo\Tests\TestSupport\Models\TestModel;

it('can discover all models in a directory', function () {
it('can discover all models in a directory', function() {
$models = ModelFinder::all(
$this->getTestSupportDirectory(),
$this->getTestDirectory(),
"Spatie\ModelInfo\Tests",
);

expect($models)->toHaveCount(3);
expect($models)->toHaveCount(4);

/** @var ReflectionClass $firstModel */
$firstModel = $models->first();

expect($firstModel)->toBe(ExtraModelInfoModel::class);
expect($models->toArray())->toEqualCanonicalizing([
NestedModel::class,
ExtraModelInfoModel::class,
RelationTestModel::class,
TestModel::class,
]);
});
2 changes: 1 addition & 1 deletion tests/ModelInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"Spatie\ModelInfo\Tests",
);

expect($modelInfo)->toHaveCount(3);
expect($modelInfo)->toHaveCount(4);
expect($modelInfo->first())->toBeInstanceOf(ModelInfo::class);
});

Expand Down
9 changes: 9 additions & 0 deletions tests/TestSupport/Models/Nested/Model/NestedModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Spatie\ModelInfo\Tests\TestSupport\Models\Nested\Model;

use Illuminate\Database\Eloquent\Model;

class NestedModel extends Model
{
}

0 comments on commit 8e71d10

Please sign in to comment.