1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 01:54:04 +00:00
This commit is contained in:
Samuel Štancl 2019-06-29 13:29:25 +02:00
parent 3abde1a791
commit d2171b5dc5
7 changed files with 58 additions and 13 deletions

View file

@ -18,7 +18,8 @@
"psy/psysh": "@stable", "psy/psysh": "@stable",
"laravel/framework": "5.7.*||5.8.*", "laravel/framework": "5.7.*||5.8.*",
"orchestra/testbench": "~3.7||~3.8", "orchestra/testbench": "~3.7||~3.8",
"league/flysystem-aws-s3-v3": "~1.0" "league/flysystem-aws-s3-v3": "~1.0",
"predis/predis": "^1.1"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View file

@ -0,0 +1,8 @@
<?php
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

@ -71,16 +71,21 @@ class RedisStorageDriver implements StorageDriver
return "tenants:{$hash}"; return "tenants:{$hash}";
}, $uuids); }, $uuids);
// Apparently, the PREFIX is applied to all functions except scan() if (! $hashes) {
$redis_prefix = $this->redis->getOption($this->redis->client()::OPT_PREFIX); // Apparently, the PREFIX is applied to all functions except scan().
$hashes = $hashes ?: $this->redis->scan(null, $redis_prefix.'tenants:*'); // Therefore, if the `tenancy` Redis connection has a prefix set
// (and PhpRedis is used), prepend the prefix to the search.
return array_map(function ($tenant) use ($redis_prefix) { $redis_prefix = '';
// Left strip $redis_prefix from $tenant if (config('database.redis.client') === 'phpredis') {
if (substr($tenant, 0, strlen($redis_prefix)) == $redis_prefix) { $redis_prefix = $this->redis->getOption($this->redis->client()::OPT_PREFIX);
$tenant = substr($tenant, strlen($redis_prefix));
} }
$hashes = array_map(function ($hash) use ($redis_prefix) {
// Left strip $redis_prefix from $hash
return substr($hash, strlen($redis_prefix));
}, $this->redis->scan(null, $redis_prefix.'tenants:*'));
}
return array_map(function ($tenant) {
return $this->redis->hgetall($tenant); return $this->redis->hgetall($tenant);
}, $hashes); }, $hashes);
} }

View file

@ -5,6 +5,8 @@ namespace Stancl\Tenancy\Traits;
use Stancl\Tenancy\CacheManager; use Stancl\Tenancy\CacheManager;
use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Symfony\Component\Debug\Exception\FatalThrowableError;
use Stancl\Tenancy\Exceptions\PhpRedisNotInstalledException;
trait BootstrapsTenancy trait BootstrapsTenancy
{ {
@ -13,7 +15,9 @@ trait BootstrapsTenancy
public function bootstrap() public function bootstrap()
{ {
$this->switchDatabaseConnection(); $this->switchDatabaseConnection();
$this->setPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']); if ($this->app['config']['tenancy.redis.multitenant']) {
$this->setPhpRedisPrefix($this->app['config']['tenancy.redis.prefixed_connections']);
}
$this->tagCache(); $this->tagCache();
$this->suffixFilesystemRootPaths(); $this->suffixFilesystemRootPaths();
} }
@ -28,7 +32,11 @@ trait BootstrapsTenancy
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();
$client->setOption($client::OPT_PREFIX, $prefix); try {
$client->setOption($client::OPT_PREFIX, $prefix);
} catch (\Exception $e) {
throw new PhpRedisNotInstalledException();
}
} }
} }

View file

@ -12,6 +12,7 @@ return [
'suffix' => '', 'suffix' => '',
], ],
'redis' => [ 'redis' => [
'multitenant' => true,
'prefix_base' => 'tenant', 'prefix_base' => 'tenant',
'prefixed_connections' => [ 'prefixed_connections' => [
'default', 'default',

View file

@ -3,6 +3,8 @@
namespace Stancl\Tenancy\Tests; namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Config;
use Stancl\Tenancy\Exceptions\PhpRedisNotInstalledException;
class BootstrapsTenancyTest extends TestCase class BootstrapsTenancyTest extends TestCase
{ {
@ -30,6 +32,26 @@ class BootstrapsTenancyTest extends TestCase
} }
} }
/** @test */
public function predis_is_supported()
{
Config::set('database.redis.client', 'predis');
Config::set('tenancy.redis.multitenant', false);
// assert no exception is thrown from initializing tenancy
$this->assertNotNull($this->initTenancy());
}
/** @test */
public function predis_is_not_supported_without_disabling_redis_multitenancy()
{
Config::set('database.redis.client', 'predis');
// todo
$this->expectException(PhpRedisNotInstalledException::class);
$this->initTenancy();
}
/** @test */ /** @test */
public function filesystem_is_suffixed() public function filesystem_is_suffixed()
{ {

View file

@ -36,7 +36,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
public function initTenancy($domain = 'localhost') public function initTenancy($domain = 'localhost')
{ {
tenancy()->init($domain); return tenancy()->init($domain);
} }
/** /**