diff --git a/assets/config.php b/assets/config.php index 39c88c11..100bb05f 100644 --- a/assets/config.php +++ b/assets/config.php @@ -56,7 +56,10 @@ return [ 'pgsql' => 'Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager', ], 'tenancy_bootstrappers' => [ - '', + 'database' => 'Stancl\Tenancy\TenancyBootstrappers\DatabaseTenancyBootstrapper', + 'cache' => 'Stancl\Tenancy\TenancyBootstrappers\CacheTenancyBootstrapper', + 'filesystem' => 'Stancl\Tenancy\TenancyBootstrappers\FilesystemTenancyBootstrapper', + 'redis' => 'Stancl\Tenancy\TenancyBootstrappers\RedisTenancyBootstrapper', ], 'queue_database_creation' => false, 'queue_database_deletion' => false, diff --git a/src/Tenant.php b/src/Tenant.php index ce6b74bb..6fe6dd09 100644 --- a/src/Tenant.php +++ b/src/Tenant.php @@ -5,13 +5,14 @@ declare(strict_types=1); namespace Stancl\Tenancy; use ArrayAccess; +use Stancl\Tenancy\Contracts\Tenant as CurrentTenant; // todo tenant storage /** * @internal Class is subject to breaking changes in minor and patch versions. */ -class Tenant implements ArrayAccess, Contracts\Tenant +class Tenant implements ArrayAccess, CurrentTenant { use Traits\HasArrayAccess; @@ -48,7 +49,9 @@ class Tenant implements ArrayAccess, Contracts\Tenant public static function new(): self { - return app(static::class); + return app(static::class)->withData([ + 'id' => static::generateId(), + ]); } public static function fromStorage(array $data): self diff --git a/src/TenantManagerv2.php b/src/TenantManagerv2.php index 7b034c82..32bf27b3 100644 --- a/src/TenantManagerv2.php +++ b/src/TenantManagerv2.php @@ -6,6 +6,9 @@ namespace Stancl\Tenancy; use Illuminate\Foundation\Application; +// todo rethink integration events +// todo events + /** * @internal Class is subject to breaking changes in minor and patch versions. */ @@ -54,7 +57,9 @@ class TenantManagerv2 public function bootstrapTenancy(Tenant $tenant): self { - foreach ($this->tenancyBootstrappers() as $bootstrapper) { + $prevented = $this->event('bootstrapping'); + + foreach ($this->tenancyBootstrappers($prevented) as $bootstrapper) { $this->app[$bootstrapper]->start($tenant); } @@ -63,7 +68,9 @@ class TenantManagerv2 public function endTenancy(): self { - foreach ($this->tenancyBootstrappers() as $bootstrapper) { + $prevented = $this->event('ending'); + + foreach ($this->tenancyBootstrappers($prevented) as $bootstrapper) { $this->app[$bootstrapper]->end(); } @@ -72,7 +79,7 @@ class TenantManagerv2 public function setTenant(Tenant $tenant): self { - $this->app->instance(Tenant::class, $tenant); + $this->app->instance(Contracts\Tenant::class, $tenant); return $this; } @@ -80,10 +87,52 @@ class TenantManagerv2 /** * Return a list of TenancyBoostrappers. * + * @param string[] $except * @return Contracts\TenancyBootstrapper[] */ - public function tenancyBootstrappers(): array + public function tenancyBootstrappers($except = []): array { - return config('tenancy.bootstrappers'); + return array_key_diff(config('tenancy.tenancy_bootstrappers'), $except); + } + + // todo event "listeners" instead of "callbacks" + + /** + * TODO + * + * @param string $name + * @param callable $callback + * @return self|string[] + */ + public function event(string $name, callable $callback = null) + { + if ($callback) { + return $this->addEventCallback($name, $callback); + } + + return $this->executeEventCallbacks($name); + } + + public function addEventCallback(string $name, callable $callback): self + { + isset($this->eventCallbacks[$name]) || $this->eventCallbacks[$name] = []; + $this->eventCallbacks[$name][] = $callback; + + return $this; + } + + /** + * TODO + * + * @param string $name + * @return string[] + */ + public function executeEventCallbacks(string $name): array + { + return array_reduce($this->eventCalbacks[$name] ?? [], function ($prevented, $callback) { + $prevented = array_merge($prevented, $callback($this) ?? []); + + return $prevented; + }, []); } }