mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 22:34:03 +00:00
Fix some tests
This commit is contained in:
parent
a632b545b7
commit
1532ccf019
15 changed files with 121 additions and 118 deletions
|
|
@ -49,22 +49,23 @@ class Migrate extends MigrateCommand
|
|||
return;
|
||||
}
|
||||
|
||||
tenant()->all($this->option('tenants'))->each(function ($tenant) {
|
||||
$this->line("Tenant: {$tenant['id']} ({$tenant['domain']})");
|
||||
$originalTenant = tenancy()->getTenant();
|
||||
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
|
||||
$this->line("Tenant: {$tenant['id']}");
|
||||
|
||||
// See Illuminate\Database\Migrations\DatabaseMigrationRepository::getConnection.
|
||||
// Database connections are cached by Illuminate\Database\ConnectionResolver.
|
||||
$this->input->setOption('database', 'tenant');
|
||||
$this->database->connectToTenant($tenant); // todo test that this works with multiple tenants with MySQL
|
||||
tenancy()->initialize($tenant); // todo test that this works with multiple tenants with MySQL
|
||||
|
||||
// Migrate
|
||||
parent::handle();
|
||||
});
|
||||
|
||||
if (tenancy()->initialized) {
|
||||
tenancy()->switchDatabaseConnection();
|
||||
if ($originalTenant) {
|
||||
tenancy()->initialize($originalTenant);
|
||||
} else {
|
||||
$this->database->disconnect();
|
||||
tenancy()->endTenancy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,18 +51,19 @@ class Rollback extends RollbackCommand
|
|||
|
||||
$this->input->setOption('database', 'tenant');
|
||||
|
||||
tenant()->all($this->option('tenants'))->each(function ($tenant) {
|
||||
$this->line("Tenant: {$tenant['id']} ({$tenant['domain']})");
|
||||
$this->database->connectToTenant($tenant);
|
||||
$originalTenant = tenancy()->getTenant();
|
||||
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
|
||||
$this->line("Tenant: {$tenant['id']}");
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
// Migrate
|
||||
parent::handle();
|
||||
});
|
||||
|
||||
if (tenancy()->initialized) {
|
||||
tenancy()->switchDatabaseConnection();
|
||||
if ($originalTenant) {
|
||||
tenancy()->initialize($originalTenant);
|
||||
} else {
|
||||
$this->database->disconnect();
|
||||
tenancy()->endTenancy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,13 +32,10 @@ class Run extends Command
|
|||
*/
|
||||
public function handle()
|
||||
{
|
||||
if ($tenancy_was_initialized = tenancy()->initialized) {
|
||||
$previous_tenants_domain = tenant('domain');
|
||||
}
|
||||
|
||||
tenant()->all($this->option('tenants'))->each(function ($tenant) {
|
||||
$this->line("Tenant: {$tenant['id']} ({$tenant['domain']})");
|
||||
tenancy()->init($tenant['domain']);
|
||||
$originalTenant = tenancy()->getTenant();
|
||||
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
|
||||
$this->line("Tenant: {$tenant['id']}");
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
$callback = function ($prefix = '') {
|
||||
return function ($arguments, $argument) use ($prefix) {
|
||||
|
|
@ -58,11 +55,13 @@ class Run extends Command
|
|||
// Run command
|
||||
$this->call($this->argument('commandname'), \array_merge($arguments, $options));
|
||||
|
||||
tenancy()->end();
|
||||
tenancy()->endTenancy();
|
||||
});
|
||||
|
||||
if ($tenancy_was_initialized) {
|
||||
tenancy()->init($previous_tenants_domain);
|
||||
if ($originalTenant) {
|
||||
tenancy()->initialize($originalTenant);
|
||||
} else {
|
||||
tenancy()->endTenancy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,18 +49,19 @@ class Seed extends SeedCommand
|
|||
|
||||
$this->input->setOption('database', 'tenant');
|
||||
|
||||
tenant()->all($this->option('tenants'))->each(function ($tenant) {
|
||||
$this->line("Tenant: {$tenant['id']} ({$tenant['domain']})");
|
||||
$this->database->connectToTenant($tenant);
|
||||
$originalTenant = tenancy()->getTenant();
|
||||
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
|
||||
$this->line("Tenant: {$tenant['id']}");
|
||||
tenancy()->initialize($tenant);
|
||||
|
||||
// Seed
|
||||
parent::handle();
|
||||
});
|
||||
|
||||
if (tenancy()->initialized) {
|
||||
tenancy()->switchDatabaseConnection();
|
||||
if ($originalTenant) {
|
||||
tenancy()->initialize($originalTenant);
|
||||
} else {
|
||||
$this->database->disconnect();
|
||||
tenancy()->endTenancy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ class TenantList extends Command
|
|||
{
|
||||
$this->info('Listing all tenants.');
|
||||
tenancy()->all()->each(function ($tenant) {
|
||||
$this->line("[Tenant] id: {$tenant['id']} @ {$tenant['domain']}");
|
||||
$this->line("[Tenant] id: {$tenant['id']} @ ", implode('; ', $tenant->domains));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Exceptions;
|
||||
|
||||
class NoTenantIdentifiedException extends \Exception
|
||||
{
|
||||
protected $message = 'No tenant has been identified yet.';
|
||||
}
|
||||
|
|
@ -102,7 +102,9 @@ class DatabaseStorageDriver implements StorageDriver
|
|||
*/
|
||||
public function all(array $ids = []): array
|
||||
{
|
||||
return Tenants::getAllTenants($ids)->toArray();
|
||||
return Tenants::getAllTenants($ids)->map(function ($array) {
|
||||
return Tenant::fromStorage($array)->withDomains([]); // todo domains
|
||||
})->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -6,8 +6,8 @@ namespace Stancl\Tenancy;
|
|||
|
||||
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\Collection;
|
||||
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
|
||||
use Stancl\Tenancy\Exceptions\NoTenantIdentifiedException;
|
||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
||||
|
||||
/**
|
||||
|
|
@ -34,9 +34,12 @@ class TenantManager
|
|||
/** @var DatabaseManager */
|
||||
protected $database;
|
||||
|
||||
/** @var callable[][] */
|
||||
/** @var callable[][] Event listeners */
|
||||
protected $listeners = [];
|
||||
|
||||
/** @var bool Has tenancy been initialized. */
|
||||
public $initialized;
|
||||
|
||||
public function __construct(Application $app, ConsoleKernel $artisan, Contracts\StorageDriver $storage, DatabaseManager $database)
|
||||
{
|
||||
$this->app = $app;
|
||||
|
|
@ -136,25 +139,32 @@ class TenantManager
|
|||
* Get all tenants.
|
||||
*
|
||||
* @param Tenant[]|string[] $only
|
||||
* @return Tenant[]
|
||||
* @return Collection<Tenant>
|
||||
*/
|
||||
public function all($only = []): array
|
||||
public function all($only = []): Collection
|
||||
{
|
||||
$only = array_map(function ($item) {
|
||||
return $item instanceof Tenant ? $item->id : $item;
|
||||
}, $only);
|
||||
}, (array) $only);
|
||||
|
||||
return $this->storage->all($only);
|
||||
return collect($this->storage->all($only));
|
||||
}
|
||||
|
||||
public function initializeTenancy(Tenant $tenant): self
|
||||
{
|
||||
$this->bootstrapTenancy($tenant);
|
||||
$this->setTenant($tenant);
|
||||
$this->initialized = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** @alias initializeTenancy */
|
||||
public function initialize(Tenant $tenant): self
|
||||
{
|
||||
return $this->initializeTenancy($tenant);
|
||||
}
|
||||
|
||||
public function bootstrapTenancy(Tenant $tenant): self
|
||||
{
|
||||
$prevented = $this->event('bootstrapping');
|
||||
|
|
@ -176,22 +186,30 @@ class TenantManager
|
|||
$this->app[$bootstrapper]->end();
|
||||
}
|
||||
|
||||
$this->initialized = false;
|
||||
$this->tenant = null;
|
||||
|
||||
$this->event('ended');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** @alias endTenancy */
|
||||
public function end(): self
|
||||
{
|
||||
return $this->endTenancy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current tenant.
|
||||
*
|
||||
* @param string $key
|
||||
* @return Tenant|mixed
|
||||
* @throws NoTenantIdentifiedException
|
||||
* @return Tenant|null|mixed
|
||||
*/
|
||||
public function getTenant(string $key = null)
|
||||
{
|
||||
if (! $this->tenant) {
|
||||
throw new NoTenantIdentifiedException;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! is_null($key)) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue