mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-15 20:34:04 +00:00
Merge branch 'master' into cache-prefix
This commit is contained in:
commit
ba8cfdda85
38 changed files with 631 additions and 362 deletions
|
|
@ -79,9 +79,9 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
|
|||
$dispatcher->listen(JobFailed::class, $revertToPreviousState); // artisan('queue:work') which fails
|
||||
}
|
||||
|
||||
protected static function initializeTenancyForQueue(string|int $tenantId): void
|
||||
protected static function initializeTenancyForQueue(string|int|null $tenantId): void
|
||||
{
|
||||
if (! $tenantId) {
|
||||
if ($tenantId === null) {
|
||||
// The job is not tenant-aware
|
||||
if (tenancy()->initialized) {
|
||||
// Tenancy was initialized, so we revert back to the central context
|
||||
|
|
|
|||
66
src/Bootstrappers/SessionTenancyBootstrapper.php
Normal file
66
src/Bootstrappers/SessionTenancyBootstrapper.php
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Bootstrappers;
|
||||
|
||||
use Illuminate\Config\Repository;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Illuminate\Session\DatabaseSessionHandler;
|
||||
use Illuminate\Session\SessionManager;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
/**
|
||||
* This resets the database connection used by the database session driver.
|
||||
*
|
||||
* It runs each time tenancy is initialized or ended.
|
||||
* That way the session driver always uses the current DB connection.
|
||||
*/
|
||||
class SessionTenancyBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
public function __construct(
|
||||
protected Repository $config,
|
||||
protected Container $container,
|
||||
protected SessionManager $session,
|
||||
) {
|
||||
}
|
||||
|
||||
public function bootstrap(Tenant $tenant): void
|
||||
{
|
||||
$this->resetDatabaseHandler();
|
||||
}
|
||||
|
||||
public function revert(): void
|
||||
{
|
||||
// When ending tenancy, this runs *before* the DatabaseTenancyBootstrapper, so DB tenancy
|
||||
// is still bootstrapped. For that reason, we have to explicitly use the central connection
|
||||
$this->resetDatabaseHandler(config('tenancy.database.central_connection'));
|
||||
}
|
||||
|
||||
protected function resetDatabaseHandler(string $defaultConnection = null): void
|
||||
{
|
||||
$sessionDrivers = $this->session->getDrivers();
|
||||
|
||||
if (isset($sessionDrivers['database'])) {
|
||||
/** @var \Illuminate\Session\Store $databaseDriver */
|
||||
$databaseDriver = $sessionDrivers['database'];
|
||||
|
||||
$databaseDriver->setHandler($this->createDatabaseHandler($defaultConnection));
|
||||
}
|
||||
}
|
||||
|
||||
protected function createDatabaseHandler(string $defaultConnection = null): DatabaseSessionHandler
|
||||
{
|
||||
// Typically returns null, so this falls back to the default DB connection
|
||||
$connection = $this->config->get('session.connection') ?? $defaultConnection;
|
||||
|
||||
// Based on SessionManager::createDatabaseDriver
|
||||
return new DatabaseSessionHandler(
|
||||
$this->container->make('db')->connection($connection),
|
||||
$this->config->get('session.table'),
|
||||
$this->config->get('session.lifetime'),
|
||||
$this->container,
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue