mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 14:14:04 +00:00
Refactor more old code and get tests to pass
This commit is contained in:
parent
c5377a16f7
commit
c32f229dd5
72 changed files with 425 additions and 531 deletions
|
|
@ -9,6 +9,7 @@ use Illuminate\Database\Console\Migrations\RollbackCommand;
|
|||
use Illuminate\Database\Migrations\Migrator;
|
||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||
use Stancl\Tenancy\DatabaseManager;
|
||||
use Stancl\Tenancy\Events\DatabaseRolledBack;
|
||||
use Stancl\Tenancy\Traits\DealsWithMigrations;
|
||||
use Stancl\Tenancy\Traits\HasATenantsOption;
|
||||
|
||||
|
|
@ -62,7 +63,7 @@ class Rollback extends RollbackCommand
|
|||
// Rollback
|
||||
parent::handle();
|
||||
|
||||
// todo DatabaseRolledBack event
|
||||
event(new DatabaseRolledBack($tenant));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\Contracts;
|
||||
|
||||
use Stancl\Tenancy\Facades\Tenancy;
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
|
||||
/** Additional features, like Telescope tags and tenant redirects. */
|
||||
interface Feature
|
||||
|
|
|
|||
|
|
@ -1,21 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Contracts\Future;
|
||||
|
||||
use Stancl\Tenancy\Tenant;
|
||||
|
||||
/**
|
||||
* This interface will be part of the StorageDriver interface in 3.x.
|
||||
*/
|
||||
interface CanDeleteKeys
|
||||
{
|
||||
/**
|
||||
* Delete keys from the storage.
|
||||
*
|
||||
* @param string[] $keys
|
||||
* @return void
|
||||
*/
|
||||
public function deleteMany(array $keys, Tenant $tenant = null): void;
|
||||
}
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Contracts\Future;
|
||||
|
||||
use Stancl\Tenancy\Exceptions\TenantDoesNotExistException;
|
||||
use Stancl\Tenancy\Tenant;
|
||||
|
||||
/**
|
||||
* This interface *might* be part of the StorageDriver interface in 3.x.
|
||||
*/
|
||||
interface CanFindByAnyKey
|
||||
{
|
||||
/**
|
||||
* Find a tenant using an arbitrary key.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return Tenant
|
||||
* @throws TenantDoesNotExistException
|
||||
*/
|
||||
public function findBy(string $key, $value): Tenant;
|
||||
}
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Contracts\Future;
|
||||
|
||||
/**
|
||||
* This interface *might* be part of the TenantDatabaseManager interface in 3.x.
|
||||
*/
|
||||
interface CanSetConnection
|
||||
{
|
||||
public function setConnection(string $connection): void;
|
||||
}
|
||||
|
|
@ -9,8 +9,7 @@ namespace Stancl\Tenancy\Contracts;
|
|||
*/
|
||||
interface TenancyBootstrapper
|
||||
{
|
||||
// todo rename methods
|
||||
public function start(Tenant $tenant);
|
||||
public function bootstrap(Tenant $tenant);
|
||||
|
||||
public function end();
|
||||
public function revert();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,4 +32,12 @@ interface TenantDatabaseManager
|
|||
* @return array
|
||||
*/
|
||||
public function makeConnectionConfig(array $baseConfig, string $databaseName): array;
|
||||
|
||||
/**
|
||||
* Set the DB connection that should be used by the tenant database manager.
|
||||
*
|
||||
* @param string $connection
|
||||
* @return void
|
||||
*/
|
||||
public function setConnection(string $connection): void;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
namespace Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
trait CentralConnection
|
||||
{
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
namespace Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
||||
|
||||
|
|
@ -14,4 +14,9 @@ trait GeneratesIds
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function getIncrementing()
|
||||
{
|
||||
return ! app()->bound(UniqueIdentifierGenerator::class);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,7 @@
|
|||
<?php
|
||||
|
||||
// todo move namespace one dir above
|
||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
namespace Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
// todo rename
|
||||
trait HasADataColumn
|
||||
{
|
||||
public static $priorityListeners = [];
|
||||
|
|
@ -53,11 +51,17 @@ trait HasADataColumn
|
|||
$model->dataEncodingStatus = 'decoded';
|
||||
};
|
||||
|
||||
static::registerPriorityListener('retrieved', $decode);
|
||||
static::registerPriorityListener('retrieved', function ($model) use ($decode) {
|
||||
// We always decode after model retrieval.
|
||||
$model->dataEncodingStatus = 'encoded';
|
||||
|
||||
$decode($model);
|
||||
});
|
||||
|
||||
static::registerPriorityListener('saving', $encode);
|
||||
static::registerPriorityListener('creating', $encode);
|
||||
static::registerPriorityListener('updating', $encode);
|
||||
|
||||
static::registerPriorityListener('saved', $decode);
|
||||
static::registerPriorityListener('created', $decode);
|
||||
static::registerPriorityListener('updated', $decode);
|
||||
16
src/Database/Concerns/HasDomains.php
Normal file
16
src/Database/Concerns/HasDomains.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
use Stancl\Tenancy\Contracts\Domain;
|
||||
|
||||
/**
|
||||
* @property-read Domain[] $domains
|
||||
*/
|
||||
trait HasDomains
|
||||
{
|
||||
public function domains()
|
||||
{
|
||||
return $this->hasMany(config('tenancy.domain_model'), 'tenant_id');
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
namespace Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
/**
|
||||
* @property-read string $primary_domain_hostname
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
namespace Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
use Stancl\Tenancy\Contracts\Syncable;
|
||||
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
namespace Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
trait TenantConnection
|
||||
{
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Models\Concerns;
|
||||
|
||||
trait HasDomains
|
||||
{
|
||||
public function domains()
|
||||
{
|
||||
return $this->hasMany(config('tenancy.domain_model'));
|
||||
}
|
||||
}
|
||||
|
|
@ -2,18 +2,30 @@
|
|||
|
||||
namespace Stancl\Tenancy\Database\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Stancl\Tenancy\DatabaseConfig;
|
||||
use Stancl\Tenancy\Events;
|
||||
use Stancl\Tenancy\Contracts;
|
||||
use Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
// todo @property
|
||||
class Tenant extends Model implements Contracts\TenantWithDatabase
|
||||
/**
|
||||
* @property string|int $id
|
||||
* @property Carbon $created_at
|
||||
* @property Carbon $updated_at
|
||||
* @property array $data
|
||||
*/
|
||||
class Tenant extends Model implements Contracts\TenantWithDatabase // todo base model that isn't TenantWithDatabase & domains
|
||||
{
|
||||
use Concerns\CentralConnection, Concerns\HasADataColumn, Concerns\GeneratesIds, Concerns\HasADataColumn {
|
||||
use Concerns\CentralConnection,
|
||||
Concerns\HasADataColumn,
|
||||
Concerns\GeneratesIds,
|
||||
Concerns\HasADataColumn,
|
||||
Concerns\HasDomains {
|
||||
Concerns\HasADataColumn::getCasts as dataColumnCasts;
|
||||
}
|
||||
|
||||
protected $table = 'tenants';
|
||||
public $primaryKey = 'id';
|
||||
public $guarded = [];
|
||||
|
||||
|
|
@ -34,11 +46,6 @@ class Tenant extends Model implements Contracts\TenantWithDatabase
|
|||
]);
|
||||
}
|
||||
|
||||
public function getIncrementing()
|
||||
{
|
||||
return config('tenancy.id_generator') === null;
|
||||
}
|
||||
|
||||
public static function internalPrefix(): string
|
||||
{
|
||||
return config('tenancy.internal_prefix');
|
||||
|
|
|
|||
|
|
@ -6,9 +6,7 @@ namespace Stancl\Tenancy;
|
|||
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Str;
|
||||
use Stancl\Tenancy\Contracts\Future\CanSetConnection;
|
||||
use Stancl\Tenancy\Contracts\ManagesDatabaseUsers;
|
||||
use Stancl\Tenancy\Contracts\ModifiesDatabaseNameForConnection;
|
||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||
use Stancl\Tenancy\Database\Models\Tenant;
|
||||
use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException;
|
||||
|
|
@ -79,6 +77,11 @@ class DatabaseConfig
|
|||
return $this->tenant->getInternal('db_password') ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate DB name, username & password and write them to the tenant model.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function makeCredentials(): void
|
||||
{
|
||||
$this->tenant->setInternal('db_name', $this->getName() ?? (static::$databaseNameGenerator)($this->tenant));
|
||||
|
|
@ -151,9 +154,7 @@ class DatabaseConfig
|
|||
/** @var TenantDatabaseManager $databaseManager */
|
||||
$databaseManager = app($databaseManagers[$driver]);
|
||||
|
||||
if ($databaseManager instanceof CanSetConnection) {
|
||||
$databaseManager->setConnection($this->getTemplateConnectionName());
|
||||
}
|
||||
$databaseManager->setConnection($this->getTemplateConnectionName());
|
||||
|
||||
return $databaseManager;
|
||||
}
|
||||
|
|
|
|||
6
src/Events/DatabaseRolledBack.php
Normal file
6
src/Events/DatabaseRolledBack.php
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Events;
|
||||
|
||||
class DatabaseRolledBack extends Contracts\TenantEvent
|
||||
{}
|
||||
16
src/Events/RevertedToCentralContext.php
Normal file
16
src/Events/RevertedToCentralContext.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Events;
|
||||
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
|
||||
class RevertedToCentralContext
|
||||
{
|
||||
/** @var Tenancy */
|
||||
public $tenancy;
|
||||
|
||||
public function __construct(Tenancy $tenancy)
|
||||
{
|
||||
$this->tenancy = $tenancy;
|
||||
}
|
||||
}
|
||||
16
src/Events/TenancyBootstrapped.php
Normal file
16
src/Events/TenancyBootstrapped.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Events;
|
||||
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
|
||||
class TenancyBootstrapped
|
||||
{
|
||||
/** @var Tenancy */
|
||||
public $tenancy;
|
||||
|
||||
public function __construct(Tenancy $tenancy)
|
||||
{
|
||||
$this->tenancy = $tenancy;
|
||||
}
|
||||
}
|
||||
|
|
@ -5,12 +5,11 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\Facades;
|
||||
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
use Stancl\Tenancy\TenantManager;
|
||||
|
||||
class Tenancy extends Facade
|
||||
{
|
||||
protected static function getFacadeAccessor()
|
||||
{
|
||||
return TenantManager::class;
|
||||
return \Stancl\Tenancy\Tenancy::class;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,63 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Features;
|
||||
|
||||
use Laravel\Telescope\IncomingEntry;
|
||||
use Laravel\Telescope\Telescope;
|
||||
use Stancl\Tenancy\Contracts\Feature;
|
||||
use Stancl\Tenancy\Middleware\PreventAccessFromTenantDomains;
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
|
||||
// todo rewrite this
|
||||
class TelescopeTags implements Feature
|
||||
{
|
||||
/** @var callable User-specific callback that returns tags. */
|
||||
protected $callback;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->callback = function ($entry) {
|
||||
return [];
|
||||
};
|
||||
}
|
||||
|
||||
public function bootstrap(Tenancy $tenancy): void
|
||||
{
|
||||
if (! class_exists(Telescope::class)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Telescope::tag(function (IncomingEntry $entry) {
|
||||
$tags = $this->getTags($entry);
|
||||
|
||||
if (! request()->route()) {
|
||||
return $tags;
|
||||
}
|
||||
|
||||
// todo lines below
|
||||
$tenantRoute = PreventAccessFromTenantDomains::routeHasMiddleware(request()->route(), 'tenancy')
|
||||
|| PreventAccessFromTenantDomains::routeHasMiddleware(request()->route(), 'universal');
|
||||
|
||||
// Don't do anything if we're visiting a universal route on a central domain
|
||||
if ($tenantRoute && tenancy()->initialized) {
|
||||
$tags = array_merge($tags, [
|
||||
'tenant:' . tenant('id'),
|
||||
]);
|
||||
}
|
||||
|
||||
return $tags;
|
||||
});
|
||||
}
|
||||
|
||||
public function getTags(IncomingEntry $entry): array
|
||||
{
|
||||
return ($this->callback)($entry);
|
||||
}
|
||||
|
||||
public function setCallback(callable $callback)
|
||||
{
|
||||
$this->callback = $callback;
|
||||
}
|
||||
}
|
||||
|
|
@ -5,12 +5,13 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\Features;
|
||||
|
||||
use Illuminate\Contracts\Config\Repository;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Stancl\Tenancy\Contracts\Feature;
|
||||
use Stancl\Tenancy\Events\RevertedToCentralContext;
|
||||
use Stancl\Tenancy\Events\TenancyBootstrapped;
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
use Stancl\Tenancy\Tenant;
|
||||
use Stancl\Tenancy\TenantManager;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
// todo rewrite this
|
||||
class TenantConfig implements Feature
|
||||
{
|
||||
/** @var Repository */
|
||||
|
|
@ -27,26 +28,26 @@ class TenantConfig implements Feature
|
|||
{
|
||||
$this->config = $config;
|
||||
|
||||
foreach ($this->getStorageToConfigMap() as $configKey) {
|
||||
foreach (static::$storageToConfigMap as $configKey) {
|
||||
$this->originalConfig[$configKey] = $this->config[$configKey];
|
||||
}
|
||||
}
|
||||
|
||||
public function bootstrap(Tenancy $tenancy): void
|
||||
{
|
||||
$tenantManager->eventListener('bootstrapped', function (TenantManager $manager) {
|
||||
$this->setTenantConfig($manager->getTenant());
|
||||
Event::listen(TenancyBootstrapped::class, function (TenancyBootstrapped $event) {
|
||||
$this->setTenantConfig($event->tenancy->tenant);
|
||||
});
|
||||
|
||||
$tenantManager->eventListener('ended', function () {
|
||||
Event::listen(RevertedToCentralContext::class, function () {
|
||||
$this->unsetTenantConfig();
|
||||
});
|
||||
}
|
||||
|
||||
public function setTenantConfig(Tenant $tenant): void
|
||||
{
|
||||
foreach ($this->getStorageToConfigMap() as $storageKey => $configKey) {
|
||||
$override = $tenant->data[$storageKey] ?? null;
|
||||
foreach (static::$storageToConfigMap as $storageKey => $configKey) {
|
||||
$override = $tenant->$storageKey ?? null;
|
||||
if (! is_null($override)) {
|
||||
$this->config[$configKey] = $override;
|
||||
}
|
||||
|
|
@ -55,13 +56,8 @@ class TenantConfig implements Feature
|
|||
|
||||
public function unsetTenantConfig(): void
|
||||
{
|
||||
foreach ($this->getStorageToConfigMap() as $configKey) {
|
||||
foreach (static::$storageToConfigMap as $configKey) {
|
||||
$this->config[$configKey] = $this->originalConfig[$configKey];
|
||||
}
|
||||
}
|
||||
|
||||
public function getStorageToConfigMap(): array
|
||||
{
|
||||
return static::$storageToConfigMap;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,46 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Features;
|
||||
|
||||
use Illuminate\Config\Repository;
|
||||
use Illuminate\Support\Facades\Date;
|
||||
use Stancl\Tenancy\Contracts\Feature;
|
||||
use Stancl\Tenancy\Tenant;
|
||||
use Stancl\Tenancy\TenantManager;
|
||||
|
||||
// todo rewrite this
|
||||
class Timestamps implements Feature
|
||||
{
|
||||
/** @var Repository */
|
||||
protected $config;
|
||||
|
||||
public static $format = 'c'; // ISO 8601
|
||||
|
||||
public function __construct(Repository $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function bootstrap(TenantManager $tenantManager): void
|
||||
{
|
||||
$tenantManager->hook('tenant.creating', function ($tm, Tenant $tenant) {
|
||||
$tenant->with('created_at', $this->now());
|
||||
$tenant->with('updated_at', $this->now());
|
||||
});
|
||||
|
||||
$tenantManager->hook('tenant.updating', function ($tm, Tenant $tenant) {
|
||||
$tenant->with('updated_at', $this->now());
|
||||
});
|
||||
|
||||
$tenantManager->hook('tenant.softDeleting', function ($tm, Tenant $tenant) {
|
||||
$tenant->with('deleted_at', $this->now());
|
||||
});
|
||||
}
|
||||
|
||||
public function now(): string
|
||||
{
|
||||
return Date::now()->format(static::$format);
|
||||
}
|
||||
}
|
||||
|
|
@ -12,6 +12,7 @@ use Illuminate\Queue\InteractsWithQueue;
|
|||
use Illuminate\Queue\SerializesModels;
|
||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Events\DatabaseCreated;
|
||||
|
||||
class CreateDatabase implements ShouldQueue
|
||||
{
|
||||
|
|
@ -30,6 +31,8 @@ class CreateDatabase implements ShouldQueue
|
|||
if ($this->tenant->getInternal('create_database') !== false) {
|
||||
$this->tenant->database()->makeCredentials();
|
||||
$this->tenant->database()->manager()->createDatabase($this->tenant);
|
||||
|
||||
event(new DatabaseCreated($this->tenant));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ use Illuminate\Queue\InteractsWithQueue;
|
|||
use Illuminate\Queue\SerializesModels;
|
||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||
use Stancl\Tenancy\Database\Models\Tenant;
|
||||
use Stancl\Tenancy\Events\DatabaseDeleted;
|
||||
|
||||
class DeleteDatabase implements ShouldQueue
|
||||
{
|
||||
|
|
@ -27,5 +28,7 @@ class DeleteDatabase implements ShouldQueue
|
|||
public function handle()
|
||||
{
|
||||
$this->tenant->database()->manager()->deleteDatabase($this->tenant);
|
||||
|
||||
event(new DatabaseDeleted($this->tenant));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,12 +31,8 @@ class MigrateDatabase implements ShouldQueue
|
|||
*/
|
||||
public function handle()
|
||||
{
|
||||
$migrationParameters = [
|
||||
// todo ...
|
||||
];
|
||||
|
||||
Artisan::call('tenants:migrate', [
|
||||
'--tenants' => [$this->tenant->id],
|
||||
] + $migrationParameters);
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Events\Listeners;
|
||||
namespace Stancl\Tenancy\Listeners;
|
||||
|
||||
use Stancl\Tenancy\Events\TenancyBootstrapped;
|
||||
use Stancl\Tenancy\Events\TenancyInitialized;
|
||||
|
||||
class BootstrapTenancy
|
||||
|
|
@ -9,7 +10,9 @@ class BootstrapTenancy
|
|||
public function handle(TenancyInitialized $event)
|
||||
{
|
||||
foreach ($event->tenancy->getBootstrappers() as $bootstrapper) {
|
||||
$bootstrapper->start($event->tenancy->tenant);
|
||||
$bootstrapper->bootstrap($event->tenancy->tenant);
|
||||
}
|
||||
|
||||
event(new TenancyBootstrapped($event->tenancy));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Events\Listeners;
|
||||
namespace Stancl\Tenancy\Listeners;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
|
@ -8,7 +8,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
|
|||
class JobPipeline implements ShouldQueue
|
||||
{
|
||||
/** @var bool */
|
||||
public static $shouldBeQueuedByDefault = false;
|
||||
public static $queueByDefault = false;
|
||||
|
||||
/** @var callable[]|string[] */
|
||||
public $jobs;
|
||||
|
|
@ -22,16 +22,16 @@ class JobPipeline implements ShouldQueue
|
|||
public $passable;
|
||||
|
||||
/** @var bool */
|
||||
public $shouldBeQueued;
|
||||
public $queue;
|
||||
|
||||
public function __construct($jobs, callable $send = null, bool $shouldBeQueued = null)
|
||||
public function __construct($jobs, callable $send = null, bool $queue = null)
|
||||
{
|
||||
$this->jobs = $jobs;
|
||||
$this->send = $send ?? function ($event) {
|
||||
// If no $send callback is set, we'll just pass the event through the jobs.
|
||||
return $event;
|
||||
};
|
||||
$this->shouldBeQueued = $shouldBeQueued ?? static::$shouldBeQueuedByDefault;
|
||||
$this->queue = $queue ?? static::$queueByDefault;
|
||||
}
|
||||
|
||||
/** @param callable[]|string[] $jobs */
|
||||
|
|
@ -47,9 +47,9 @@ class JobPipeline implements ShouldQueue
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function shouldBeQueued(bool $shouldBeQueued)
|
||||
public function queue(bool $queue)
|
||||
{
|
||||
$this->shouldBeQueued = $shouldBeQueued;
|
||||
$this->queue = $queue;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
@ -69,7 +69,7 @@ class JobPipeline implements ShouldQueue
|
|||
return function (...$args) {
|
||||
$executable = $this->executable($args);
|
||||
|
||||
if ($this->shouldBeQueued) {
|
||||
if ($this->queue) {
|
||||
dispatch($executable);
|
||||
} else {
|
||||
dispatch_now($executable);
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Events\Listeners;
|
||||
namespace Stancl\Tenancy\Listeners;
|
||||
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
|
||||
|
|
@ -1,7 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Events\Listeners;
|
||||
namespace Stancl\Tenancy\Listeners;
|
||||
|
||||
use Stancl\Tenancy\Events\RevertedToCentralContext;
|
||||
use Stancl\Tenancy\Events\TenancyEnded;
|
||||
|
||||
class RevertToCentralContext
|
||||
|
|
@ -9,7 +10,9 @@ class RevertToCentralContext
|
|||
public function handle(TenancyEnded $event)
|
||||
{
|
||||
foreach ($event->tenancy->getBootstrappers() as $bootstrapper) {
|
||||
$bootstrapper->end();
|
||||
$bootstrapper->revert();
|
||||
}
|
||||
|
||||
event(new RevertedToCentralContext($event->tenancy));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Events\Listeners;
|
||||
namespace Stancl\Tenancy\Listeners;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\Pivot;
|
||||
|
|
@ -9,7 +9,6 @@ use Illuminate\Http\Request;
|
|||
use Stancl\Tenancy\Resolvers\RequestDataTenantResolver;
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
|
||||
// todo write tests for this
|
||||
class InitializeTenancyByRequestData extends IdentificationMiddleware
|
||||
{
|
||||
/** @var string|null */
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ class CacheTenancyBootstrapper implements TenancyBootstrapper
|
|||
$this->app = $app;
|
||||
}
|
||||
|
||||
public function start(Tenant $tenant)
|
||||
public function bootstrap(Tenant $tenant)
|
||||
{
|
||||
$this->resetFacadeCache();
|
||||
|
||||
|
|
@ -34,7 +34,7 @@ class CacheTenancyBootstrapper implements TenancyBootstrapper
|
|||
});
|
||||
}
|
||||
|
||||
public function end()
|
||||
public function revert()
|
||||
{
|
||||
$this->resetFacadeCache();
|
||||
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ class DatabaseTenancyBootstrapper implements TenancyBootstrapper
|
|||
$this->database = $database;
|
||||
}
|
||||
|
||||
public function start(Tenant $tenant)
|
||||
public function bootstrap(Tenant $tenant)
|
||||
{
|
||||
/** @var TenantWithDatabase $tenant */
|
||||
|
||||
|
|
@ -32,7 +32,7 @@ class DatabaseTenancyBootstrapper implements TenancyBootstrapper
|
|||
$this->database->connectToTenant($tenant);
|
||||
}
|
||||
|
||||
public function end()
|
||||
public function revert()
|
||||
{
|
||||
$this->database->reconnectToCentral();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
|||
});
|
||||
}
|
||||
|
||||
public function start(Tenant $tenant)
|
||||
public function bootstrap(Tenant $tenant)
|
||||
{
|
||||
$suffix = $this->app['config']['tenancy.filesystem.suffix_base'] . $tenant->getTenantKey();
|
||||
|
||||
|
|
@ -69,7 +69,7 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
|||
}
|
||||
}
|
||||
|
||||
public function end()
|
||||
public function revert()
|
||||
{
|
||||
// storage_path()
|
||||
$this->app->useStoragePath($this->originalPaths['storage']);
|
||||
|
|
|
|||
|
|
@ -67,12 +67,12 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
|
|||
}
|
||||
}
|
||||
|
||||
public function start(Tenant $tenant)
|
||||
public function bootstrap(Tenant $tenant)
|
||||
{
|
||||
$this->tenancyInitialized = true;
|
||||
}
|
||||
|
||||
public function end()
|
||||
public function revert()
|
||||
{
|
||||
$this->tenancyInitialized = false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ class RedisTenancyBootstrapper implements TenancyBootstrapper
|
|||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function start(Tenant $tenant)
|
||||
public function bootstrap(Tenant $tenant)
|
||||
{
|
||||
foreach ($this->prefixedConnections() as $connection) {
|
||||
$prefix = $this->config['tenancy.redis.prefix_base'] . $tenant->getTenantKey();
|
||||
|
|
@ -33,7 +33,7 @@ class RedisTenancyBootstrapper implements TenancyBootstrapper
|
|||
}
|
||||
}
|
||||
|
||||
public function end()
|
||||
public function revert()
|
||||
{
|
||||
foreach ($this->prefixedConnections() as $connection) {
|
||||
$client = Redis::connection($connection)->client();
|
||||
|
|
|
|||
|
|
@ -21,23 +21,7 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
{
|
||||
$this->mergeConfigFrom(__DIR__ . '/../assets/config.php', 'tenancy');
|
||||
|
||||
$this->app->bind(Contracts\UniqueIdentifierGenerator::class, $this->app['config']['tenancy.id_generator']);
|
||||
$this->app->singleton(DatabaseManager::class);
|
||||
$this->app->singleton(Tenancy::class);
|
||||
$this->app->extend(Tenancy::class, function (Tenancy $tenancy) {
|
||||
foreach ($this->app['config']['tenancy.features'] as $feature) {
|
||||
$this->app[$feature]->bootstrap($tenancy);
|
||||
}
|
||||
|
||||
return $tenancy;
|
||||
});
|
||||
$this->app->bind(Tenant::class, function ($app) {
|
||||
return $app[Tenancy::class]->tenant;
|
||||
});
|
||||
|
||||
foreach ($this->app['config']['tenancy.bootstrappers'] as $bootstrapper) {
|
||||
$this->app->singleton($bootstrapper);
|
||||
}
|
||||
|
||||
$this->app->singleton(Commands\Migrate::class, function ($app) {
|
||||
return new Commands\Migrate($app['migrator'], $app[DatabaseManager::class]);
|
||||
|
|
@ -52,8 +36,6 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
$this->app->bind('globalCache', function ($app) {
|
||||
return new CacheManager($app);
|
||||
});
|
||||
|
||||
$this->app->register(TenantRouteServiceProvider::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -7,11 +7,10 @@ namespace Stancl\Tenancy\TenantDatabaseManagers;
|
|||
use Illuminate\Contracts\Config\Repository;
|
||||
use Illuminate\Database\Connection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Contracts\Future\CanSetConnection;
|
||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||
|
||||
class MySQLDatabaseManager implements TenantDatabaseManager, CanSetConnection
|
||||
class MySQLDatabaseManager implements TenantDatabaseManager
|
||||
{
|
||||
/** @var string */
|
||||
protected $connection;
|
||||
|
|
|
|||
|
|
@ -7,11 +7,10 @@ namespace Stancl\Tenancy\TenantDatabaseManagers;
|
|||
use Illuminate\Contracts\Config\Repository;
|
||||
use Illuminate\Database\Connection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Contracts\Future\CanSetConnection;
|
||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||
|
||||
class PostgreSQLDatabaseManager implements TenantDatabaseManager, CanSetConnection
|
||||
class PostgreSQLDatabaseManager implements TenantDatabaseManager
|
||||
{
|
||||
/** @var string */
|
||||
protected $connection;
|
||||
|
|
|
|||
|
|
@ -7,11 +7,10 @@ namespace Stancl\Tenancy\TenantDatabaseManagers;
|
|||
use Illuminate\Contracts\Config\Repository;
|
||||
use Illuminate\Database\Connection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Stancl\Tenancy\Contracts\Future\CanSetConnection;
|
||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||
|
||||
class PostgreSQLSchemaManager implements TenantDatabaseManager, CanSetConnection
|
||||
class PostgreSQLSchemaManager implements TenantDatabaseManager
|
||||
{
|
||||
/** @var string */
|
||||
protected $connection;
|
||||
|
|
|
|||
|
|
@ -38,4 +38,9 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
|
|||
|
||||
return $baseConfig;
|
||||
}
|
||||
|
||||
public function setConnection(string $connection): void
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,22 +0,0 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy;
|
||||
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
class TenantRouteServiceProvider extends RouteServiceProvider
|
||||
{
|
||||
public function map()
|
||||
{
|
||||
$this->app->booted(function () {
|
||||
if (file_exists(base_path('routes/tenant.php'))) {
|
||||
Route::middleware(['web'])
|
||||
->namespace($this->app['config']['tenancy.tenant_route_namespace'] ?? 'App\Http\Controllers')
|
||||
->group(base_path('routes/tenant.php'));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -4,9 +4,9 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\Traits;
|
||||
|
||||
use Stancl\Tenancy\Tenant;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
trait TenantAwareCommand
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue