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