mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:44:04 +00:00
Events
This commit is contained in:
parent
87916bd8e5
commit
d3712dfacc
3 changed files with 63 additions and 8 deletions
|
|
@ -56,7 +56,10 @@ return [
|
||||||
'pgsql' => 'Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager',
|
'pgsql' => 'Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager',
|
||||||
],
|
],
|
||||||
'tenancy_bootstrappers' => [
|
'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_creation' => false,
|
||||||
'queue_database_deletion' => false,
|
'queue_database_deletion' => false,
|
||||||
|
|
|
||||||
|
|
@ -5,13 +5,14 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy;
|
namespace Stancl\Tenancy;
|
||||||
|
|
||||||
use ArrayAccess;
|
use ArrayAccess;
|
||||||
|
use Stancl\Tenancy\Contracts\Tenant as CurrentTenant;
|
||||||
|
|
||||||
// todo tenant storage
|
// todo tenant storage
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal Class is subject to breaking changes in minor and patch versions.
|
* @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;
|
use Traits\HasArrayAccess;
|
||||||
|
|
||||||
|
|
@ -48,7 +49,9 @@ class Tenant implements ArrayAccess, Contracts\Tenant
|
||||||
|
|
||||||
public static function new(): self
|
public static function new(): self
|
||||||
{
|
{
|
||||||
return app(static::class);
|
return app(static::class)->withData([
|
||||||
|
'id' => static::generateId(),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromStorage(array $data): self
|
public static function fromStorage(array $data): self
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,9 @@ namespace Stancl\Tenancy;
|
||||||
|
|
||||||
use Illuminate\Foundation\Application;
|
use Illuminate\Foundation\Application;
|
||||||
|
|
||||||
|
// todo rethink integration events
|
||||||
|
// todo events
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal Class is subject to breaking changes in minor and patch versions.
|
* @internal Class is subject to breaking changes in minor and patch versions.
|
||||||
*/
|
*/
|
||||||
|
|
@ -54,7 +57,9 @@ class TenantManagerv2
|
||||||
|
|
||||||
public function bootstrapTenancy(Tenant $tenant): self
|
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);
|
$this->app[$bootstrapper]->start($tenant);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,7 +68,9 @@ class TenantManagerv2
|
||||||
|
|
||||||
public function endTenancy(): self
|
public function endTenancy(): self
|
||||||
{
|
{
|
||||||
foreach ($this->tenancyBootstrappers() as $bootstrapper) {
|
$prevented = $this->event('ending');
|
||||||
|
|
||||||
|
foreach ($this->tenancyBootstrappers($prevented) as $bootstrapper) {
|
||||||
$this->app[$bootstrapper]->end();
|
$this->app[$bootstrapper]->end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -72,7 +79,7 @@ class TenantManagerv2
|
||||||
|
|
||||||
public function setTenant(Tenant $tenant): self
|
public function setTenant(Tenant $tenant): self
|
||||||
{
|
{
|
||||||
$this->app->instance(Tenant::class, $tenant);
|
$this->app->instance(Contracts\Tenant::class, $tenant);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
@ -80,10 +87,52 @@ class TenantManagerv2
|
||||||
/**
|
/**
|
||||||
* Return a list of TenancyBoostrappers.
|
* Return a list of TenancyBoostrappers.
|
||||||
*
|
*
|
||||||
|
* @param string[] $except
|
||||||
* @return Contracts\TenancyBootstrapper[]
|
* @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;
|
||||||
|
}, []);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue