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

Merge branch 'master' into add-log-bootstrapper

This commit is contained in:
Samuel Štancl 2025-08-25 16:18:20 +02:00 committed by GitHub
commit 412c1d04d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
33 changed files with 1171 additions and 163 deletions

View file

@ -30,6 +30,11 @@ use Stancl\Tenancy\Resolvers\PathTenantResolver;
* By providing a callback to shouldClone(), you can change how it's determined if a route should be cloned if you don't want to use middleware flags.
*
* Cloned routes are prefixed with '/{tenant}', flagged with 'tenant' middleware, and have their names prefixed with 'tenant.'.
*
* The addition of the tenant parameter can be controlled using addTenantParameter(true|false). Note that if you decide to disable
* tenant parameter addition, the routes MUST differ in domains. This can be controlled using the domain(string|null) method. The
* default behavior is to NOT set any domains on cloned routes -- unless specified otherwise using that method.
*
* The parameter name and prefix can be changed e.g. to `/{team}` and `team.` by configuring the path resolver (tenantParameterName and tenantRouteNamePrefix).
* Routes with names that are already prefixed won't be cloned - but that's just the default behavior.
* The cloneUsing() method allows you to completely override the default behavior and customize how the cloned routes will be defined.
@ -43,8 +48,8 @@ use Stancl\Tenancy\Resolvers\PathTenantResolver;
*
* Example usage:
* ```
* Route::get('/foo', fn () => true)->name('foo')->middleware('clone');
* Route::get('/bar', fn () => true)->name('bar')->middleware('universal');
* Route::get('/foo', ...)->name('foo')->middleware('clone');
* Route::get('/bar', ...)->name('bar')->middleware('universal');
*
* $cloneAction = app(CloneRoutesAsTenant::class);
*
@ -54,10 +59,20 @@ use Stancl\Tenancy\Resolvers\PathTenantResolver;
* // Clone bar route as /{tenant}/bar and name it tenant.bar ('universal' middleware won't be present in the cloned route)
* $cloneAction->cloneRoutesWithMiddleware(['universal'])->handle();
*
* Route::get('/baz', fn () => true)->name('baz');
* Route::get('/baz', ...)->name('baz');
*
* // Clone baz route as /{tenant}/bar and name it tenant.baz ('universal' middleware won't be present in the cloned route)
* $cloneAction->cloneRoute('baz')->handle();
*
* Route::domain('central.localhost')->get('/no-tenant-parameter', ...)->name('no-tenant-parameter')->middleware('clone');
*
* // Clone baz route as /no-tenant-parameter and name it tenant.no-tenant-parameter (the route won't have the tenant parameter)
* // This can be useful with domain identification. Importantly, the original route MUST have a domain set. The domain of the
* // cloned route can be customized using domain(string|null). By default, the cloned route will not be scoped to a domain,
* // unless a domain() call is used. It's important to keep in mind that:
* // 1. When addTenantParameter(false) is used, the paths will be the same, thus domains must differ.
* // 2. If the original route (with the same path) has no domain, the cloned route will never be used due to registration order.
* $cloneAction->addTenantParameter(false)->cloneRoutesWithMiddleware(['clone'])->cloneRoute('no-tenant-parameter')->handle();
* ```
*
* Calling handle() will also clear the $routesToClone array.
@ -70,6 +85,8 @@ use Stancl\Tenancy\Resolvers\PathTenantResolver;
class CloneRoutesAsTenant
{
protected array $routesToClone = [];
protected bool $addTenantParameter = true;
protected string|null $domain = null;
protected Closure|null $cloneUsing = null; // The callback should accept Route instance or the route name (string)
protected Closure|null $shouldClone = null;
protected array $cloneRoutesWithMiddleware = ['clone'];
@ -78,6 +95,7 @@ class CloneRoutesAsTenant
protected Router $router,
) {}
/** Clone routes. This resets routesToClone() but not other config. */
public function handle(): void
{
// If no routes were specified using cloneRoute(), get all routes
@ -102,15 +120,37 @@ class CloneRoutesAsTenant
$route = $this->router->getRoutes()->getByName($route);
}
$this->copyMiscRouteProperties($route, $this->createNewRoute($route));
$this->createNewRoute($route);
}
// Clean up the routesToClone array after cloning so that subsequent calls aren't affected
$this->routesToClone = [];
$this->router->getRoutes()->refreshNameLookups();
$this->router->getRoutes()->refreshActionLookups();
}
/**
* Should a tenant parameter be added to the cloned route.
*
* The name of the parameter is controlled using PathTenantResolver::tenantParameterName().
*/
public function addTenantParameter(bool $addTenantParameter): static
{
$this->addTenantParameter = $addTenantParameter;
return $this;
}
/** The domain the cloned route should use. Set to null if it shouldn't be scoped to a domain. */
public function domain(string|null $domain): static
{
$this->domain = $domain;
return $this;
}
/** Provide a custom callback for cloning routes, instead of the default behavior. */
public function cloneUsing(Closure|null $cloneUsing): static
{
$this->cloneUsing = $cloneUsing;
@ -118,6 +158,7 @@ class CloneRoutesAsTenant
return $this;
}
/** Specify which middleware should serve as "flags" telling this action to clone those routes. */
public function cloneRoutesWithMiddleware(array $middleware): static
{
$this->cloneRoutesWithMiddleware = $middleware;
@ -125,6 +166,10 @@ class CloneRoutesAsTenant
return $this;
}
/**
* Provide a custom callback for determining whether a route should be cloned.
* Overrides the default middleware-based detection.
* */
public function shouldClone(Closure|null $shouldClone): static
{
$this->shouldClone = $shouldClone;
@ -132,6 +177,7 @@ class CloneRoutesAsTenant
return $this;
}
/** Clone an individual route. */
public function cloneRoute(Route|string $route): static
{
$this->routesToClone[] = $route;
@ -171,28 +217,31 @@ class CloneRoutesAsTenant
$action->put('as', PathTenantResolver::tenantRouteNamePrefix() . $name);
}
$action
->put('middleware', $middleware)
->put('prefix', $prefix . '/{' . PathTenantResolver::tenantParameterName() . '}');
if ($this->domain) {
$action->put('domain', $this->domain);
} elseif ($action->offsetExists('domain')) {
$action->offsetUnset('domain');
}
$action->put('middleware', $middleware);
if ($this->addTenantParameter) {
$action->put('prefix', $prefix . '/{' . PathTenantResolver::tenantParameterName() . '}');
}
/** @var Route $newRoute */
$newRoute = $this->router->$method($uri, $action->toArray());
return $newRoute;
}
/**
* Copy misc properties of the original route to the new route.
*/
protected function copyMiscRouteProperties(Route $originalRoute, Route $newRoute): void
{
// Copy misc properties of the original route to the new route.
$newRoute
->setBindingFields($originalRoute->bindingFields())
->setFallback($originalRoute->isFallback)
->setWheres($originalRoute->wheres)
->block($originalRoute->locksFor(), $originalRoute->waitsFor())
->withTrashed($originalRoute->allowsTrashedBindings())
->setDefaults($originalRoute->defaults);
->setBindingFields($route->bindingFields())
->setFallback($route->isFallback)
->setWheres($route->wheres)
->block($route->locksFor(), $route->waitsFor())
->withTrashed($route->allowsTrashedBindings())
->setDefaults($route->defaults);
return $newRoute;
}
/** Removes top-level cloneRoutesWithMiddleware and adds 'tenant' middleware. */

View file

@ -0,0 +1,123 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Bootstrappers;
use Exception;
use Illuminate\Cache\CacheManager;
use Illuminate\Cache\DatabaseStore;
use Illuminate\Config\Repository;
use Illuminate\Support\Facades\DB;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\TenancyServiceProvider;
/**
* This bootstrapper allows cache to be stored in tenant databases by switching the database
* connection used by cache stores that use the database driver.
*
* Can be used instead of CacheTenancyBootstrapper.
*
* By default, this bootstrapper scopes ALL cache stores that use the database driver. If you only
* want to scope SOME stores, set the static $stores property to an array of names of the stores
* you want to scope. These stores must use 'database' as their driver.
*
* Notably, this bootstrapper sets TenancyServiceProvider::$adjustCacheManagerUsing to a callback
* that ensures all affected stores still use the central connection when accessed via global cache
* (typicaly the GlobalCache facade or global_cache() helper).
*/
class DatabaseCacheBootstrapper implements TenancyBootstrapper
{
/**
* Cache stores to scope.
*
* If null, all cache stores that use the database driver will be scoped.
* If an array, only the specified stores will be scoped. These all must use the database driver.
*/
public static array|null $stores = null;
/**
* Should scoped stores be adjusted on the global cache manager to use the central connection.
*
* You may want to set this to false if you don't use the built-in global cache and instead provide
* a list of stores to scope (static::$stores), with your own global store excluded that you then
* use manually. But in such a scenario you likely wouldn't be using global cache at all which means
* the callbacks for adjusting it wouldn't be executed in the first place.
*/
public static bool $adjustGlobalCacheManager = true;
public function __construct(
protected Repository $config,
protected CacheManager $cache,
protected array $originalConnections = [],
protected array $originalLockConnections = []
) {}
public function bootstrap(Tenant $tenant): void
{
if (! config('database.connections.tenant')) {
throw new Exception('DatabaseCacheBootstrapper must run after DatabaseTenancyBootstrapper.');
}
$stores = $this->scopedStoreNames();
foreach ($stores as $storeName) {
$this->originalConnections[$storeName] = $this->config->get("cache.stores.{$storeName}.connection");
$this->originalLockConnections[$storeName] = $this->config->get("cache.stores.{$storeName}.lock_connection");
$this->config->set("cache.stores.{$storeName}.connection", 'tenant');
$this->config->set("cache.stores.{$storeName}.lock_connection", 'tenant');
$this->cache->purge($storeName);
}
if (static::$adjustGlobalCacheManager) {
// Preferably we'd try to respect the original value of this static property -- store it in a variable,
// pull it into the closure, and execute it there. But such a naive approach would lead to existing callbacks
// *from here* being executed repeatedly in a loop on reinitialization. For that reason we do not do that
// (this is our only use of $adjustCacheManagerUsing anyway) but ideally at some point we'd have a better solution.
$originalConnections = array_combine($stores, array_map(fn (string $storeName) => [
'connection' => $this->originalConnections[$storeName] ?? config('tenancy.database.central_connection'),
'lockConnection' => $this->originalLockConnections[$storeName] ?? config('tenancy.database.central_connection'),
], $stores));
TenancyServiceProvider::$adjustCacheManagerUsing = static function (CacheManager $manager) use ($originalConnections) {
foreach ($originalConnections as $storeName => $connections) {
/** @var DatabaseStore $store */
$store = $manager->store($storeName)->getStore();
$store->setConnection(DB::connection($connections['connection']));
$store->setLockConnection(DB::connection($connections['lockConnection']));
}
};
}
}
public function revert(): void
{
foreach ($this->originalConnections as $storeName => $originalConnection) {
$this->config->set("cache.stores.{$storeName}.connection", $originalConnection);
$this->config->set("cache.stores.{$storeName}.lock_connection", $this->originalLockConnections[$storeName]);
$this->cache->purge($storeName);
}
TenancyServiceProvider::$adjustCacheManagerUsing = null;
}
protected function scopedStoreNames(): array
{
return array_filter(
static::$stores ?? array_keys($this->config->get('cache.stores', [])),
function ($storeName) {
$store = $this->config->get("cache.stores.{$storeName}");
if (! $store) return false;
if (! isset($store['driver'])) return false;
return $store['driver'] === 'database';
}
);
}
}

View file

@ -86,7 +86,6 @@ class FortifyRouteBootstrapper implements TenancyBootstrapper
protected function useTenantRoutesInFortify(Tenant $tenant): void
{
if (static::$passQueryParameter) {
// todo@tests
$tenantParameterName = RequestDataTenantResolver::queryParameterName();
$tenantParameterValue = RequestDataTenantResolver::payloadValue($tenant);
} else {

View file

@ -51,13 +51,24 @@ class Migrate extends MigrateCommand
return 1;
}
if ($this->getProcesses() > 1) {
return $this->runConcurrently($this->getTenantChunks()->map(function ($chunk) {
return $this->getTenants($chunk);
}));
$originalTemplateConnection = config('tenancy.database.template_tenant_connection');
if ($database = $this->input->getOption('database')) {
config(['tenancy.database.template_tenant_connection' => $database]);
}
return $this->migrateTenants($this->getTenants()) ? 0 : 1;
if ($this->getProcesses() > 1) {
$code = $this->runConcurrently($this->getTenantChunks()->map(function ($chunk) {
return $this->getTenants($chunk);
}));
} else {
$code = $this->migrateTenants($this->getTenants()) ? 0 : 1;
}
// Reset the template tenant connection to the original one
config(['tenancy.database.template_tenant_connection' => $originalTemplateConnection]);
return $code;
}
protected function childHandle(mixed ...$args): bool

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Commands;
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Database\Console\Migrations\BaseCommand;
use Illuminate\Database\QueryException;
use Illuminate\Support\LazyCollection;
@ -17,7 +18,7 @@ use Symfony\Component\Console\Output\OutputInterface as OI;
class MigrateFresh extends BaseCommand
{
use HasTenantOptions, DealsWithMigrations, ParallelCommand;
use HasTenantOptions, DealsWithMigrations, ParallelCommand, ConfirmableTrait;
protected $description = 'Drop all tables and re-run all migrations for tenant(s)';
@ -27,6 +28,7 @@ class MigrateFresh extends BaseCommand
$this->addOption('drop-views', null, InputOption::VALUE_NONE, 'Drop views along with tenant tables.', null);
$this->addOption('step', null, InputOption::VALUE_NONE, 'Force the migrations to be run so they can be rolled back individually.');
$this->addOption('force', null, InputOption::VALUE_NONE, 'Force the command to run when in production.', null);
$this->addProcessesOption();
$this->setName('tenants:migrate-fresh');
@ -34,6 +36,10 @@ class MigrateFresh extends BaseCommand
public function handle(): int
{
if (! $this->confirmToProceed()) {
return 1;
}
$success = true;
if ($this->getProcesses() > 1) {

View file

@ -46,7 +46,7 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
* tenant instances passed to $closeInMemoryConnectionUsing closures,
* if you're setting that property as well.
*
* @property Closure(PDO, string)|null
* @var Closure(PDO, string)|null
*/
public static Closure|null $persistInMemoryConnectionUsing = null;
@ -59,7 +59,7 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
* NOTE: The parameter provided to the closure is the Tenant
* instance, not a PDO connection.
*
* @property Closure(Tenant)|null
* @var Closure(Tenant)|null
*/
public static Closure|null $closeInMemoryConnectionUsing = null;

View file

@ -8,6 +8,9 @@ use Illuminate\Support\Facades\Cache;
class GlobalCache extends Cache
{
/** Make sure this works identically to global_cache() */
protected static $cached = false;
protected static function getFacadeAccessor()
{
return 'globalCache';

View file

@ -20,6 +20,10 @@ class BootstrapTenancy
$tenant = $event->tenancy->tenant;
$bootstrapper->bootstrap($tenant);
if (! in_array($bootstrapper::class, $event->tenancy->initializedBootstrappers)) {
$event->tenancy->initializedBootstrappers[] = $bootstrapper::class;
}
}
event(new TenancyBootstrapped($event->tenancy));

View file

@ -11,18 +11,18 @@ use Stancl\Tenancy\Resolvers\PathTenantResolver;
// todo@earlyIdReview
/**
* Remove the tenant parameter from the matched route when path identification is used globally.
* Conditionally removes the tenant parameter from matched routes when using kernel path identification.
*
* While initializing tenancy, we forget the tenant parameter (in PathTenantResolver),
* so that the route actions don't have to accept it.
* When path identification middleware is in the global stack,
* the tenant parameter is initially forgotten during tenancy initialization in PathTenantResolver.
* However, because kernel identification occurs before route matching, the route still contains
* the tenant parameter when RouteMatched is fired. This listener removes it to prevent route
* actions from needing to accept an unwanted tenant parameter.
*
* With kernel identification, tenancy gets initialized before the route gets matched.
* The matched route gets the tenant parameter again, so we have to forget the parameter again on RouteMatched.
*
* We remove the {tenant} parameter from the matched route when
* 1) the InitializeTenancyByPath middleware is in the global stack, AND
* 2) the matched route does not have identification middleware (so that {tenant} isn't forgotten when using route-level identification), AND
* 3) the route isn't in the central context (so that {tenant} doesn't get accidentally removed from central routes).
* The {tenant} parameter is removed from the matched route only when ALL of these conditions are met:
* 1) A path identification middleware is in the global middleware stack (kernel identification)
* 2) The matched route does NOT have its own identification middleware (route-level identification takes precedence)
* 3) The route is in tenant or universal context (central routes keep their tenant parameter)
*/
class ForgetTenantParameter
{

View file

@ -15,7 +15,9 @@ class RevertToCentralContext
event(new RevertingToCentralContext($event->tenancy));
foreach (array_reverse($event->tenancy->getBootstrappers()) as $bootstrapper) {
$bootstrapper->revert();
if (in_array($bootstrapper::class, $event->tenancy->initializedBootstrappers)) {
$bootstrapper->revert();
}
}
event(new RevertedToCentralContext($event->tenancy));

View file

@ -4,8 +4,8 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Resolvers\Contracts;
use Illuminate\Contracts\Cache\Factory;
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\Eloquent\Model;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Contracts\TenantCouldNotBeIdentifiedException;
@ -13,12 +13,14 @@ use Stancl\Tenancy\Contracts\TenantResolver;
abstract class CachedTenantResolver implements TenantResolver
{
/** @var Repository */
protected $cache;
protected Repository $cache;
public function __construct(Factory $cache)
public function __construct(Application $app)
{
$this->cache = $cache->store(static::cacheStore());
// globalCache should generally not be injected, however in this case
// the class is always created from scratch when calling invalidateCache()
// meaning the global cache stores are also resolved from scratch.
$this->cache = $app->make('globalCache')->store(static::cacheStore());
}
/**

View file

@ -31,7 +31,6 @@ class RequestDataTenantResolver extends Contracts\CachedTenantResolver
public function getPossibleCacheKeys(Tenant&Model $tenant): array
{
// todo@tests
return [
$this->formatCacheKey(static::payloadValue($tenant)),
];

View file

@ -35,6 +35,20 @@ class Tenancy
*/
public static array $findWith = [];
/**
* A list of bootstrappers that have been initialized.
*
* This is used when reverting tenancy, mainly if an exception
* occurs during bootstrapping, to ensure we don't revert
* bootstrappers that haven't been properly initialized
* (bootstrapped for the first time) previously.
*
* @internal
*
* @var list<class-string<TenancyBootstrapper>>
*/
public array $initializedBootstrappers = [];
/** Initialize tenancy for the passed tenant. */
public function initialize(Tenant|int|string $tenant): void
{
@ -192,7 +206,6 @@ class Tenancy
/**
* Run a callback for multiple tenants.
* More performant than running $tenant->run() one by one.
*
* @param array<Tenant>|array<string|int>|\Traversable|string|int|null $tenants
*/

View file

@ -20,6 +20,11 @@ use Stancl\Tenancy\Resolvers\DomainTenantResolver;
class TenancyServiceProvider extends ServiceProvider
{
public static Closure|null $configure = null;
public static bool $registerForgetTenantParameterListener = true;
public static bool $migrateFreshOverride = true;
/** @internal */
public static Closure|null $adjustCacheManagerUsing = null;
/* Register services. */
public function register(): void
@ -79,7 +84,29 @@ class TenancyServiceProvider extends ServiceProvider
});
$this->app->bind('globalCache', function ($app) {
return new CacheManager($app);
// We create a separate CacheManager to be used for "global" cache -- cache that
// is always central, regardless of the current context.
//
// Importantly, we use a regular binding here, not a singleton. Thanks to that,
// any time we resolve this cache manager, we get a *fresh* instance -- an instance
// that was not affected by any scoping logic.
//
// This works great for cache stores that are *directly* scoped, like Redis or
// any other tagged or prefixed stores, but it doesn't work for the database driver.
//
// When we use the DatabaseTenancyBootstrapper, it changes the default connection,
// and therefore the connection of the database store that will be created when
// this new CacheManager is instantiated again.
//
// For that reason, we also adjust the relevant stores on this new CacheManager
// using the callback below. It is set by DatabaseCacheBootstrapper.
$manager = new CacheManager($app);
if (static::$adjustCacheManagerUsing !== null) {
(static::$adjustCacheManagerUsing)($manager);
}
return $manager;
});
}
@ -104,9 +131,11 @@ class TenancyServiceProvider extends ServiceProvider
Commands\CreateUserWithRLSPolicies::class,
]);
$this->app->extend(FreshCommand::class, function ($_, $app) {
return new Commands\MigrateFreshOverride($app['migrator']);
});
if (static::$migrateFreshOverride) {
$this->app->extend(FreshCommand::class, function ($_, $app) {
return new Commands\MigrateFreshOverride($app['migrator']);
});
}
$this->publishes([
__DIR__ . '/../assets/config.php' => config_path('tenancy.php'),
@ -152,6 +181,14 @@ class TenancyServiceProvider extends ServiceProvider
Route::middlewareGroup('tenant', []);
Route::middlewareGroup('central', []);
Event::listen(RouteMatched::class, ForgetTenantParameter::class);
if (static::$registerForgetTenantParameterListener) {
// Ideally, this listener would only be registered when kernel-level
// path identification is used, however doing that check reliably
// at this point in the lifecycle isn't feasible. For that reason,
// rather than doing an "outer" check, we do an "inner" check within
// that listener. That also means the listener needs to be registered
// always. We allow for this to be controlled using a static property.
Event::listen(RouteMatched::class, ForgetTenantParameter::class);
}
}
}