From ac00f03273dc6b99af647bd1f95c5e84f1cb49f5 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Wed, 7 Jun 2023 14:45:36 +0200 Subject: [PATCH] Move model-related things from Tenancy to a trait --- src/Database/Concerns/DealsWithModels.php | 74 +++++++++++++++++++++++ src/Tenancy.php | 68 +-------------------- 2 files changed, 76 insertions(+), 66 deletions(-) create mode 100644 src/Database/Concerns/DealsWithModels.php diff --git a/src/Database/Concerns/DealsWithModels.php b/src/Database/Concerns/DealsWithModels.php new file mode 100644 index 00000000..11462ffa --- /dev/null +++ b/src/Database/Concerns/DealsWithModels.php @@ -0,0 +1,74 @@ +files()->name('*.php')->in(static::$modelDirectories)->depth('== 0'); + + $classes = collect($modelFiles)->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; + })->filter(); + + return $classes->filter(fn ($class) => in_array(Model::class, class_parents($class))); + } + + public static function getTenantModels(): Collection + { + return static::getModels()->filter(fn (Model $model) => tenancy()->modelBelongsToTenant($model) || tenancy()->modelBelongsToTenantIndirectly($model)); + } + + protected static function getModelFromTable(string $table): Model|null + { + foreach (get_declared_classes() as $class) { + if (is_subclass_of($class, Model::class)) { + $model = new $class; + + if ($model->getTable() === $table) { + return $model; + } + } + } + + return null; + } + + public static function modelBelongsToTenant(Model $model): bool + { + return Schema::hasColumn($model->getTable(), static::tenantKeyColumn()); + } + + public static function modelBelongsToTenantIndirectly(Model $model): bool + { + return in_array(BelongsToPrimaryModel::class, class_uses_recursive($model::class)); + } +} diff --git a/src/Tenancy.php b/src/Tenancy.php index 92c2af0f..86513686 100644 --- a/src/Tenancy.php +++ b/src/Tenancy.php @@ -7,19 +7,15 @@ namespace Stancl\Tenancy; use Closure; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; -use Illuminate\Support\Collection; -use Illuminate\Support\Facades\Schema; use Illuminate\Support\Traits\Macroable; use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\Tenant; -use Stancl\Tenancy\Database\Concerns\BelongsToPrimaryModel; +use Stancl\Tenancy\Database\Concerns\DealsWithModels; use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByIdException; -use Symfony\Component\Finder\Finder; -use Symfony\Component\Finder\SplFileInfo; class Tenancy { - use Macroable; + use Macroable, DealsWithModels; /** * The current tenant. @@ -32,10 +28,6 @@ class Tenancy /** Is tenancy fully initialized? */ public bool $initialized = false; // todo document the difference between $tenant being set and $initialized being true (e.g. end of initialize() method) - public static array $modelDirectories = ['App/Models']; - - public static Closure|null $modelDiscoveryOverride = null; - /** Initialize tenancy for the passed tenant. */ public function initialize(Tenant|int|string $tenant): void { @@ -224,60 +216,4 @@ class Tenancy { return config('tenancy.identification.default_middleware', Middleware\InitializeTenancyByDomain::class); } - - public static function getModels(): Collection - { - if (static::$modelDiscoveryOverride) { - return (static::$modelDiscoveryOverride)(); - } - - $modelFiles = Finder::create()->files()->name('*.php')->in(static::$modelDirectories)->depth('== 0'); - - $classes = collect($modelFiles)->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; - })->filter(); - - return $classes->filter(fn ($class) => in_array(Model::class, class_parents($class))); - } - - public static function getTenantModels(): Collection - { - return static::getModels()->filter(fn (Model $model) => tenancy()->modelBelongsToTenant($model) || tenancy()->modelBelongsToTenantIndirectly($model)); - } - - protected static function getModelFromTable(string $table): Model|null - { - foreach (get_declared_classes() as $class) { - if (is_subclass_of($class, Model::class)) { - $model = new $class; - - if ($model->getTable() === $table) { - return $model; - } - } - } - - return null; - } - - public static function modelBelongsToTenant(Model $model): bool - { - return Schema::hasColumn($model->getTable(), static::tenantKeyColumn()); - } - - public static function modelBelongsToTenantIndirectly(Model $model): bool - { - return in_array(BelongsToPrimaryModel::class, class_uses_recursive($model::class)); - } }