files()->name('*.php')->in(config('tenancy.rls.model_directories')); return array_filter(array_map(function (SplFileInfo $file) { $fileContents = str($file->getContents()); $class = $fileContents->after('class ')->before("\n")->explode(' ')->first(); if ($fileContents->contains('namespace ')) { try { return new ($fileContents->after('namespace ')->before(';')->toString() . '\\' . $class); } catch (\Throwable $th) { // Skip non-instantiable classes – we only care about models, and those are instantiable } } return null; }, iterator_to_array($modelFiles)), fn (object|null $class) => $class instanceof Model); } /** * Filter all models retrieved by static::getModels() to get only the models that belong to tenants. */ public static function getTenantModels(): array { return array_filter(static::getModels(), fn (Model $model) => static::modelBelongsToTenant($model) || static::modelBelongsToTenantIndirectly($model)); } public static function modelBelongsToTenant(Model $model): bool { return in_array(BelongsToTenant::class, class_uses_recursive($model::class)); } public static function modelBelongsToTenantIndirectly(Model $model): bool { return in_array(BelongsToPrimaryModel::class, class_uses_recursive($model::class)); } }