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

Tenant contract, Tenancy bootstrappers, drop predis

This commit is contained in:
Samuel Štancl 2019-09-06 18:51:34 +02:00
parent 98cb49b1c0
commit f04ca349bd
13 changed files with 158 additions and 47 deletions

6
src/Contracts/Tenant.php Normal file
View file

@ -0,0 +1,6 @@
<?php
namespace Stancl\Tenancy\Contracts;
/** Empty interface implemented by Stancl\Tenancy\Tenant have a dependency-injectable contract for the current tenant. */
interface Tenant {}

View file

@ -1,10 +0,0 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Exceptions;
class PhpRedisNotInstalledException extends \Exception
{
protected $message = 'PhpRedis is not installed. PhpRedis is required for Redis multi-tenancy because Predis does not support prefixes.';
}

View file

@ -0,0 +1,26 @@
<?php
namespace Stancl\Tenancy\TenancyBoostrappers;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
class CacheTenancyBootstrapped implements TenancyBootstrapper
{
/** @var \Illuminate\Cache\CacheManager */
protected $originalCache;
public function start()
{
$this->originalCache = $this->originalCache ?? $this->app['cache'];
$this->app->extend('cache', function () {
return new CacheManager($this->app);
});
}
public function end()
{
$this->app->extend('cache', function () {
return $this->originalCache;
});
}
}

View file

@ -0,0 +1,57 @@
<?php
namespace Stancl\Tenancy\TenancyBoostrappers;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
// todo better solution than tenant_asset?
class FilesystemTenancyBootstrapper implements TenancyBootstrapper
{
protected $originalPaths = [];
/** @var Application */
protected $app;
public function __construct(Application $app)
{
$this->app = $app;
$this->originalPaths = [
'disks' => [],
'path' => $this->app->storagePath(),
];
}
public function start()
{
// todo revisit this
$suffix = $this->app['config']['tenancy.filesystem.suffix_base'] . tenant('uuid');
// storage_path()
$this->app->useStoragePath($this->originalPaths['path'] . "/{$suffix}");
// Storage facade
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) {
$this->originalPaths['disks'][$disk] = Storage::disk($disk)->getAdapter()->getPathPrefix();
if ($root = \str_replace('%storage_path%', storage_path(), $this->app['config']["tenancy.filesystem.root_override.{$disk}"])) {
Storage::disk($disk)->getAdapter()->setPathPrefix($root);
} else {
$root = $this->app['config']["filesystems.disks.{$disk}.root"];
Storage::disk($disk)->getAdapter()->setPathPrefix($root . "/{$suffix}");
}
}
}
public function end()
{
// storage_path()
$this->app->useStoragePath($this->originalPaths['path']);
// Storage facade
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) {
Storage::disk($disk)->getAdapter()->setPathPrefix($this->originalPaths['disks'][$disk]);
}
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace Stancl\Tenancy\TenantDatabaseManagers;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
class RedisTenancyBootstrapper implements TenancyBootstrapper
{
/** @var string[string] Original prefixes of connections */
protected $originalPrefixes = [];
/** @var Application */
protected $app;
public function __construct(Application $app)
{
$this->app = $app;
}
public function start()
{
foreach ($this->prefixedConnections() as $connection) {
$prefix = $this->app['config']['tenancy.redis.prefix_base'] . $this->tenant['uuid'];
$client = Redis::connection($connection)->client();
$this->originalPrefixes[$connection] = $client->getOption($client::OPT_PREFIX);
$client->setOption($client::OPT_PREFIX, $prefix);
}
}
public function end()
{
foreach ($this->prefixedConnections() as $connection) {
$client = Redis::connection($connection)->client();
$client->setOption($client::OPT_PREFIX, $this->originalPrefixes[$connection]);
}
}
protected function prefixedConnections()
{
return config('tenancy.redis.prefixed_connections');
}
}

View file

@ -132,6 +132,8 @@ class TenancyServiceProvider extends ServiceProvider
);
});
// todo foreach bootstrappers, singleton
$this->app->singleton(Migrate::class, function ($app) {
return new Migrate($app['migrator'], $app[DatabaseManager::class]);
});

View file

@ -6,12 +6,10 @@ namespace Stancl\Tenancy;
use ArrayAccess;
// todo laravel events instead of custom events?
/**
* @internal Class is subject to breaking changes in minor and patch versions.
*/
class Tenant implements ArrayAccess
class Tenant implements ArrayAccess, Contracts\Tenant
{
use Traits\HasArrayAccess;
@ -56,7 +54,7 @@ class Tenant implements ArrayAccess
return app(static::class)->withData($data)->persisted();
}
public function persisted()
protected function persisted()
{
$this->persisted = true;

View file

@ -55,7 +55,16 @@ class TenantManagerv2
public function bootstrapTenancy(Tenant $tenant): self
{
foreach($this->tenancyBootstrappers() as $bootstrapper) {
$bootstrapper::start($tenant);
$this->app[$bootstrapper]->start($tenant);
}
return $this;
}
public function endTenancy(): self
{
foreach($this->tenancyBootstrappers() as $bootstrapper) {
$this->app[$bootstrapper]->end();
}
return $this;

View file

@ -6,6 +6,8 @@ namespace Stancl\Tenancy;
use Illuminate\Database\Eloquent\Model;
// todo move this to a database driver domain?
/**
* @final Class is subject to breaking changes in minor and patch versions.
*/