From abac147c84405d9e4eb379041c646cece4fdac7e Mon Sep 17 00:00:00 2001 From: Riley Schoppa Date: Tue, 7 Jun 2022 20:51:47 -0400 Subject: [PATCH] Add batch tenancy queue bootstrapper --- assets/config.php | 1 + .../BatchTenancyBootstrapper.php | 51 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/Bootstrappers/BatchTenancyBootstrapper.php diff --git a/assets/config.php b/assets/config.php index 85592d14..1c613664 100644 --- a/assets/config.php +++ b/assets/config.php @@ -33,6 +33,7 @@ return [ Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class, Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class, // Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed + // Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class, ], /** diff --git a/src/Bootstrappers/BatchTenancyBootstrapper.php b/src/Bootstrappers/BatchTenancyBootstrapper.php new file mode 100644 index 00000000..ae920d24 --- /dev/null +++ b/src/Bootstrappers/BatchTenancyBootstrapper.php @@ -0,0 +1,51 @@ +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; + } + } +} \ No newline at end of file