1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 16:14:03 +00:00
This commit is contained in:
Samuel Štancl 2019-08-15 12:13:26 +02:00
commit f183235992
9 changed files with 287 additions and 15 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,10 @@ 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

@ -9,6 +9,8 @@ use Stancl\Tenancy\Exceptions\PhpRedisNotInstalledException;
trait BootstrapsTenancy
{
use TenantManagerEvents;
public $originalSettings = [];
/**
* Was tenancy initialized/bootstrapped?
@ -19,26 +21,55 @@ trait BootstrapsTenancy
public function bootstrap()
{
$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();
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()
{
$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();
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

@ -0,0 +1,85 @@
<?php
namespace Stancl\Tenancy\Traits;
use Illuminate\Support\Collection;
trait TenantManagerEvents
{
/**
* Event listeners.
*
* @var callable[][]
*/
protected $listeners = [
'bootstrapping' => [],
'bootstrapped' => [],
'ending' => [],
'ended' => [],
];
/**
* Register a listener that will be executed before tenancy is bootstrapped.
*
* @param callable $callback
* @return self
*/
public function bootstrapping(callable $callback)
{
$this->listeners['bootstrapping'][] = $callback;
return $this;
}
/**
* Register a listener that will be executed after tenancy is bootstrapped.
*
* @param callable $callback
* @return self
*/
public function bootstrapped(callable $callback)
{
$this->listeners['bootstrapped'][] = $callback;
return $this;
}
/**
* Register a listener that will be executed before tenancy is ended.
*
* @param callable $callback
* @return self
*/
public function ending(callable $callback)
{
$this->listeners['ending'][] = $callback;
return $this;
}
/**
* Register a listener that will be executed after tenancy is ended.
*
* @param callable $callback
* @return self
*/
public function ended(callable $callback)
{
$this->listeners['ended'][] = $callback;
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([]));
}
}