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

Enable caching in initialization mw

This commit is contained in:
Samuel Štancl 2020-05-22 12:34:25 +02:00
parent d7c04d304d
commit 800e8d5a56
7 changed files with 116 additions and 3 deletions

View file

@ -6,6 +6,7 @@ namespace Stancl\Tenancy\Middleware;
use Stancl\Tenancy\Contracts\TenantCouldNotBeIdentifiedException; use Stancl\Tenancy\Contracts\TenantCouldNotBeIdentifiedException;
use Stancl\Tenancy\Contracts\TenantResolver; use Stancl\Tenancy\Contracts\TenantResolver;
use Stancl\Tenancy\Resolvers\CachedTenantResolver;
use Stancl\Tenancy\Tenancy; use Stancl\Tenancy\Tenancy;
abstract class IdentificationMiddleware abstract class IdentificationMiddleware
@ -13,6 +14,15 @@ abstract class IdentificationMiddleware
/** @var callable */ /** @var callable */
public static $onFail; public static $onFail;
/** @var bool */
public static $shouldCache = false;
/** @var int */
public static $cacheTTL = 3600; // seconds
/** @var string|null */
public static $cacheStore = null; // default
/** @var Tenancy */ /** @var Tenancy */
protected $tenancy; protected $tenancy;
@ -22,9 +32,15 @@ abstract class IdentificationMiddleware
public function initializeTenancy($request, $next, ...$resolverArguments) public function initializeTenancy($request, $next, ...$resolverArguments)
{ {
try { try {
if (static::$shouldCache) {
app(CachedTenantResolver::class)->resolve(
get_class($this->resolver), $resolverArguments, static::$cacheTTL, static::$cacheStore
);
} else {
$this->tenancy->initialize( $this->tenancy->initialize(
$this->resolver->resolve(...$resolverArguments) $this->resolver->resolve(...$resolverArguments)
); );
}
} catch (TenantCouldNotBeIdentifiedException $e) { } catch (TenantCouldNotBeIdentifiedException $e) {
$onFail = static::$onFail ?? function ($e) { $onFail = static::$onFail ?? function ($e) {
throw $e; throw $e;

View file

@ -13,6 +13,15 @@ class InitializeTenancyByDomain extends IdentificationMiddleware
/** @var callable|null */ /** @var callable|null */
public static $onFail; public static $onFail;
/** @var bool */
public static $shouldCache = false;
/** @var int */
public static $cacheTTL = 3600; // seconds
/** @var string|null */
public static $cacheStore = null; // default
/** @var Tenancy */ /** @var Tenancy */
protected $tenancy; protected $tenancy;

View file

@ -13,6 +13,18 @@ use Stancl\Tenancy\Tenancy;
class InitializeTenancyByPath extends IdentificationMiddleware class InitializeTenancyByPath extends IdentificationMiddleware
{ {
/** @var callable|null */
public static $onFail;
/** @var bool */
public static $shouldCache = false;
/** @var int */
public static $cacheTTL = 3600; // seconds
/** @var string|null */
public static $cacheStore = null; // default
/** @var Tenancy */ /** @var Tenancy */
protected $tenancy; protected $tenancy;

View file

@ -17,6 +17,18 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware
/** @var string|null */ /** @var string|null */
public static $queryParameter = 'tenant'; public static $queryParameter = 'tenant';
/** @var callable|null */
public static $onFail;
/** @var bool */
public static $shouldCache = false;
/** @var int */
public static $cacheTTL = 3600; // seconds
/** @var string|null */
public static $cacheStore = null; // default
/** @var Tenancy */ /** @var Tenancy */
protected $tenancy; protected $tenancy;

View file

@ -23,6 +23,18 @@ class InitializeTenancyBySubdomain extends InitializeTenancyByDomain
*/ */
public static $subdomainIndex = 0; public static $subdomainIndex = 0;
/** @var callable|null */
public static $onFail;
/** @var bool */
public static $shouldCache = false;
/** @var int */
public static $cacheTTL = 3600; // seconds
/** @var string|null */
public static $cacheStore = null; // default
/** /**
* Handle an incoming request. * Handle an incoming request.
* *

View file

@ -22,6 +22,8 @@ class CachedTenantResolver implements TenantResolver
{ {
$resolverClass = $args[0]; $resolverClass = $args[0];
$data = $args[1]; $data = $args[1];
$ttl = $args[2];
$cacheStore = $args[3];
/** @var TenantResolver $resolver */ /** @var TenantResolver $resolver */
$resolver = app($resolverClass); $resolver = app($resolverClass);

View file

@ -4,12 +4,23 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Tests; namespace Stancl\Tenancy\Tests;
use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
use Stancl\Tenancy\Resolvers\CachedTenantResolver; use Stancl\Tenancy\Resolvers\CachedTenantResolver;
use Stancl\Tenancy\Resolvers\DomainTenantResolver; use Stancl\Tenancy\Resolvers\DomainTenantResolver;
use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Tests\Etc\Tenant;
class CachedTenantResolverTest extends TestCase class CachedTenantResolverTest extends TestCase
{ {
public function tearDown(): void
{
InitializeTenancyByDomain::$shouldCache = false;
InitializeTenancyByRequestData::$shouldCache = false;
parent::tearDown();
}
/** @test */ /** @test */
public function tenants_can_be_resolved_using_the_cached_resolver() public function tenants_can_be_resolved_using_the_cached_resolver()
{ {
@ -38,4 +49,43 @@ class CachedTenantResolverTest extends TestCase
$this->assertTrue($tenant->is(app(CachedTenantResolver::class)->resolve(DomainTenantResolver::class, ['acme']))); $this->assertTrue($tenant->is(app(CachedTenantResolver::class)->resolve(DomainTenantResolver::class, ['acme'])));
} }
/** @test */
public function caching_can_be_configured_selectively_on_initialization_middleware()
{
InitializeTenancyByDomain::$shouldCache = true;
$tenant = Tenant::create([
'id' => 'acme',
]);
$tenant->domains()->create([
'domain' => 'acme.localhost',
]);
Route::get('/foo', function () {
return 'bar';
})->middleware(InitializeTenancyByDomain::class);
// create cache
$this->get('http://acme.localhost/foo')
->assertSee('bar');
$this->mock(CachedTenantResolver::class, function ($mock) {
return $mock->shouldReceive('resolve')->once(); // only once
});
// use cache
$this->get('http://acme.localhost/foo')
->assertSee('bar');
Route::get('/abc', function () {
return 'xyz';
})->middleware(InitializeTenancyByRequestData::class);
$this->get('/abc?tenant=acme')
->assertSee('xyz');
$this->get('/abc?tenant=acme')
->assertSee('xyz');
}
} }