From 9262d95ce4a4c67bd05e2d31dd9ab946d84aca7a Mon Sep 17 00:00:00 2001 From: lukinovec Date: Mon, 19 Jun 2023 15:24:39 +0200 Subject: [PATCH] Revert try/catch removal, add/update comments --- src/Database/Concerns/BelongsToTenant.php | 4 ++-- src/Database/Concerns/DealsWithModels.php | 19 ++++++++----------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/Database/Concerns/BelongsToTenant.php b/src/Database/Concerns/BelongsToTenant.php index c4d89039..c9a36a82 100644 --- a/src/Database/Concerns/BelongsToTenant.php +++ b/src/Database/Concerns/BelongsToTenant.php @@ -21,9 +21,9 @@ trait BelongsToTenant public static function bootBelongsToTenant(): void { - // If tenancy.rls.enabled is true or this model implements RlsModel + // If 'tenancy.rls.enabled' is true or this model implements RlsModel // Scope queries using Postgres RLS instead of TenantScope - if (! (config('tenancy.rls.enabled') || (new static) instanceof RLSModel)) { + if (! (config('tenancy.rls.enabled') || (new static) instanceof RlsModel)) { static::addGlobalScope(new TenantScope); } diff --git a/src/Database/Concerns/DealsWithModels.php b/src/Database/Concerns/DealsWithModels.php index bc481f65..3be31932 100644 --- a/src/Database/Concerns/DealsWithModels.php +++ b/src/Database/Concerns/DealsWithModels.php @@ -12,6 +12,9 @@ use Symfony\Component\Finder\SplFileInfo; trait DealsWithModels { + /** + * If this is not null, use this instead of the default model discovery logic. + */ public static Closure|null $modelDiscoveryOverride = null; /** @@ -27,19 +30,13 @@ trait DealsWithModels return array_filter(array_map(function (SplFileInfo $file) { $fileContents = str($file->getContents()); - $className = $fileContents->after('class ')->before("\n")->explode(' ')->first(); + $class = $fileContents->after('class ')->before("\n")->explode(' ')->first(); if ($fileContents->contains('namespace ')) { - /** @var class-string $fullClassName */ - $fullClassName = $fileContents->after('namespace ')->before(';')->toString() . '\\' . $className; - - // Skip non-instantiable classes – we only care about models, and those are instantiable - if ((new ReflectionClass($fullClassName))->getConstructor()?->getNumberOfRequiredParameters() === 0) { - $object = new $fullClassName; - - if ($object instanceof Model) { - return $object; - } + 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 } }