1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 16:24:04 +00:00
This commit is contained in:
Samuel Štancl 2019-09-07 16:11:38 +02:00
parent 87916bd8e5
commit d3712dfacc
3 changed files with 63 additions and 8 deletions

View file

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

View file

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

View file

@ -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;
}, []);
}
}