mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 00:34:03 +00:00
TenantModel putMany
This commit is contained in:
parent
8b2c64c8b1
commit
0fd1d82c68
10 changed files with 88 additions and 46 deletions
|
|
@ -7,8 +7,7 @@ namespace Stancl\Tenancy\Contracts;
|
||||||
use Stancl\Tenancy\TenantManager;
|
use Stancl\Tenancy\TenantManager;
|
||||||
|
|
||||||
/** Additional features, like Telescope tags and tenant redirects. */
|
/** Additional features, like Telescope tags and tenant redirects. */
|
||||||
// todo interface name. Feature, FeatureProvider, ProvidesFeature(s)
|
interface Feature
|
||||||
interface FeatureProvider
|
|
||||||
{
|
{
|
||||||
public function bootstrap(TenantManager $tenantManager): void;
|
public function bootstrap(TenantManager $tenantManager): void;
|
||||||
}
|
}
|
||||||
|
|
@ -8,7 +8,7 @@ use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
interface TenancyBootstrapper
|
interface TenancyBootstrapper
|
||||||
{
|
{
|
||||||
public function start(Tenant $tenant);
|
public function start(Tenant $tenant); // todo TenantManager instead of Tenant
|
||||||
|
|
||||||
public function end();
|
public function end();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,10 @@ namespace Stancl\Tenancy\Features;
|
||||||
|
|
||||||
use Laravel\Telescope\IncomingEntry;
|
use Laravel\Telescope\IncomingEntry;
|
||||||
use Laravel\Telescope\Telescope;
|
use Laravel\Telescope\Telescope;
|
||||||
use Stancl\Tenancy\Contracts\FeatureProvider;
|
use Stancl\Tenancy\Contracts\Feature;
|
||||||
use Stancl\Tenancy\TenantManager;
|
use Stancl\Tenancy\TenantManager;
|
||||||
|
|
||||||
class TelescopeTags implements FeatureProvider
|
class TelescopeTags implements Feature
|
||||||
{
|
{
|
||||||
/** @var callable User-specific callback that returns tags. */
|
/** @var callable User-specific callback that returns tags. */
|
||||||
protected $callback;
|
protected $callback;
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,10 @@
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
use Stancl\Tenancy\Contracts\FeatureProvider;
|
use Stancl\Tenancy\Contracts\Feature;
|
||||||
use Stancl\Tenancy\TenantManager;
|
use Stancl\Tenancy\TenantManager;
|
||||||
|
|
||||||
class TenantRedirect implements FeatureProvider
|
class TenantRedirect implements Feature
|
||||||
{
|
{
|
||||||
public function bootstrap(TenantManager $tenantManager): void
|
public function bootstrap(TenantManager $tenantManager): void
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ class DatabaseStorageDriver implements StorageDriver
|
||||||
->withDomains(Domains::where('tenant_id', $id)->all()->only('domain')->toArray());
|
->withDomains(Domains::where('tenant_id', $id)->all()->only('domain')->toArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function ensureTEnantCanBeCreated(Tenant $tenant)
|
public function ensureTenantCanBeCreated(Tenant $tenant)
|
||||||
{
|
{
|
||||||
// todo
|
// todo
|
||||||
}
|
}
|
||||||
|
|
@ -61,7 +61,7 @@ class DatabaseStorageDriver implements StorageDriver
|
||||||
public function deleteTenant(Tenant $tenant): void
|
public function deleteTenant(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
Tenants::find($tenant->id)->delete();
|
Tenants::find($tenant->id)->delete();
|
||||||
// todo domains
|
Domains::where('tenant_id', $tenant->id)->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function all(array $ids = []): array
|
public function all(array $ids = []): array
|
||||||
|
|
@ -87,8 +87,6 @@ class DatabaseStorageDriver implements StorageDriver
|
||||||
|
|
||||||
public function putMany(array $kvPairs, Tenant $tenant = null): void
|
public function putMany(array $kvPairs, Tenant $tenant = null): void
|
||||||
{
|
{
|
||||||
foreach ($kvPairs as $key => $value) { // todo performance
|
Tenants::find($tenant->id)->putMany($kvPairs);
|
||||||
Tenants::find($tenant->id)->put($key, $value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -52,14 +52,14 @@ class TenantModel extends Model
|
||||||
public static function decodeData($tenant)
|
public static function decodeData($tenant)
|
||||||
{
|
{
|
||||||
$tenant = $tenant instanceof self ? (array) $tenant->attributes : $tenant;
|
$tenant = $tenant instanceof self ? (array) $tenant->attributes : $tenant;
|
||||||
$decoded = \json_decode($tenant[$dataColumn = static::dataColumn()], true);
|
$decoded = json_decode($tenant[$dataColumn = static::dataColumn()], true);
|
||||||
|
|
||||||
foreach ($decoded as $key => $value) {
|
foreach ($decoded as $key => $value) {
|
||||||
$tenant[$key] = $value;
|
$tenant[$key] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If $tenant[$dataColumn] has been overriden by a value, don't delete the key.
|
// If $tenant[$dataColumn] has been overriden by a value, don't delete the key.
|
||||||
if (! \array_key_exists($dataColumn, $decoded)) {
|
if (! array_key_exists($dataColumn, $decoded)) {
|
||||||
unset($tenant[$dataColumn]);
|
unset($tenant[$dataColumn]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -68,7 +68,7 @@ class TenantModel extends Model
|
||||||
|
|
||||||
public function getFromData(string $key)
|
public function getFromData(string $key)
|
||||||
{
|
{
|
||||||
$this->dataArray = $this->dataArray ?? \json_decode($this->{$this->dataColumn()}, true);
|
$this->dataArray = $this->dataArray ?? json_decode($this->{$this->dataColumn()}, true);
|
||||||
|
|
||||||
return $this->dataArray[$key] ?? null;
|
return $this->dataArray[$key] ?? null;
|
||||||
}
|
}
|
||||||
|
|
@ -89,15 +89,35 @@ class TenantModel extends Model
|
||||||
|
|
||||||
public function put(string $key, $value)
|
public function put(string $key, $value)
|
||||||
{
|
{
|
||||||
if (\in_array($key, $this->customColumns())) {
|
if (in_array($key, $this->customColumns())) {
|
||||||
$this->update([$key => $value]);
|
$this->update([$key => $value]);
|
||||||
} else {
|
} else {
|
||||||
$obj = \json_decode($this->{$this->dataColumn()});
|
$obj = json_decode($this->{$this->dataColumn()});
|
||||||
$obj->$key = $value;
|
$obj->$key = $value;
|
||||||
|
|
||||||
$this->update([$this->dataColumn() => \json_encode($obj)]);
|
$this->update([$this->dataColumn() => json_encode($obj)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function putMany(array $kvPairs)
|
||||||
|
{
|
||||||
|
$customColumns = [];
|
||||||
|
$jsonObj = json_decode($this->{$this->customColumns()});
|
||||||
|
|
||||||
|
foreach($kvPairs as $key => $value)
|
||||||
|
{
|
||||||
|
if (in_array($key, $this->customColumns())) {
|
||||||
|
$customColumns[$key] = $value;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$jsonObj->$key = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->update(array_merge($customColumns, [
|
||||||
|
$this->dataColumn() => json_encode($jsonObj),
|
||||||
|
]))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ use Stancl\Tenancy\Tenant;
|
||||||
class QueueTenancyBootstrapper implements TenancyBootstrapper
|
class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||||
{
|
{
|
||||||
/** @var bool Has tenancy been started. */
|
/** @var bool Has tenancy been started. */
|
||||||
protected $started = false; // todo var name?
|
protected $started = false;
|
||||||
|
|
||||||
/** @var Application */
|
/** @var Application */
|
||||||
protected $app;
|
protected $app;
|
||||||
|
|
|
||||||
|
|
@ -7,13 +7,6 @@ namespace Stancl\Tenancy;
|
||||||
use Illuminate\Cache\CacheManager;
|
use Illuminate\Cache\CacheManager;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Stancl\Tenancy\Commands\Install;
|
|
||||||
use Stancl\Tenancy\Commands\Migrate;
|
|
||||||
use Stancl\Tenancy\Commands\Rollback;
|
|
||||||
use Stancl\Tenancy\Commands\Run;
|
|
||||||
use Stancl\Tenancy\Commands\Seed;
|
|
||||||
use Stancl\Tenancy\Commands\TenantList;
|
|
||||||
use Stancl\Tenancy\Contracts\StorageDriver;
|
|
||||||
|
|
||||||
class TenancyServiceProvider extends ServiceProvider
|
class TenancyServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
|
|
@ -25,12 +18,12 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
$this->commands([
|
$this->commands([
|
||||||
Run::class,
|
Commands\Run::class,
|
||||||
Seed::class,
|
Commands\Seed::class,
|
||||||
Install::class,
|
Commands\Install::class,
|
||||||
Migrate::class,
|
Commands\Migrate::class,
|
||||||
Rollback::class,
|
Commands\Rollback::class,
|
||||||
TenantList::class,
|
Commands\TenantList::class,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->publishes([
|
$this->publishes([
|
||||||
|
|
@ -59,29 +52,27 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
$this->mergeConfigFrom(__DIR__ . '/../assets/config.php', 'tenancy');
|
$this->mergeConfigFrom(__DIR__ . '/../assets/config.php', 'tenancy');
|
||||||
|
|
||||||
$this->app->bind(StorageDriver::class, $this->app['config']['tenancy.storage_driver']);
|
$this->app->bind(Contracts\StorageDriver::class, $this->app['config']['tenancy.storage_driver']);
|
||||||
|
$this->app->bind(Contracts\UniqueIdentifierGenerator::class, $this->app['config']['tenancy.unique_id_generator']);
|
||||||
$this->app->singleton(DatabaseManager::class);
|
$this->app->singleton(DatabaseManager::class);
|
||||||
$this->app->singleton(TenantManager::class, function ($app) {
|
$this->app->singleton(TenantManager::class);
|
||||||
return new TenantManager(
|
|
||||||
$app, $app[StorageDriver::class], $app[DatabaseManager::class], $app[$app['config']['tenancy.unique_id_generator']] // todo
|
|
||||||
);
|
|
||||||
});
|
|
||||||
$this->app->bind(Tenant::class, function ($app) {
|
$this->app->bind(Tenant::class, function ($app) {
|
||||||
return $app[TenantManager::class]->currentTenant();
|
return $app[TenantManager::class]->getTenant();
|
||||||
});
|
});
|
||||||
|
|
||||||
foreach ($this->app['config']['tenancy.bootstrappers'] as $bootstrapper) {
|
foreach ($this->app['config']['tenancy.bootstrappers'] as $bootstrapper) {
|
||||||
$this->app->singleton($bootstrapper);
|
$this->app->singleton($bootstrapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// todo are these necessary?
|
||||||
$this->app->singleton(Migrate::class, function ($app) {
|
$this->app->singleton(Migrate::class, function ($app) {
|
||||||
return new Migrate($app['migrator'], $app[DatabaseManager::class]);
|
return new Commands\Migrate($app['migrator'], $app[DatabaseManager::class]);
|
||||||
});
|
});
|
||||||
$this->app->singleton(Rollback::class, function ($app) {
|
$this->app->singleton(Rollback::class, function ($app) {
|
||||||
return new Rollback($app['migrator'], $app[DatabaseManager::class]);
|
return new Commands\Rollback($app['migrator'], $app[DatabaseManager::class]);
|
||||||
});
|
});
|
||||||
$this->app->singleton(Seed::class, function ($app) {
|
$this->app->singleton(Seed::class, function ($app) {
|
||||||
return new Seed($app['db'], $app[DatabaseManager::class]);
|
return new Commands\Seed($app['db'], $app[DatabaseManager::class]);
|
||||||
});
|
});
|
||||||
|
|
||||||
$this->app->bind('globalCache', function ($app) {
|
$this->app->bind('globalCache', function ($app) {
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,41 @@ class Tenant implements ArrayAccess
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// todo addDomain, removeDomain
|
/**
|
||||||
|
* Assign domains to the tenant.
|
||||||
|
*
|
||||||
|
* @param string|string[] $domains
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function addDomains($domains): self
|
||||||
|
{
|
||||||
|
$domains = (array) $domains;
|
||||||
|
$this->domains = array_merge($this->domains, $domains);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unassign domains from the tenant.
|
||||||
|
*
|
||||||
|
* @param string|string[] $domains
|
||||||
|
* @return self
|
||||||
|
*/
|
||||||
|
public function removeDomains($domains): self
|
||||||
|
{
|
||||||
|
$domains = (array) $domains;
|
||||||
|
$this->domains = array_diff($this->domains, $domains);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clearDomains(): self
|
||||||
|
{
|
||||||
|
$this->domains = [];
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
public function withDomains($domains): self
|
public function withDomains($domains): self
|
||||||
{
|
{
|
||||||
$domains = (array) $domains;
|
$domains = (array) $domains;
|
||||||
|
|
@ -135,7 +169,7 @@ class Tenant implements ArrayAccess
|
||||||
public function softDelete(): self
|
public function softDelete(): self
|
||||||
{
|
{
|
||||||
$this->put('_tenancy_original_domains', $this->domains);
|
$this->put('_tenancy_original_domains', $this->domains);
|
||||||
$this->domains = [];
|
$this->clearDomains();
|
||||||
$this->save();
|
$this->save();
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|
|
||||||
|
|
@ -185,10 +185,10 @@ class TenantManager
|
||||||
* Get the current tenant.
|
* Get the current tenant.
|
||||||
*
|
*
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @return Tenant
|
* @return Tenant|mixed
|
||||||
* @throws NoTenantIdentifiedException
|
* @throws NoTenantIdentifiedException
|
||||||
*/
|
*/
|
||||||
public function getTenant(string $key = null): Tenant
|
public function getTenant(string $key = null)
|
||||||
{
|
{
|
||||||
if (! $this->tenant) {
|
if (! $this->tenant) {
|
||||||
throw new NoTenantIdentifiedException;
|
throw new NoTenantIdentifiedException;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue