1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 21:14:03 +00:00

Using laravel components

This commit is contained in:
j.stein 2022-10-09 00:11:37 -04:00
parent 6ee93d0441
commit 2f15dd5473
10 changed files with 142 additions and 59 deletions

View file

@ -22,24 +22,28 @@ class Down extends DownCommand
public function handle(): int public function handle(): int
{ {
// The base down command is heavily used. Instead of saving the data inside a file, // First we retrieve the compiled payload with all the
// the data is stored the tenant database, which means some Laravel features // parameters given, then for each tenant we will
// are not available with tenants. // put down for maintenance and pass the payload.
$payload = $this->getDownDatabasePayload(); $payload = $this->getDownDatabasePayload();
// This runs for all tenants if no --tenants are specified
tenancy()->runForMultiple($this->getTenants(), function ($tenant) use ($payload) { tenancy()->runForMultiple($this->getTenants(), function ($tenant) use ($payload) {
$this->line("Tenant: {$tenant['id']}"); $this->components->info("Tenant: {$tenant->getTenantKey()}");
$tenant->putDownForMaintenance($payload); $tenant->putDownForMaintenance($payload);
}); });
$this->comment('Tenants are now in maintenance mode.'); $this->components->info('Tenants are now in maintenance mode.');
return 0; return 0;
} }
/** Get the payload to be placed in the "down" file. */ /**
* Get the payload to be placed in the "down" file. This
* payload is the same as the original function
* but without the 'template' option
*
* @return array
*/
protected function getDownDatabasePayload(): array protected function getDownDatabasePayload(): array
{ {
return [ return [

View file

@ -14,40 +14,93 @@ class Install extends Command
public function handle(): void public function handle(): void
{ {
$this->comment('Installing stancl/tenancy...'); $this->newLine();
$this->components->task('Publishing config file', function () {
$this->callSilent('vendor:publish', [ $this->callSilent('vendor:publish', [
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider', '--provider' => 'Stancl\Tenancy\TenancyServiceProvider',
'--tag' => 'config', '--tag' => 'config',
]); ]);
$this->info('✔️ Created config/tenancy.php'); });
if (! file_exists(base_path('routes/tenant.php'))) { $this->patienceIsKeyToLife();
$this->newLine();
if (!file_exists(base_path('routes/tenant.php'))) {
$this->components->task('Publishing routes', function () {
$this->callSilent('vendor:publish', [ $this->callSilent('vendor:publish', [
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider', '--provider' => 'Stancl\Tenancy\TenancyServiceProvider',
'--tag' => 'routes', '--tag' => 'routes',
]); ]);
$this->info('✔️ Created routes/tenant.php'); });
$this->newLine();
} else { } else {
$this->info('Found routes/tenant.php.'); $this->components->warn('File [routes/tenant.php] already existe.');
} }
$this->patienceIsKeyToLife();
$this->components->task('Publishing providers', function () {
$this->callSilent('vendor:publish', [ $this->callSilent('vendor:publish', [
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider', '--provider' => 'Stancl\Tenancy\TenancyServiceProvider',
'--tag' => 'providers', '--tag' => 'providers',
]); ]);
$this->info('✔️ Created TenancyServiceProvider.php'); });
$this->patienceIsKeyToLife();
$this->newLine();
$this->components->task('Publishing migrations', function () {
$this->callSilent('vendor:publish', [ $this->callSilent('vendor:publish', [
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider', '--provider' => 'Stancl\Tenancy\TenancyServiceProvider',
'--tag' => 'migrations', '--tag' => 'migrations',
]); ]);
$this->info('✔️ Created migrations. Remember to run [php artisan migrate]!'); });
if (! is_dir(database_path('migrations/tenant'))) { $this->patienceIsKeyToLife();
$this->newLine();
if (!is_dir(database_path('migrations/tenant'))) {
$this->components->task('Creating database/migrations/tenant folder', function () {
mkdir(database_path('migrations/tenant')); mkdir(database_path('migrations/tenant'));
$this->info('✔️ Created database/migrations/tenant folder.'); });
} else {
$this->components->warn('Folder [database/migrations/tenant] already existe.');
} }
$this->comment('✨️ stancl/tenancy installed successfully.'); $this->components->info('✨️ stancl/tenancy installed successfully.');
$this->patienceIsKeyToLife();
$this->askForSupport();
}
/**
* Pause the console execution for 1 second, help the user to have time and read the output
*
* @return void
*/
public function patienceIsKeyToLife(): void
{
sleep(1);
}
/**
* If the user accepts, opens the GitHub project in the browser
*
* @return void
*/
public function askForSupport(): void
{
if ($this->components->confirm("Would you like to show your support by starring the project on github ?", true)) {
if (PHP_OS_FAMILY === 'Darwin') {
exec('open https://github.com/archtechx/tenancy');
}
if (PHP_OS_FAMILY === 'Windows') {
exec('start https://github.com/archtechx/tenancy');
}
if (PHP_OS_FAMILY === 'Linux') {
exec('xdg-open https://github.com/archtechx/tenancy');
}
}
} }
} }

View file

@ -42,7 +42,7 @@ class Link extends Command
{ {
RemoveStorageSymlinksAction::handle($tenants); RemoveStorageSymlinksAction::handle($tenants);
$this->info('The links have been removed.'); $this->components->info('The links have been removed.');
} }
protected function createLinks(LazyCollection $tenants): void protected function createLinks(LazyCollection $tenants): void
@ -53,6 +53,6 @@ class Link extends Command
(bool) ($this->option('force') ?? false), (bool) ($this->option('force') ?? false),
); );
$this->info('The links have been created.'); $this->components->info('The links have been created.');
} }
} }

View file

@ -43,7 +43,7 @@ class Migrate extends MigrateCommand
} }
tenancy()->runForMultiple($this->getTenants(), function ($tenant) { tenancy()->runForMultiple($this->getTenants(), function ($tenant) {
$this->line("Tenant: {$tenant->getTenantKey()}"); $this->components->info("Tenant: {$tenant->getTenantKey()}");
event(new MigratingDatabase($tenant)); event(new MigratingDatabase($tenant));

View file

@ -26,20 +26,23 @@ final class MigrateFresh extends Command
public function handle(): void public function handle(): void
{ {
tenancy()->runForMultiple($this->getTenants(), function ($tenant) { tenancy()->runForMultiple($this->getTenants(), function ($tenant) {
$this->info('Dropping tables.'); $this->components->info("Tenant: {$tenant->getTenantKey()}");
$this->call('db:wipe', array_filter([
$this->components->task('Dropping tables', function () {
$this->callSilently('db:wipe', array_filter([
'--database' => 'tenant', '--database' => 'tenant',
'--drop-views' => $this->option('drop-views'), '--drop-views' => $this->option('drop-views'),
'--force' => true, '--force' => true,
])); ]));
});
$this->info('Migrating.'); $this->components->task('Migrating', function () use ($tenant){
$this->callSilent('tenants:migrate', [ $this->callSilent('tenants:migrate', [
'--tenants' => [$tenant->getTenantKey()], '--tenants' => [$tenant->getTenantKey()],
'--force' => true, '--force' => true,
]); ]);
}); });
});
$this->info('Done.');
} }
} }

View file

@ -37,7 +37,7 @@ class Rollback extends RollbackCommand
} }
tenancy()->runForMultiple($this->getTenants(), function ($tenant) { tenancy()->runForMultiple($this->getTenants(), function ($tenant) {
$this->line("Tenant: {$tenant->getTenantKey()}"); $this->components->info("Tenant: {$tenant->getTenantKey()}");
event(new RollingBackDatabase($tenant)); event(new RollingBackDatabase($tenant));

View file

@ -24,7 +24,7 @@ class Run extends Command
$argvInput = $this->argvInput(); $argvInput = $this->argvInput();
tenancy()->runForMultiple($this->getTenants(), function ($tenant) use ($argvInput) { tenancy()->runForMultiple($this->getTenants(), function ($tenant) use ($argvInput) {
$this->line("Tenant: {$tenant->getTenantKey()}"); $this->components->info("Tenant: {$tenant->getTenantKey()}");
$this->getLaravel() $this->getLaravel()
->make(Kernel::class) ->make(Kernel::class)

View file

@ -36,7 +36,7 @@ class Seed extends SeedCommand
} }
tenancy()->runForMultiple($this->getTenants(), function ($tenant) { tenancy()->runForMultiple($this->getTenants(), function ($tenant) {
$this->line("Tenant: {$tenant->getTenantKey()}"); $this->components->info("Tenant: {$tenant->getTenantKey()}");
event(new SeedingDatabase($tenant)); event(new SeedingDatabase($tenant));

View file

@ -16,17 +16,40 @@ class TenantList extends Command
public function handle(): void public function handle(): void
{ {
$this->info('Listing all tenants.');
$tenants = tenancy()->query()->cursor(); $tenants = tenancy()->query()->cursor();
$this->components->info("Listing {$tenants->count()} tenants.");
foreach ($tenants as $tenant) { foreach ($tenants as $tenant) {
/** @var Model&Tenant $tenant */ /** @var Model&Tenant $tenant */
if ($tenant->domains) { $this->components->twoColumnDetail($this->tenantCli($tenant), $this->domainsCli($tenant));
$this->line("[Tenant] {$tenant->getTenantKeyName()}: {$tenant->getTenantKey()} @ " . implode('; ', $tenant->domains->pluck('domain')->toArray() ?? []));
} else {
$this->line("[Tenant] {$tenant->getTenantKeyName()}: {$tenant->getTenantKey()}");
} }
} }
/**
* Generate the visual cli output for the tenant name
*
* @param Model $tenant
* @return string
*/
protected function tenantCli(Model $tenant): string
{
return "<fg=yellow>{$tenant->getTenantKeyName()}: {$tenant->getTenantKey()}</>";
}
/**
* Generate the visual cli output for the domain names
*
* @param Model $tenant
* @return string|null
*/
protected function domainsCli(Model $tenant): ?string
{
if (is_null($tenant->domains)){
return null;
}
return "<fg=blue;options=bold>{$tenant->domains->pluck('domain')->implode(' ; ')}</>";
} }
} }

View file

@ -18,10 +18,10 @@ class Up extends Command
public function handle(): void public function handle(): void
{ {
tenancy()->runForMultiple($this->getTenants(), function ($tenant) { tenancy()->runForMultiple($this->getTenants(), function ($tenant) {
$this->line("Tenant: {$tenant['id']}"); $this->components->info("Tenant: {$tenant->getTenantKey()}");
$tenant->bringUpFromMaintenance(); $tenant->bringUpFromMaintenance();
}); });
$this->comment('Tenants are now out of maintenance mode.'); $this->components->info('Tenants are now out of maintenance mode.');
} }
} }