1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 15:54:03 +00:00

Add tenancy()->end() (#68)

This commit is contained in:
Samuel Štancl 2019-07-13 11:38:07 +02:00 committed by GitHub
parent b2e2460c34
commit 353f7afb82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 132 additions and 7 deletions

View file

@ -185,6 +185,7 @@ class TenantManager
/** /**
* Reconnects to the default database. * Reconnects to the default database.
* @todo More descriptive name?
* *
* @return void * @return void
*/ */

View file

@ -21,6 +21,16 @@ trait BootstrapsTenancy
$this->suffixFilesystemRootPaths(); $this->suffixFilesystemRootPaths();
} }
public function end()
{
$this->disconnectDatabase();
if ($this->app['config']['tenancy.redis.tenancy']) {
$this->resetPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
}
$this->untagCache();
$this->resetFileSystemRootPaths();
}
public function switchDatabaseConnection() public function switchDatabaseConnection()
{ {
$this->database->connect($this->getDatabaseName()); $this->database->connect($this->getDatabaseName());
@ -28,10 +38,14 @@ trait BootstrapsTenancy
public function setPhpRedisPrefix($connections = ['default']) public function setPhpRedisPrefix($connections = ['default'])
{ {
$this->originalSettings['redis'] = $this->originalSettings['redis'] ?? [];
foreach ($connections as $connection) { foreach ($connections as $connection) {
$prefix = $this->app['config']['tenancy.redis.prefix_base'] . $this->tenant['uuid']; $prefix = $this->app['config']['tenancy.redis.prefix_base'] . $this->tenant['uuid'];
$client = Redis::connection($connection)->client(); $client = Redis::connection($connection)->client();
try { try {
$this->originalSettings['redis'][$connection] = $client->getOption($client::OPT_PREFIX);
$client->setOption($client::OPT_PREFIX, $prefix); $client->setOption($client::OPT_PREFIX, $prefix);
} catch (\Throwable $t) { } catch (\Throwable $t) {
throw new PhpRedisNotInstalledException(); throw new PhpRedisNotInstalledException();
@ -39,27 +53,49 @@ trait BootstrapsTenancy
} }
} }
public function resetPhpRedisPrefix($connections = ['default']) {
foreach ($connections as $connection) {
$client = Redis::connection($connection)->client();
try {
$client->setOption($client::OPT_PREFIX, $this->originalSettings['redis'][$connection]);
} catch (\Throwable $t) {
throw new PhpRedisNotInstalledException();
}
}
}
public function tagCache() public function tagCache()
{ {
$this->originalSettings['cache'] = $this->app['cache'];
$this->app->extend('cache', function () { $this->app->extend('cache', function () {
return new CacheManager($this->app); return new CacheManager($this->app);
}); });
} }
public function untagCache()
{
$this->app->extend('cache', function () {
return $this->originalSettings['cache'];
});
}
public function suffixFilesystemRootPaths() public function suffixFilesystemRootPaths()
{ {
$old = $this->originalSettings ?: [ $old = $this->originalSettings['storage'] ?? [
"storage_disks" => [], 'disks' => [],
"storage_path" => $this->app->storagePath(), 'path' => $this->app->storagePath(),
]; ];
$suffix = $this->app['config']['tenancy.filesystem.suffix_base'] . tenant('uuid'); $suffix = $this->app['config']['tenancy.filesystem.suffix_base'] . tenant('uuid');
// storage_path() // storage_path()
$this->app->useStoragePath($old['storage_path'] . "/{$suffix}"); $this->app->useStoragePath($old['path'] . "/{$suffix}");
// Storage facade // Storage facade
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) { foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) {
$old['disks'][$disk] = Storage::disk($disk)->getAdapter()->getPathPrefix();
if ($root = str_replace('%storage_path%', storage_path(), $this->app['config']["tenancy.filesystem.root_override.{$disk}"])) { if ($root = str_replace('%storage_path%', storage_path(), $this->app['config']["tenancy.filesystem.root_override.{$disk}"])) {
Storage::disk($disk)->getAdapter()->setPathPrefix($root); Storage::disk($disk)->getAdapter()->setPathPrefix($root);
} else { } else {
@ -67,10 +103,19 @@ trait BootstrapsTenancy
Storage::disk($disk)->getAdapter()->setPathPrefix($root . "/{$suffix}"); Storage::disk($disk)->getAdapter()->setPathPrefix($root . "/{$suffix}");
} }
$old['storage_disks'][$disk] = $root;
} }
$this->originalSettings = $old; $this->originalSettings['storage'] = $old;
}
public function resetFilesystemRootPaths()
{
// storage_path()
$this->app->useStoragePath($this->originalSettings['storage']['path']);
// Storage facade
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) {
Storage::disk($disk)->getAdapter()->setPathPrefix($this->originalSettings['storage']['disks'][$disk]);
}
} }
} }

View file

@ -2,6 +2,9 @@
namespace Stancl\Tenancy\Tests; namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
class TenantManagerTest extends TestCase class TenantManagerTest extends TestCase
{ {
public $autoCreateTenant = false; public $autoCreateTenant = false;
@ -98,4 +101,80 @@ class TenantManagerTest extends TestCase
$this->expectException(\Exception::class); $this->expectException(\Exception::class);
tenancy()->findByDomain('nonexistent.domain'); tenancy()->findByDomain('nonexistent.domain');
} }
/** @test */
public function tenancy_can_be_ended()
{
$originals = [
'databasePDO' => DB::connection()->getPDO(),
'databaseName' => DB::connection()->getDatabaseName(),
'storage_path' => storage_path(),
'storage_root' => Storage::disk('local')->getAdapter()->getPathPrefix(),
'cache' => app('cache'),
];
// Verify that these assertions are the right way for testing this
$this->assertSame($originals['databaseName'], DB::connection()->getDatabaseName());
$this->assertSame($originals['storage_path'], storage_path());
$this->assertSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
$this->assertSame($originals['cache'], app('cache'));
tenant()->create('foo.localhost');
tenancy()->init('foo.localhost');
$this->assertNotSame($originals['databaseName'], DB::connection()->getDatabaseName());
$this->assertNotSame($originals['storage_path'], storage_path());
$this->assertNotSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
$this->assertNotSame($originals['cache'], app('cache'));
tenancy()->end();
$this->assertSame($originals['databaseName'], DB::connection()->getDatabaseName());
$this->assertSame($originals['storage_path'], storage_path());
$this->assertSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
$this->assertSame($originals['cache'], app('cache'));
}
/** @test */
public function tenancy_can_be_ended_after_reidentification()
{
$originals = [
'databasePDO' => DB::connection()->getPDO(),
'databaseName' => DB::connection()->getDatabaseName(),
'storage_path' => storage_path(),
'storage_root' => Storage::disk('local')->getAdapter()->getPathPrefix(),
'cache' => app('cache'),
];
tenant()->create('foo.localhost');
tenancy()->init('foo.localhost');
$this->assertNotSame($originals['databaseName'], DB::connection()->getDatabaseName());
$this->assertNotSame($originals['storage_path'], storage_path());
$this->assertNotSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
$this->assertNotSame($originals['cache'], app('cache'));
tenancy()->end();
$this->assertSame($originals['databaseName'], DB::connection()->getDatabaseName());
$this->assertSame($originals['storage_path'], storage_path());
$this->assertSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
$this->assertSame($originals['cache'], app('cache'));
// Reidentify tenant
tenant()->create('bar.localhost');
tenancy()->init('bar.localhost');
$this->assertNotSame($originals['databaseName'], DB::connection()->getDatabaseName());
$this->assertNotSame($originals['storage_path'], storage_path());
$this->assertNotSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
$this->assertNotSame($originals['cache'], app('cache'));
tenancy()->end();
$this->assertSame($originals['databaseName'], DB::connection()->getDatabaseName());
$this->assertSame($originals['storage_path'], storage_path());
$this->assertSame($originals['storage_root'], Storage::disk('local')->getAdapter()->getPathPrefix());
$this->assertSame($originals['cache'], app('cache'));
}
} }