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

Filesystem logic refactor, improved defaults for cache tenancy (#42)

* refactor FilesystemTenancyBootstrapper

* clean up tests and improve coverage

* minor maintenance mode changes

* Improve tenants:migrate --skip-failing logic

* make tenants:migrate output consistently formatted

* minor RootUrlBootstrapper + misc changes

* cache bootstrapper-related improvements

* Fix code style (php-cs-fixer)

* misc refactor

* Fix code style (php-cs-fixer)

* add %original_storage_path% to fs bootstrapper, improve default config for cache

* rename method

* inject concrete implementations where needed instead of abstracts

* Fix code style (php-cs-fixer)

* refactor DealsWithTenantSymlinks

* remove obsolete phpstan ignore

---------

Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
This commit is contained in:
Samuel Štancl 2024-04-02 04:26:10 +02:00 committed by GitHub
parent 4b6fa22aa7
commit a41ad69023
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 234 additions and 160 deletions

View file

@ -20,6 +20,8 @@ use Illuminate\Support\Facades\Route as RouteFacade;
use Stancl\Tenancy\Middleware\InitializeTenancyByPath;
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
// todo update all the docblock sections here, including the Livewire references
class TenancyServiceProvider extends ServiceProvider
{
// By default, no namespace is used to support the callable array syntax.
@ -66,6 +68,7 @@ class TenancyServiceProvider extends ServiceProvider
return $event->tenant;
})->shouldBeQueued(false), // `false` by default, but you probably want to make this `true` for production.
],
Events\TenantMaintenanceModeEnabled::class => [],
Events\TenantMaintenanceModeDisabled::class => [],
@ -137,17 +140,21 @@ class TenancyServiceProvider extends ServiceProvider
protected function overrideUrlInTenantContext(): void
{
/**
* Example of CLI tenant URL root override:
*
* RootUrlBootstrapper::$rootUrlOverride = function (Tenant $tenant) {
* $baseUrl = env('APP_URL');
* $scheme = str($baseUrl)->before('://');
* $hostname = str($baseUrl)->after($scheme . '://');
*
* return $scheme . '://' . $tenant->getTenantKey() . '.' . $hostname;
* };
*/
// Import your tenant model!
// \Stancl\Tenancy\Bootstrappers\RootUrlBootstrapper::$rootUrlOverride = function (Tenant $tenant, string $originalRootUrl) {
// $tenantDomain = $tenant instanceof \Stancl\Tenancy\Contracts\SingleDomainTenant
// ? $tenant->domain
// : $tenant->domains->first()->domain;
//
// $scheme = str($originalRootUrl)->before('://');
//
// // If you're using subdomain identification:
// // $originalDomain = str($originalRootUrl)->after($scheme . '://');
// // return $scheme . '://' . $tenantDomain . '.' . $originalDomain . '/';
//
// // If you're using domain identification:
// return $scheme . '://' . $tenantDomain . '/';
// };
}
public function register()
@ -239,7 +246,7 @@ class TenancyServiceProvider extends ServiceProvider
{
$this->app->booted(function () {
if (file_exists(base_path('routes/tenant.php'))) {
RouteFacade::namespace(static::$controllerNamespace)
RouteFacade::namespace(static::$controllerNamespace)
->middleware('tenant')
->group(base_path('routes/tenant.php'));
}

View file

@ -205,19 +205,29 @@ return [
],
/**
* Cache tenancy config. Used by the custom CacheManager and the PrefixCacheTenancyBootstrapper.
* Cache tenancy config. Used by the CacheTenancyBootstrapper, the CacheTagsBootstrapper, and the custom CacheManager.
*
* This works for all Cache facade calls, cache() helper
* calls and direct calls to injected cache stores.
*
* Each key in cache will have a tag applied on it. This tag is used to
* scope the cache both when writing to it and when reading from it.
* CacheTenancyBootstrapper:
* A prefix is applied *GLOBALLY*, using the `cache.prefix` config. This separates
* one tenant's cache from another's. The list of stores is used for refreshing
* them so that they re-load the prefix from the `cache.prefix` configuration.
*
* CacheTagsBootstrapper:
* Each key in cache will have a tag applied on it. This tag is used to
* scope the cache both when writing to it and when reading from it.
*
* You can clear cache selectively by specifying the tag.
*/
'cache' => [
'prefix_base' => 'tenant', // This prefix_base, followed by the tenant_id, will form a cache prefix that will be used for every cache key.
'stores' => [
env('CACHE_STORE'),
],
'tag_base' => 'tenant', // This tag_base, followed by the tenant_id, will form a tag that will be applied on each cache call.
'prefix_base' => 'tenant_', // This prefix_base, followed by the tenant_id, will form a cache prefix that will be used for every cache key.
],
/**
@ -297,6 +307,7 @@ return [
'prefix_base' => 'tenant', // Each key in Redis will be prepended by this prefix_base, followed by the tenant id.
'prefixed_connections' => [ // Redis connections whose keys are prefixed, to separate one tenant's keys from another.
'default',
// 'cache', // Enable this if you want to scope cache using RedisTenancyBootstrapper
],
],