1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 18:14:04 +00:00

Add batch tenancy queue bootstrapper

This commit is contained in:
Riley Schoppa 2022-06-07 20:51:47 -04:00
parent db9480f54e
commit abac147c84
2 changed files with 52 additions and 0 deletions

View file

@ -33,6 +33,7 @@ return [
Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class, Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class, Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class,
// Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed // Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed
// Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class,
], ],
/** /**

View file

@ -0,0 +1,51 @@
<?php
namespace Stancl\Tenancy\Bootstrappers;
use Illuminate\Bus\BatchRepository;
use Illuminate\Bus\DatabaseBatchRepository;
use Illuminate\Support\Facades\DB;
use ReflectionClass;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
class BatchTenancyBootstrapper implements TenancyBootstrapper
{
private $previousConnection = null;
public function bootstrap(Tenant $tenant)
{
$batchRepository = app(BatchRepository::class);
if ($batchRepository instanceof DatabaseBatchRepository) {
/**
* Access the resolved batch repository instance and update its connection to use the tenant connection
*/
$batchRepositoryReflection = new ReflectionClass($batchRepository);
$connectionProperty = $batchRepositoryReflection->getProperty('connection');
$connectionProperty->setAccessible(true);
$connection = $connectionProperty->getValue($batchRepository);
$this->previousConnection = $connection;
$connectionProperty->setValue($batchRepository, DB::connection('tenant'));
}
}
public function revert()
{
if ($this->previousConnection) {
/**
* Access the resolved batch repository instance and replace its connection with the previously replaced one
*/
$batchRepository = app(BatchRepository::class);
$batchRepositoryReflection = new ReflectionClass($batchRepository);
$connectionProperty = $batchRepositoryReflection->getProperty('connection');
$connectionProperty->setAccessible(true);
$connectionProperty->setValue($batchRepository, $this->previousConnection);
$this->previousConnection = null;
}
}
}