mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-04 13:14:03 +00:00
Change default tenant model, write more tests, cleanup
This commit is contained in:
parent
c32f229dd5
commit
de53b81c0e
33 changed files with 210 additions and 90 deletions
|
|
@ -43,7 +43,11 @@ class Install extends Command
|
|||
$this->info('Found routes/tenant.php.');
|
||||
}
|
||||
|
||||
// todo tenancy SP stub
|
||||
$this->callSilent('vendor:publish', [
|
||||
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider',
|
||||
'--tag' => 'provider',
|
||||
]);
|
||||
$this->info('✔️ Created TenancyServiceProvider.php');
|
||||
|
||||
$this->callSilent('vendor:publish', [
|
||||
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider',
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
trait HasADataColumn
|
||||
trait HasDataColumn
|
||||
{
|
||||
public static $priorityListeners = [];
|
||||
|
||||
|
|
@ -13,9 +13,9 @@ trait HasADataColumn
|
|||
*
|
||||
* @var string
|
||||
*/
|
||||
public $dataEncodingStatus = 'decoded'; // todo write tests for this
|
||||
public $dataEncodingStatus = 'decoded';
|
||||
|
||||
public static function bootHasADataColumn()
|
||||
public static function bootHasDataColumn()
|
||||
{
|
||||
$encode = function (self $model) {
|
||||
if ($model->dataEncodingStatus === 'encoded') {
|
||||
16
src/Database/Concerns/HasDatabase.php
Normal file
16
src/Database/Concerns/HasDatabase.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Stancl\Tenancy\Database\Concerns;
|
||||
|
||||
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||
use Stancl\Tenancy\DatabaseConfig;
|
||||
|
||||
trait HasDatabase
|
||||
{
|
||||
public function database(): DatabaseConfig
|
||||
{
|
||||
/** @var TenantWithDatabase $this */
|
||||
|
||||
return new DatabaseConfig($this);
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,6 @@ 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;
|
||||
|
|
@ -15,14 +14,13 @@ use Stancl\Tenancy\Database\Concerns;
|
|||
* @property Carbon $updated_at
|
||||
* @property array $data
|
||||
*/
|
||||
class Tenant extends Model implements Contracts\TenantWithDatabase // todo base model that isn't TenantWithDatabase & domains
|
||||
class Tenant extends Model implements Contracts\Tenant
|
||||
{
|
||||
use Concerns\CentralConnection,
|
||||
Concerns\HasADataColumn,
|
||||
Concerns\HasDataColumn,
|
||||
Concerns\GeneratesIds,
|
||||
Concerns\HasADataColumn,
|
||||
Concerns\HasDomains {
|
||||
Concerns\HasADataColumn::getCasts as dataColumnCasts;
|
||||
Concerns\HasDataColumn {
|
||||
Concerns\HasDataColumn::getCasts as dataColumnCasts;
|
||||
}
|
||||
|
||||
protected $table = 'tenants';
|
||||
|
|
@ -69,11 +67,6 @@ class Tenant extends Model implements Contracts\TenantWithDatabase // todo base
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function database(): DatabaseConfig
|
||||
{
|
||||
return new DatabaseConfig($this);
|
||||
}
|
||||
|
||||
public function run(callable $callback)
|
||||
{
|
||||
$originalTenant = tenant();
|
||||
|
|
|
|||
|
|
@ -4,16 +4,17 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Str;
|
||||
use Stancl\Tenancy\Contracts\ManagesDatabaseUsers;
|
||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||
use Stancl\Tenancy\Database\Models\Tenant;
|
||||
use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException;
|
||||
use Stancl\Tenancy\Contracts\TenantWithDatabase as Tenant;
|
||||
|
||||
class DatabaseConfig
|
||||
{
|
||||
/** @var Tenant */
|
||||
/** @var Tenant|Model */
|
||||
public $tenant;
|
||||
|
||||
/** @var callable */
|
||||
|
|
@ -90,6 +91,10 @@ class DatabaseConfig
|
|||
$this->tenant->setInternal('db_username', $this->getUsername() ?? (static::$usernameGenerator)($this->tenant));
|
||||
$this->tenant->setInternal('db_password', $this->getPassword() ?? (static::$passwordGenerator)($this->tenant));
|
||||
}
|
||||
|
||||
if ($this->tenant->exists) {
|
||||
$this->tenant->save();
|
||||
}
|
||||
}
|
||||
|
||||
public function getTemplateConnectionName(): string
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ class DatabaseManager
|
|||
* @throws DatabaseManagerNotRegisteredException
|
||||
* @throws TenantDatabaseAlreadyExistsException
|
||||
*/
|
||||
public function ensureTenantCanBeCreated(TenantWithDatabase $tenant): void // todo do we need this?
|
||||
public function ensureTenantCanBeCreated(TenantWithDatabase $tenant): void
|
||||
{
|
||||
if ($tenant->database()->manager()->databaseExists($database = $tenant->database()->getName())) {
|
||||
throw new TenantDatabaseAlreadyExistsException($database);
|
||||
|
|
|
|||
|
|
@ -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\DatabaseManager;
|
||||
use Stancl\Tenancy\Events\DatabaseCreated;
|
||||
|
||||
class CreateDatabase implements ShouldQueue
|
||||
|
|
@ -26,9 +27,10 @@ class CreateDatabase implements ShouldQueue
|
|||
$this->tenant = $tenant;
|
||||
}
|
||||
|
||||
public function handle()
|
||||
public function handle(DatabaseManager $databaseManager)
|
||||
{
|
||||
if ($this->tenant->getInternal('create_database') !== false) {
|
||||
$databaseManager->ensureTenantCanBeCreated($this->tenant);
|
||||
$this->tenant->database()->makeCredentials();
|
||||
$this->tenant->database()->manager()->createDatabase($this->tenant);
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use Illuminate\Contracts\Queue\ShouldQueue;
|
|||
class JobPipeline implements ShouldQueue
|
||||
{
|
||||
/** @var bool */
|
||||
public static $queueByDefault = false;
|
||||
public static $shouldBeQueuedByDefault = false;
|
||||
|
||||
/** @var callable[]|string[] */
|
||||
public $jobs;
|
||||
|
|
@ -22,16 +22,16 @@ class JobPipeline implements ShouldQueue
|
|||
public $passable;
|
||||
|
||||
/** @var bool */
|
||||
public $queue;
|
||||
public $shouldBeQueued;
|
||||
|
||||
public function __construct($jobs, callable $send = null, bool $queue = null)
|
||||
public function __construct($jobs, callable $send = null, bool $shouldBeQueued = 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->queue = $queue ?? static::$queueByDefault;
|
||||
$this->shouldBeQueued = $shouldBeQueued ?? static::$shouldBeQueuedByDefault;
|
||||
}
|
||||
|
||||
/** @param callable[]|string[] $jobs */
|
||||
|
|
@ -47,9 +47,9 @@ class JobPipeline implements ShouldQueue
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function queue(bool $queue)
|
||||
public function shouldBeQueued(bool $shouldBeQueued)
|
||||
{
|
||||
$this->queue = $queue;
|
||||
$this->shouldBeQueued = $shouldBeQueued;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
@ -69,7 +69,7 @@ class JobPipeline implements ShouldQueue
|
|||
return function (...$args) {
|
||||
$executable = $this->executable($args);
|
||||
|
||||
if ($this->queue) {
|
||||
if ($this->shouldBeQueued) {
|
||||
dispatch($executable);
|
||||
} else {
|
||||
dispatch_now($executable);
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy;
|
||||
|
||||
use Illuminate\Cache\CacheManager;
|
||||
use Illuminate\Queue\Events\JobProcessing;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Stancl\Tenancy\TenancyBootstrappers\FilesystemTenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
|
@ -23,6 +22,31 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
|
||||
$this->app->singleton(DatabaseManager::class);
|
||||
|
||||
// Make sure Tenancy is stateful.
|
||||
$this->app->singleton(Tenancy::class);
|
||||
|
||||
// Make sure features are bootstrapped as soon as Tenancy is instantiated.
|
||||
$this->app->extend(Tenancy::class, function (Tenancy $tenancy) {
|
||||
foreach ($this->app['config']['tenancy.features'] as $feature) {
|
||||
$this->app[$feature]->bootstrap($tenancy);
|
||||
}
|
||||
|
||||
return $tenancy;
|
||||
});
|
||||
|
||||
// Make it possible to inject the current tenant by typehinting the Tenant contract.
|
||||
$this->app->bind(Tenant::class, function ($app) {
|
||||
return $app[Tenancy::class]->tenant;
|
||||
});
|
||||
|
||||
// Make sure bootstrappers are stateful (singletons).
|
||||
foreach ($this->app['config']['tenancy.bootstrappers'] as $bootstrapper) {
|
||||
$this->app->singleton($bootstrapper);
|
||||
}
|
||||
|
||||
// Bind the class in the tenancy.id_generator config to the UniqueIdentifierGenerator abstract.
|
||||
$this->app->bind(Contracts\UniqueIdentifierGenerator::class, $this->app['config']['tenancy.id_generator']);
|
||||
|
||||
$this->app->singleton(Commands\Migrate::class, function ($app) {
|
||||
return new Commands\Migrate($app['migrator'], $app[DatabaseManager::class]);
|
||||
});
|
||||
|
|
@ -63,6 +87,10 @@ class TenancyServiceProvider extends ServiceProvider
|
|||
__DIR__ . '/../assets/migrations/' => database_path('migrations'),
|
||||
], 'migrations');
|
||||
|
||||
$this->publishes([
|
||||
__DIR__ . '/../assets/TenancyServiceProvider.stub.php' => app_path('Providers/TenancyServiceProvider.php'),
|
||||
], 'migrations');
|
||||
|
||||
$this->loadRoutesFrom(__DIR__ . '/routes.php');
|
||||
|
||||
$this->app->singleton('globalUrl', function ($app) {
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
|
|||
public function createDatabase(TenantWithDatabase $tenant): bool
|
||||
{
|
||||
try {
|
||||
return fclose(fopen(database_path($tenant->database()->getName()), 'w'));
|
||||
return file_put_contents(database_path($tenant->database()->getName()), '');
|
||||
} catch (\Throwable $th) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue