mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 20:34:03 +00:00
* exclude master from CI * Add batch tenancy queue bootstrapper * add test case * skip tests for old versions * variable docblocks * use Laravel's connection getter and setter * convert test to pest * bottom space * singleton regis in TestCase * Update src/Bootstrappers/BatchTenancyBootstrapper.php Co-authored-by: Samuel Štancl <samuel@archte.ch> * convert batch class resolution to property level * enabled BatchTenancyBootstrapper by default * typehint DatabaseBatchRepository * refactore name * DI DB manager * typehint * Update config.php * use initialize() twice without end()ing tenancy to assert that previousConnection logic works correctly Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com> Co-authored-by: Abrar Ahmad <abrar.dev99@gmail.com> Co-authored-by: Samuel Štancl <samuel@archte.ch>
41 lines
1.2 KiB
PHP
41 lines
1.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Stancl\Tenancy\Bootstrappers;
|
|
|
|
use Illuminate\Bus\DatabaseBatchRepository;
|
|
use Illuminate\Database\Connection;
|
|
use Illuminate\Database\DatabaseManager;
|
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
|
use Stancl\Tenancy\Contracts\Tenant;
|
|
|
|
class BatchTenancyBootstrapper implements TenancyBootstrapper
|
|
{
|
|
/**
|
|
* The previous database connection instance.
|
|
*/
|
|
protected ?Connection $previousConnection = null;
|
|
|
|
public function __construct(
|
|
protected DatabaseBatchRepository $batchRepository,
|
|
protected DatabaseManager $databaseManager
|
|
) {
|
|
}
|
|
|
|
public function bootstrap(Tenant $tenant)
|
|
{
|
|
// Update batch repository connection to use the tenant connection
|
|
$this->previousConnection = $this->batchRepository->getConnection();
|
|
$this->batchRepository->setConnection($this->databaseManager->connection('tenant'));
|
|
}
|
|
|
|
public function revert()
|
|
{
|
|
if ($this->previousConnection) {
|
|
// Replace batch repository connection with the previously replaced one
|
|
$this->batchRepository->setConnection($this->previousConnection);
|
|
$this->previousConnection = null;
|
|
}
|
|
}
|
|
}
|