1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 09:34:04 +00:00

remove debuggable trait, update larastan

This commit is contained in:
Samuel Štancl 2023-01-31 03:59:33 +01:00
parent 87c08cebc4
commit c74a7b6fd4
7 changed files with 7 additions and 177 deletions

View file

@ -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": {

View file

@ -1,72 +0,0 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Concerns;
use Closure;
use Stancl\Tenancy\Enums\LogMode;
use Stancl\Tenancy\Events\Contracts\TenancyEvent;
use Stancl\Tenancy\Tenancy;
// todo finish this feature
/**
* @mixin Tenancy
*/
trait Debuggable
{
protected LogMode $logMode = LogMode::NONE;
protected array $eventLog = [];
public function log(LogMode $mode = LogMode::SILENT): static
{
$this->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);
}
}
}

View file

@ -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());
}

View file

@ -1,12 +0,0 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Enums;
enum LogMode
{
case NONE;
case SILENT;
case INSTANT;
}

View file

@ -8,21 +8,18 @@ use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Traits\Macroable;
use Stancl\Tenancy\Concerns\Debuggable;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByIdException;
class Tenancy
{
use Macroable, Debuggable;
use Macroable;
/**
* The current tenant.
*
* @var (Tenant&Model)|null
*/
public ?Tenant $tenant = null;
public (Tenant&Model)|null $tenant = null;
// todo docblock
public ?Closure $getBootstrappersUsing = null;
@ -97,9 +94,9 @@ class Tenancy
public static function model(): Tenant&Model
{
/** @var class-string<Tenant&Model> $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;

View file

@ -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'];

View file

@ -1,68 +0,0 @@
<?php
use Stancl\Tenancy\Enums\LogMode;
use Stancl\Tenancy\Events\EndingTenancy;
use Stancl\Tenancy\Events\InitializingTenancy;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Tests\Etc\Tenant;
test('tenancy can log events silently', function () {
tenancy()->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);
}