mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-04 19:24:02 +00:00
Move model-related things from Tenancy to a trait
This commit is contained in:
parent
a5a9f1431e
commit
ac00f03273
2 changed files with 76 additions and 66 deletions
74
src/Database/Concerns/DealsWithModels.php
Normal file
74
src/Database/Concerns/DealsWithModels.php
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Support\Collection;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Symfony\Component\Finder\SplFileInfo;
|
||||
use Stancl\Tenancy\Database\Concerns\BelongsToPrimaryModel;
|
||||
|
||||
trait DealsWithModels
|
||||
{
|
||||
public static array $modelDirectories = ['App/Models'];
|
||||
|
||||
public static Closure|null $modelDiscoveryOverride = null;
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue