mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 00:34: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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue