1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 22:14:03 +00:00

TenantModel putMany

This commit is contained in:
Samuel Štancl 2019-09-15 11:01:24 +02:00
parent 8b2c64c8b1
commit 0fd1d82c68
10 changed files with 88 additions and 46 deletions

View file

@ -7,8 +7,7 @@ namespace Stancl\Tenancy\Contracts;
use Stancl\Tenancy\TenantManager;
/** Additional features, like Telescope tags and tenant redirects. */
// todo interface name. Feature, FeatureProvider, ProvidesFeature(s)
interface FeatureProvider
interface Feature
{
public function bootstrap(TenantManager $tenantManager): void;
}

View file

@ -8,7 +8,7 @@ use Stancl\Tenancy\Tenant;
interface TenancyBootstrapper
{
public function start(Tenant $tenant);
public function start(Tenant $tenant); // todo TenantManager instead of Tenant
public function end();
}

View file

@ -6,10 +6,10 @@ namespace Stancl\Tenancy\Features;
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Telescope;
use Stancl\Tenancy\Contracts\FeatureProvider;
use Stancl\Tenancy\Contracts\Feature;
use Stancl\Tenancy\TenantManager;
class TelescopeTags implements FeatureProvider
class TelescopeTags implements Feature
{
/** @var callable User-specific callback that returns tags. */
protected $callback;

View file

@ -2,10 +2,10 @@
declare(strict_types=1);
use Stancl\Tenancy\Contracts\FeatureProvider;
use Stancl\Tenancy\Contracts\Feature;
use Stancl\Tenancy\TenantManager;
class TenantRedirect implements FeatureProvider
class TenantRedirect implements Feature
{
public function bootstrap(TenantManager $tenantManager): void
{

View file

@ -30,7 +30,7 @@ class DatabaseStorageDriver implements StorageDriver
->withDomains(Domains::where('tenant_id', $id)->all()->only('domain')->toArray());
}
public function ensureTEnantCanBeCreated(Tenant $tenant)
public function ensureTenantCanBeCreated(Tenant $tenant)
{
// todo
}
@ -61,7 +61,7 @@ class DatabaseStorageDriver implements StorageDriver
public function deleteTenant(Tenant $tenant): void
{
Tenants::find($tenant->id)->delete();
// todo domains
Domains::where('tenant_id', $tenant->id)->delete();
}
public function all(array $ids = []): array
@ -87,8 +87,6 @@ class DatabaseStorageDriver implements StorageDriver
public function putMany(array $kvPairs, Tenant $tenant = null): void
{
foreach ($kvPairs as $key => $value) { // todo performance
Tenants::find($tenant->id)->put($key, $value);
}
Tenants::find($tenant->id)->putMany($kvPairs);
}
}

View file

@ -52,14 +52,14 @@ class TenantModel extends Model
public static function decodeData($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) {
$tenant[$key] = $value;
}
// 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]);
}
@ -68,7 +68,7 @@ class TenantModel extends Model
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;
}
@ -89,15 +89,35 @@ class TenantModel extends Model
public function put(string $key, $value)
{
if (\in_array($key, $this->customColumns())) {
if (in_array($key, $this->customColumns())) {
$this->update([$key => $value]);
} else {
$obj = \json_decode($this->{$this->dataColumn()});
$obj = json_decode($this->{$this->dataColumn()});
$obj->$key = $value;
$this->update([$this->dataColumn() => \json_encode($obj)]);
$this->update([$this->dataColumn() => json_encode($obj)]);
}
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),
]))
}
}

View file

@ -10,7 +10,7 @@ use Stancl\Tenancy\Tenant;
class QueueTenancyBootstrapper implements TenancyBootstrapper
{
/** @var bool Has tenancy been started. */
protected $started = false; // todo var name?
protected $started = false;
/** @var Application */
protected $app;

View file

@ -7,13 +7,6 @@ namespace Stancl\Tenancy;
use Illuminate\Cache\CacheManager;
use Illuminate\Support\Facades\Route;
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
{
@ -25,12 +18,12 @@ class TenancyServiceProvider extends ServiceProvider
public function boot()
{
$this->commands([
Run::class,
Seed::class,
Install::class,
Migrate::class,
Rollback::class,
TenantList::class,
Commands\Run::class,
Commands\Seed::class,
Commands\Install::class,
Commands\Migrate::class,
Commands\Rollback::class,
Commands\TenantList::class,
]);
$this->publishes([
@ -59,29 +52,27 @@ class TenancyServiceProvider extends ServiceProvider
{
$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(TenantManager::class, function ($app) {
return new TenantManager(
$app, $app[StorageDriver::class], $app[DatabaseManager::class], $app[$app['config']['tenancy.unique_id_generator']] // todo
);
});
$this->app->singleton(TenantManager::class);
$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) {
$this->app->singleton($bootstrapper);
}
// todo are these necessary?
$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) {
return new Rollback($app['migrator'], $app[DatabaseManager::class]);
return new Commands\Rollback($app['migrator'], $app[DatabaseManager::class]);
});
$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) {

View file

@ -73,7 +73,41 @@ class Tenant implements ArrayAccess
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
{
$domains = (array) $domains;
@ -135,7 +169,7 @@ class Tenant implements ArrayAccess
public function softDelete(): self
{
$this->put('_tenancy_original_domains', $this->domains);
$this->domains = [];
$this->clearDomains();
$this->save();
return $this;

View file

@ -185,10 +185,10 @@ class TenantManager
* Get the current tenant.
*
* @param string $key
* @return Tenant
* @return Tenant|mixed
* @throws NoTenantIdentifiedException
*/
public function getTenant(string $key = null): Tenant
public function getTenant(string $key = null)
{
if (! $this->tenant) {
throw new NoTenantIdentifiedException;