diff --git a/composer.json b/composer.json index 0cdb3c66..0ca231c4 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ "doctrine/dbal": "^2.10", "spatie/valuestore": "^1.2.5", "pestphp/pest": "^1.21", - "nunomaduro/larastan": "^1.0", + "nunomaduro/larastan": "^2.4", "spatie/invade": "^1.1" }, "autoload": { diff --git a/src/Concerns/Debuggable.php b/src/Concerns/Debuggable.php deleted file mode 100644 index ff781f89..00000000 --- a/src/Concerns/Debuggable.php +++ /dev/null @@ -1,72 +0,0 @@ -eventLog = []; - $this->logMode = $mode; - - return $this; - } - - public function logMode(): LogMode - { - return $this->logMode; - } - - public function getLog(): array - { - return $this->eventLog; - } - - public function logEvent(TenancyEvent $event): static - { - $this->eventLog[] = ['time' => now(), 'event' => $event::class, 'tenant' => $this->tenant]; - - return $this; - } - - public function dump(Closure $dump = null): static - { - $dump ??= dd(...); - - // Dump the log if we were already logging in silent mode - // Otherwise start logging in instant mode - match ($this->logMode) { - LogMode::NONE => $this->log(LogMode::INSTANT), - LogMode::SILENT => $dump($this->eventLog), - LogMode::INSTANT => null, - }; - - return $this; - } - - public function dd(Closure $dump = null): void - { - $dump ??= dd(...); - - if ($this->logMode === LogMode::SILENT) { - $dump($this->eventLog); - } else { - $dump($this); - } - } -} diff --git a/src/Database/Concerns/BelongsToTenant.php b/src/Database/Concerns/BelongsToTenant.php index ccf87c81..3ca9703c 100644 --- a/src/Database/Concerns/BelongsToTenant.php +++ b/src/Database/Concerns/BelongsToTenant.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Stancl\Tenancy\Database\Concerns; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Database\TenantScope; use Stancl\Tenancy\Tenancy; @@ -13,7 +14,7 @@ use Stancl\Tenancy\Tenancy; */ trait BelongsToTenant { - public function tenant() + public function tenant(): BelongsTo { return $this->belongsTo(config('tenancy.models.tenant'), Tenancy::tenantKeyColumn()); } diff --git a/src/Enums/LogMode.php b/src/Enums/LogMode.php deleted file mode 100644 index 77d6f073..00000000 --- a/src/Enums/LogMode.php +++ /dev/null @@ -1,12 +0,0 @@ - $class */ $class = config('tenancy.models.tenant'); - /** @var Tenant&Model $model */ $model = new $class; return $model; @@ -113,13 +110,9 @@ class Tenancy /** * Try to find a tenant using an ID. - * - * @return (Tenant&Model)|null */ - public static function find(int|string $id): Tenant|null + public static function find(int|string $id): (Tenant&Model)|null { - // todo update all syntax like this once we're fully on PHP 8.2 - /** @var (Tenant&Model)|null */ $tenant = static::model()->where(static::model()->getTenantKeyName(), $id)->first(); return $tenant; diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index ded96f35..9d2d56c4 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -121,18 +121,6 @@ class TenancyServiceProvider extends ServiceProvider $this->loadRoutesFrom(__DIR__ . '/../assets/routes.php'); } - Event::listen('Stancl\\Tenancy\\Events\\*', function (string $name, array $data) { - $event = $data[0]; - - if ($event instanceof TenancyEvent) { - match (tenancy()->logMode()) { - LogMode::SILENT => tenancy()->logEvent($event), - LogMode::INSTANT => dump($event), // todo1 perhaps still log - default => null, - }; - } - }); - $this->app->singleton('globalUrl', function ($app) { if ($app->bound(FilesystemTenancyBootstrapper::class)) { $instance = clone $app['url']; diff --git a/tests/DebuggableTest.php b/tests/DebuggableTest.php deleted file mode 100644 index 49e180d7..00000000 --- a/tests/DebuggableTest.php +++ /dev/null @@ -1,68 +0,0 @@ -log(LogMode::SILENT); - - $tenant = Tenant::create(); - - tenancy()->initialize($tenant); - - tenancy()->end(); - - assertTenancyInitializedAndEnded(tenancy()->getLog(), $tenant); -}); - -test('tenancy logs event silently by default', function () { - tenancy()->log(); - - expect(tenancy()->logMode())->toBe(LogMode::SILENT); -}); - -test('the log can be dumped', function (string $method) { - tenancy()->log(); - - $tenant = Tenant::create(); - - tenancy()->initialize($tenant); - - tenancy()->end(); - - $output = []; - tenancy()->$method(function ($data) use (&$output) { - $output = $data; - }); - - assertTenancyInitializedAndEnded($output, $tenant); -})->with([ - 'dump', - 'dd', -]); - -test('tenancy can log events immediately', function () { - // todo implement - pest()->markTestIncomplete(); -}); - -// todo test the different behavior of the methods in different contexts, or get rid of the logic and simplify it - -function assertTenancyInitializedAndEnded(array $log, Tenant $tenant): void -{ - expect($log)->toHaveCount(4); - - expect($log[0]['event'])->toBe(InitializingTenancy::class); - expect($log[0]['tenant'])->toBe($tenant); - expect($log[1]['event'])->toBe(TenancyInitialized::class); - expect($log[1]['tenant'])->toBe($tenant); - - expect($log[2]['event'])->toBe(EndingTenancy::class); - expect($log[2]['tenant'])->toBe($tenant); - expect($log[3]['event'])->toBe(TenancyEnded::class); - expect($log[3]['tenant'])->toBe($tenant); -}