mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 15:54:03 +00:00
wip Debuggable trait
This commit is contained in:
parent
f9c9d8615f
commit
be13160133
5 changed files with 163 additions and 1 deletions
68
src/Concerns/Debuggable.php
Normal file
68
src/Concerns/Debuggable.php
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Concerns;
|
||||
|
||||
use Closure;
|
||||
use Stancl\Tenancy\Enums\LogMode;
|
||||
use Stancl\Tenancy\Events\Contracts\TenancyEvent;
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
}
|
||||
10
src/Enums/LogMode.php
Normal file
10
src/Enums/LogMode.php
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Enums;
|
||||
|
||||
enum LogMode
|
||||
{
|
||||
case NONE;
|
||||
case SILENT;
|
||||
case INSTANT;
|
||||
}
|
||||
|
|
@ -7,13 +7,14 @@ namespace Stancl\Tenancy;
|
|||
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\TenantCouldNotBeIdentifiedById;
|
||||
|
||||
class Tenancy
|
||||
{
|
||||
use Macroable;
|
||||
use Macroable, Debuggable;
|
||||
|
||||
/** @var Tenant|Model|null */
|
||||
public $tenant;
|
||||
|
|
|
|||
|
|
@ -5,10 +5,13 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy;
|
||||
|
||||
use Illuminate\Cache\CacheManager;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Domain;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Enums\LogMode;
|
||||
use Stancl\Tenancy\Events\Contracts\TenancyEvent;
|
||||
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
|
||||
|
||||
class TenancyServiceProvider extends ServiceProvider
|
||||
|
|
@ -112,6 +115,18 @@ 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), // todo0 perhaps still log
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
$this->app->singleton('globalUrl', function ($app) {
|
||||
if ($app->bound(FilesystemTenancyBootstrapper::class)) {
|
||||
$instance = clone $app['url'];
|
||||
|
|
|
|||
68
tests/DebuggableTest.php
Normal file
68
tests/DebuggableTest.php
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
<?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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue