mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-14 11:54:04 +00:00
Add event prevents, Tenant facade
This commit is contained in:
parent
1a88cad4d6
commit
4aa35322da
9 changed files with 187 additions and 97 deletions
|
|
@ -21,42 +21,55 @@ trait BootstrapsTenancy
|
|||
|
||||
public function bootstrap()
|
||||
{
|
||||
array_map(function ($listener) {
|
||||
$listener($this);
|
||||
}, $this->listeners['bootstrapping']);
|
||||
|
||||
$prevented = $this->event('bootstrapping');
|
||||
$this->initialized = true;
|
||||
|
||||
$this->switchDatabaseConnection();
|
||||
if ($this->app['config']['tenancy.redis.tenancy']) {
|
||||
$this->setPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
|
||||
if (! $prevented->contains('database')) {
|
||||
$this->switchDatabaseConnection();
|
||||
}
|
||||
$this->tagCache();
|
||||
$this->suffixFilesystemRootPaths();
|
||||
|
||||
array_map(function ($listener) {
|
||||
$listener($this);
|
||||
}, $this->listeners['bootstrapped']);
|
||||
if (! $prevented->contains('redis')) {
|
||||
if ($this->app['config']['tenancy.redis.tenancy']) {
|
||||
$this->setPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
|
||||
}
|
||||
}
|
||||
|
||||
if (! $prevented->contains('cache')) {
|
||||
$this->tagCache();
|
||||
}
|
||||
|
||||
if (! $prevented->contains('filesystem')) {
|
||||
$this->suffixFilesystemRootPaths();
|
||||
}
|
||||
|
||||
$this->event('bootstrapped');
|
||||
}
|
||||
|
||||
public function end()
|
||||
{
|
||||
array_map(function ($listener) {
|
||||
$listener($this);
|
||||
}, $this->listeners['ending']);
|
||||
$prevented = $this->event('ending');
|
||||
|
||||
$this->initialized = false;
|
||||
|
||||
$this->disconnectDatabase();
|
||||
if ($this->app['config']['tenancy.redis.tenancy']) {
|
||||
$this->resetPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
|
||||
if (! $prevented->contains('database')) {
|
||||
$this->disconnectDatabase();
|
||||
}
|
||||
$this->untagCache();
|
||||
$this->resetFileSystemRootPaths();
|
||||
|
||||
array_map(function ($listener) {
|
||||
$listener($this);
|
||||
}, $this->listeners['ended']);
|
||||
if (! $prevented->contains('redis')) {
|
||||
if ($this->app['config']['tenancy.redis.tenancy']) {
|
||||
$this->resetPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
|
||||
}
|
||||
}
|
||||
|
||||
if (! $prevented->contains('cache')) {
|
||||
$this->untagCache();
|
||||
}
|
||||
|
||||
if (! $prevented->contains('filesystem')) {
|
||||
$this->resetFileSystemRootPaths();
|
||||
}
|
||||
|
||||
$this->event('ended');
|
||||
}
|
||||
|
||||
public function switchDatabaseConnection()
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
namespace Stancl\Tenancy\Traits;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
trait TenantManagerEvents
|
||||
{
|
||||
/**
|
||||
|
|
@ -67,4 +69,17 @@ trait TenantManagerEvents
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire an event.
|
||||
*
|
||||
* @param string $name Event name
|
||||
* @return Collection Prevented events
|
||||
*/
|
||||
public function event(string $name): Collection
|
||||
{
|
||||
return array_reduce($this->listeners[$name], function ($prevents, $listener) {
|
||||
return $prevents->merge($listener($this));
|
||||
}, collect([]));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue