mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:44:04 +00:00
Use a nicer way to bind the current tenant
This commit is contained in:
parent
27425a4360
commit
6b716e5345
6 changed files with 62 additions and 38 deletions
|
|
@ -4,26 +4,12 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Contracts;
|
namespace Stancl\Tenancy\Contracts;
|
||||||
|
|
||||||
|
use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
interface StorageDriver
|
interface StorageDriver
|
||||||
{
|
{
|
||||||
public function identifyTenant(string $domain): array;
|
public function createTenant(Tenant $tenant): bool; // todo return type
|
||||||
|
public function updateTenant(Tenant $tenant): bool; // todo return type
|
||||||
/** @return array[] */
|
public function findById(string $id): Tenant;
|
||||||
public function getAllTenants(array $uuids = []): array;
|
public function findByDomain(string $domain): Tenant;
|
||||||
|
|
||||||
public function getTenantById(string $uuid, array $fields = []): array;
|
|
||||||
|
|
||||||
public function getTenantIdByDomain(string $domain): ?string;
|
|
||||||
|
|
||||||
public function createTenant(string $domain, string $uuid): array;
|
|
||||||
|
|
||||||
public function deleteTenant(string $uuid): bool;
|
|
||||||
|
|
||||||
public function get(string $uuid, string $key);
|
|
||||||
|
|
||||||
public function getMany(string $uuid, array $keys): array;
|
|
||||||
|
|
||||||
public function put(string $uuid, string $key, $value);
|
|
||||||
|
|
||||||
public function putMany(string $uuid, array $values): array;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Contracts;
|
|
||||||
|
|
||||||
/** Empty interface implemented by Stancl\Tenancy\Tenant have a dependency-injectable contract for the current tenant. */
|
|
||||||
interface Tenant
|
|
||||||
{
|
|
||||||
}
|
|
||||||
8
src/Exceptions/NoTenantIdentifiedException.php
Normal file
8
src/Exceptions/NoTenantIdentifiedException.php
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Exceptions;
|
||||||
|
|
||||||
|
class NoTenantIdentifiedExceptions extends Exception
|
||||||
|
{
|
||||||
|
protected $message = 'No tenant has been identified yet.';
|
||||||
|
}
|
||||||
|
|
@ -103,6 +103,9 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
$app, $app[StorageDriver::class], $app[DatabaseManager::class], $app[$app['config']['tenancy.unique_id_generator']] // todo
|
$app, $app[StorageDriver::class], $app[DatabaseManager::class], $app[$app['config']['tenancy.unique_id_generator']] // todo
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
$this->app->bind(Tenant::class, function ($app) {
|
||||||
|
return $app[TenantManager::class]->currentTenant();
|
||||||
|
});
|
||||||
|
|
||||||
// todo foreach bootstrappers, singleton
|
// todo foreach bootstrappers, singleton
|
||||||
foreach ($this->app['config']['tenancy.bootstrappers'] as $bootstrapper) {
|
foreach ($this->app['config']['tenancy.bootstrappers'] as $bootstrapper) {
|
||||||
|
|
|
||||||
|
|
@ -5,14 +5,14 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy;
|
namespace Stancl\Tenancy;
|
||||||
|
|
||||||
use ArrayAccess;
|
use ArrayAccess;
|
||||||
use Stancl\Tenancy\Contracts\Tenant as CurrentTenant;
|
|
||||||
|
|
||||||
// todo tenant storage
|
// todo tenant storage
|
||||||
|
// todo create tenant - create db?
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal Class is subject to breaking changes in minor and patch versions.
|
* @internal Class is subject to breaking changes in minor and patch versions.
|
||||||
*/
|
*/
|
||||||
class Tenant implements ArrayAccess, CurrentTenant
|
class Tenant implements ArrayAccess
|
||||||
{
|
{
|
||||||
use Traits\HasArrayAccess;
|
use Traits\HasArrayAccess;
|
||||||
|
|
||||||
|
|
@ -85,7 +85,7 @@ class Tenant implements ArrayAccess, CurrentTenant
|
||||||
public function save(): self
|
public function save(): self
|
||||||
{
|
{
|
||||||
if ($this->persisted) {
|
if ($this->persisted) {
|
||||||
$this->manager->addTenant($this);
|
$this->manager->createTenant($this);
|
||||||
} else {
|
} else {
|
||||||
$this->manager->updateTenant($this);
|
$this->manager->updateTenant($this);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ class TenantManagerv2
|
||||||
*
|
*
|
||||||
* @var Tenant
|
* @var Tenant
|
||||||
*/
|
*/
|
||||||
public $tenant;
|
protected $tenant;
|
||||||
|
|
||||||
/** @var Application */
|
/** @var Application */
|
||||||
private $app;
|
private $app;
|
||||||
|
|
@ -36,9 +36,9 @@ class TenantManagerv2
|
||||||
$this->storage = $storage;
|
$this->storage = $storage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addTenant(Tenant $tenant): self
|
public function createTenant(Tenant $tenant): self
|
||||||
{
|
{
|
||||||
$this->storage->addTenant($tenant);
|
$this->storage->createTenant($tenant);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
@ -50,6 +50,34 @@ class TenantManagerv2
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo @throws
|
||||||
|
public function init(string $domain): self
|
||||||
|
{
|
||||||
|
$this->initializeTenancy($this->findByDomain($domain));
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo @throws
|
||||||
|
public function initById(string $id): self
|
||||||
|
{
|
||||||
|
$this->initializeTenancy($this->find($id));
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo @throws
|
||||||
|
public function find(string $id): Tenant
|
||||||
|
{
|
||||||
|
return $this->storage->findById($id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// todo @throws
|
||||||
|
public function findByDomain(string $domain): Tenant
|
||||||
|
{
|
||||||
|
return $this->storage->findByDomain($domain);
|
||||||
|
}
|
||||||
|
|
||||||
public function initializeTenancy(Tenant $tenant): self
|
public function initializeTenancy(Tenant $tenant): self
|
||||||
{
|
{
|
||||||
$this->bootstrapTenancy($tenant);
|
$this->bootstrapTenancy($tenant);
|
||||||
|
|
@ -84,9 +112,18 @@ class TenantManagerv2
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setTenant(Tenant $tenant): self
|
public function getTenant(): Tenant
|
||||||
{
|
{
|
||||||
$this->app->instance(Contracts\Tenant::class, $tenant);
|
if (! $this->tenant) {
|
||||||
|
throw new NoTenantIdentifiedException;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->tenant;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setTenant(Tenant $tenant): self
|
||||||
|
{
|
||||||
|
$this->tenant = $tenant;
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue