1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 19:14:04 +00:00

Add event prevents, Tenant facade

This commit is contained in:
Samuel Štancl 2019-08-14 22:16:51 +02:00
parent 1a88cad4d6
commit 4aa35322da
9 changed files with 187 additions and 97 deletions

View file

@ -6,7 +6,7 @@ use Stancl\Tenancy\Jobs\QueuedTenantDatabaseCreator;
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseDeleter;
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
class DatabaseManager
final class DatabaseManager
{
public $originalDefaultConnection;
@ -19,8 +19,7 @@ class DatabaseManager
public function connect(string $database)
{
$this->createTenantConnection($database);
$this->database->setDefaultConnection('tenant');
$this->database->reconnect('tenant');
$this->useConnection('tenant');
}
public function connectToTenant($tenant)
@ -105,4 +104,9 @@ class DatabaseManager
$database_name = $this->getDriver() === 'sqlite' ? database_path($database_name) : $database_name;
config()->set(['database.connections.tenant.database' => $database_name]);
}
public function useConnection(string $connection) {
$this->database->setDefaultConnection($connection);
$this->database->reconnect($connection);
}
}

View file

@ -7,7 +7,7 @@ use Stancl\Tenancy\Traits\BootstrapsTenancy;
use Illuminate\Contracts\Foundation\Application;
use Stancl\Tenancy\Exceptions\CannotChangeUuidOrDomainException;
class TenantManager
final class TenantManager
{
use BootstrapsTenancy;
@ -30,7 +30,7 @@ class TenantManager
*
* @var DatabaseManager
*/
protected $database;
public $database;
/**
* Current tenant.

View file

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

View file

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