1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 21:54:02 +00:00

Merge branch 'master' into stein-j-readied-tenant

This commit is contained in:
Abrar Ahmad 2022-08-03 15:00:46 +05:00
commit 3990aa6eb6
42 changed files with 3233 additions and 3508 deletions

View file

@ -0,0 +1,70 @@
<?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;
/**
* @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);
}
}
}

12
src/Enums/LogMode.php Normal file
View file

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

View file

@ -23,11 +23,17 @@ class UniversalRoutes implements Feature
public function bootstrap(Tenancy $tenancy): void
{
foreach (static::$identificationMiddlewares as $middleware) {
$middleware::$onFail = function ($exception, $request, $next) {
$originalOnFail = $middleware::$onFail;
$middleware::$onFail = function ($exception, $request, $next) use ($originalOnFail) {
if (static::routeHasMiddleware($request->route(), static::$middlewareGroup)) {
return $next($request);
}
if ($originalOnFail) {
return $originalOnFail($exception, $request, $next);
}
throw $exception;
};
}

View file

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

View file

@ -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
@ -114,6 +117,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'];