mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 02:44:02 +00:00
Cache prefixing logic rewrite, session scoping improvements, tests refactor (#43)
* Run cache tests on all supported drivers * update ci healthcheck for memcached * remove memcached healthcheck * fix typos in test comments, expand internal.md [ci skip] * add empty line [ci skip] * switch to using $store->setPrefix() * add dynamodb * refactor try-finally to try-catch * remove unnecessary clearResolvedInstances() call * add dual Cache:: and cache() assertions * add apc * Flush APCu cache in test setup * Revert "add dual Cache:: and cache() assertions" This reverts commit a0bab162fbe2dd0d25e7056ceca4fb7ce54efc77. * phpstan fix * Add logic for scoping 'file' disks to FilesystemTenancyBootstrapper * minor changes, add todos * refactor how the session.connection is used in the DB session bootstrapper * add session forgery prevention logic to the db session bootstrapper * only use the fs bootstrapper for file disk in 'cache data is separated' dataset * minor session scoping test changes * Add session scoping logic to FilesystemTenancyBootstrapper, correctly update disk roots even with storage_path_tenancy disabled * Fix code style (php-cs-fixer) * update docblock * make not-null check more explicit * separate bootstrapper tests, fix swapped test names for two tests * refactor cache bootstrapper tests * resolve global cache todo * expand tests: session separation tests, more filesystem separation assertions; change prefix_base-type config keys to templates/formats * add apc session scoping test, various session separation bugfixes * phpstan + minor logic fixes * prefix_format -> prefix * fix database session separation test * revert composer.json changes, update laravel dependencies to expected next release * only run session scoping logic in cache bootstrapper for redis, memcached, dynamodb, apc; update gitattributes * tenancy.central_domains -> tenancy.identification.central_domains * db session separation test: add datasets --------- Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
This commit is contained in:
parent
943b960718
commit
eecf6f21c8
40 changed files with 1856 additions and 1177 deletions
|
|
@ -6,6 +6,7 @@ namespace Stancl\Tenancy\Bootstrappers;
|
|||
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Routing\UrlGenerator;
|
||||
use Illuminate\Session\FileSessionHandler;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
|
@ -37,6 +38,9 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
|||
$this->storagePath($suffix);
|
||||
$this->assetHelper($suffix);
|
||||
$this->forgetDisks();
|
||||
$this->scopeCache($suffix);
|
||||
$this->scopeSessions($suffix);
|
||||
// todo@docs update fs docs
|
||||
|
||||
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) {
|
||||
$this->diskRoot($disk, $tenant);
|
||||
|
|
@ -55,6 +59,8 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
|||
$this->storagePath(false);
|
||||
$this->assetHelper(false);
|
||||
$this->forgetDisks();
|
||||
$this->scopeCache(false);
|
||||
$this->scopeSessions(false);
|
||||
|
||||
foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) {
|
||||
$this->diskRoot($disk, false);
|
||||
|
|
@ -76,10 +82,15 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
|||
if ($suffix === false) {
|
||||
$this->app->useStoragePath($this->originalStoragePath);
|
||||
} else {
|
||||
$this->app->useStoragePath($this->originalStoragePath . "/{$suffix}");
|
||||
$this->app->useStoragePath($this->tenantStoragePath($suffix));
|
||||
}
|
||||
}
|
||||
|
||||
protected function tenantStoragePath(string $suffix): string
|
||||
{
|
||||
return $this->originalStoragePath . "/{$suffix}";
|
||||
}
|
||||
|
||||
protected function assetHelper(string|false $suffix): void
|
||||
{
|
||||
if (! $this->app['config']['tenancy.filesystem.asset_helper_tenancy']) {
|
||||
|
|
@ -125,7 +136,7 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
|||
// This is executed if the disk is in tenancy.filesystem.disks AND has a root_override
|
||||
// This behavior is used for local disks.
|
||||
$newRoot = str($override)
|
||||
->replace('%storage_path%', storage_path())
|
||||
->replace('%storage_path%', $this->tenantStoragePath($suffix))
|
||||
->replace('%original_storage_path%', $this->originalStoragePath)
|
||||
->replace('%tenant%', (string) $tenant->getTenantKey())
|
||||
->toString();
|
||||
|
|
@ -156,4 +167,70 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
|||
$this->app['config']["filesystems.disks.{$disk}.url"] = url($override);
|
||||
}
|
||||
}
|
||||
|
||||
public function scopeCache(string|false $suffix): void
|
||||
{
|
||||
if (! $this->app['config']['tenancy.filesystem.scope_cache']) {
|
||||
return;
|
||||
}
|
||||
|
||||
$storagePath = $suffix
|
||||
? $this->tenantStoragePath($suffix)
|
||||
: $this->originalStoragePath;
|
||||
|
||||
$stores = array_filter($this->app['config']['tenancy.cache.stores'], function ($name) {
|
||||
$store = $this->app['config']["cache.stores.{$name}"];
|
||||
|
||||
if ($store === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $store['driver'] === 'file';
|
||||
});
|
||||
|
||||
foreach ($stores as $name) {
|
||||
$path = $storagePath . '/framework/cache/data';
|
||||
$this->app['config']["cache.stores.{$name}.path"] = $path;
|
||||
$this->app['config']["cache.stores.{$name}.lock_path"] = $path;
|
||||
|
||||
/** @var \Illuminate\Cache\FileStore $store */
|
||||
$store = $this->app['cache']->store($name)->getStore();
|
||||
$store->setDirectory($path);
|
||||
$store->setLockDirectory($path);
|
||||
}
|
||||
}
|
||||
|
||||
public function scopeSessions(string|false $suffix): void
|
||||
{
|
||||
if (! $this->app['config']['tenancy.filesystem.scope_sessions']) {
|
||||
return;
|
||||
}
|
||||
|
||||
$path = $suffix
|
||||
? $this->tenantStoragePath($suffix) . '/framework/sessions'
|
||||
: $this->originalStoragePath . '/framework/sessions';
|
||||
|
||||
if (! is_dir($path)) {
|
||||
// Create tenant framework/sessions directory if it does not exist
|
||||
mkdir($path, 0755, true);
|
||||
}
|
||||
|
||||
$this->app['config']['session.files'] = $path;
|
||||
|
||||
/** @var \Illuminate\Session\SessionManager $sessionManager */
|
||||
$sessionManager = $this->app['session'];
|
||||
|
||||
// Since this bootstrapper runs much earlier than the StartSession middleware, this doesn't execute
|
||||
// on the average tenant request. It only executes when the context is switched *after* original
|
||||
// middleware initialization.
|
||||
if (isset($sessionManager->getDrivers()['file'])) {
|
||||
$handler = new FileSessionHandler(
|
||||
$this->app->make('files'),
|
||||
$path,
|
||||
$this->app['config']->get('session.lifetime'),
|
||||
);
|
||||
|
||||
$sessionManager->getDrivers()['file']->setHandler($handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue