Skip to content

Instantly share code, notes, and snippets.

@mcelal
Created November 29, 2024 14:43
Show Gist options
  • Save mcelal/e78c8db32b7bd0b45d0046e60afb00e8 to your computer and use it in GitHub Desktop.
Save mcelal/e78c8db32b7bd0b45d0046e60afb00e8 to your computer and use it in GitHub Desktop.
<?php
namespace App\Support\Core;
use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
class ModelFinder
{
public static bool $kuralYonetimi = false;
private static array $folders = [
'app/Models',
'Modules/*/App/Models',
];
private static array $excludedModels = [
BaseModel::class,
];
public static function all($path = null, bool $withExcluded = false): array
{
$list = [];
if ($path) {
self::$folders = [$path];
}
foreach (self::$folders as $modelFolder) {
foreach (File::allFiles(base_path($modelFolder)) as $item) {
$temp = str_replace(base_path('/'), '', $item->getPathname());
$temp = str_replace('app/', 'App/', $temp);
$temp = substr($temp, 0, -4);
$temp = str_replace('/', '\\', $temp);
if (class_exists($temp)
&& ($withExcluded || ! in_array($temp, self::$excludedModels))
&& is_subclass_of($temp, Model::class)
) {
$list[] = $temp;
}
}
}
return ($list);
}
public static function liste(): array
{
return collect(self::all())
->sort()
->map(fn($item) => str_replace(['App\Models\\', 'Modules\\'], '', $item))
->values()
->toArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment