diff --git a/src/Commands/Down.php b/src/Commands/Down.php index 6b390957..d79d2b20 100644 --- a/src/Commands/Down.php +++ b/src/Commands/Down.php @@ -22,24 +22,28 @@ class Down extends DownCommand public function handle(): int { - // The base down command is heavily used. Instead of saving the data inside a file, - // the data is stored the tenant database, which means some Laravel features - // are not available with tenants. - + // First we retrieve the compiled payload with all the + // parameters given, then for each tenant we will + // put down for maintenance and pass the payload. $payload = $this->getDownDatabasePayload(); - // This runs for all tenants if no --tenants are specified tenancy()->runForMultiple($this->getTenants(), function ($tenant) use ($payload) { - $this->line("Tenant: {$tenant['id']}"); + $this->components->info("Tenant: {$tenant->getTenantKey()}"); $tenant->putDownForMaintenance($payload); }); - $this->comment('Tenants are now in maintenance mode.'); + $this->components->info('Tenants are now in maintenance mode.'); 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 { return [ diff --git a/src/Commands/Install.php b/src/Commands/Install.php index 12a2c2c9..830080a1 100644 --- a/src/Commands/Install.php +++ b/src/Commands/Install.php @@ -14,40 +14,93 @@ class Install extends Command public function handle(): void { - $this->comment('Installing stancl/tenancy...'); - $this->callSilent('vendor:publish', [ - '--provider' => 'Stancl\Tenancy\TenancyServiceProvider', - '--tag' => 'config', - ]); - $this->info('✔️ Created config/tenancy.php'); + $this->newLine(); - if (! file_exists(base_path('routes/tenant.php'))) { + $this->components->task('Publishing config file', function () { $this->callSilent('vendor:publish', [ '--provider' => 'Stancl\Tenancy\TenancyServiceProvider', - '--tag' => 'routes', + '--tag' => 'config', ]); - $this->info('✔️ Created 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', [ + '--provider' => 'Stancl\Tenancy\TenancyServiceProvider', + '--tag' => 'routes', + ]); + }); + $this->newLine(); } else { - $this->info('Found routes/tenant.php.'); + $this->components->warn('File [routes/tenant.php] already existe.'); } - $this->callSilent('vendor:publish', [ - '--provider' => 'Stancl\Tenancy\TenancyServiceProvider', - '--tag' => 'providers', - ]); - $this->info('✔️ Created TenancyServiceProvider.php'); + $this->patienceIsKeyToLife(); - $this->callSilent('vendor:publish', [ - '--provider' => 'Stancl\Tenancy\TenancyServiceProvider', - '--tag' => 'migrations', - ]); - $this->info('✔️ Created migrations. Remember to run [php artisan migrate]!'); + $this->components->task('Publishing providers', function () { + $this->callSilent('vendor:publish', [ + '--provider' => 'Stancl\Tenancy\TenancyServiceProvider', + '--tag' => 'providers', + ]); + }); - if (! is_dir(database_path('migrations/tenant'))) { - mkdir(database_path('migrations/tenant')); - $this->info('✔️ Created database/migrations/tenant folder.'); + $this->patienceIsKeyToLife(); + $this->newLine(); + + $this->components->task('Publishing migrations', function () { + $this->callSilent('vendor:publish', [ + '--provider' => 'Stancl\Tenancy\TenancyServiceProvider', + '--tag' => 'migrations', + ]); + }); + + $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')); + }); + } 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'); + } + } } } diff --git a/src/Commands/Link.php b/src/Commands/Link.php index 53f3cf6f..9ca0ec73 100644 --- a/src/Commands/Link.php +++ b/src/Commands/Link.php @@ -42,7 +42,7 @@ class Link extends Command { RemoveStorageSymlinksAction::handle($tenants); - $this->info('The links have been removed.'); + $this->components->info('The links have been removed.'); } protected function createLinks(LazyCollection $tenants): void @@ -53,6 +53,6 @@ class Link extends Command (bool) ($this->option('force') ?? false), ); - $this->info('The links have been created.'); + $this->components->info('The links have been created.'); } } diff --git a/src/Commands/Migrate.php b/src/Commands/Migrate.php index 739b56de..82395fcc 100644 --- a/src/Commands/Migrate.php +++ b/src/Commands/Migrate.php @@ -43,7 +43,7 @@ class Migrate extends MigrateCommand } tenancy()->runForMultiple($this->getTenants(), function ($tenant) { - $this->line("Tenant: {$tenant->getTenantKey()}"); + $this->components->info("Tenant: {$tenant->getTenantKey()}"); event(new MigratingDatabase($tenant)); diff --git a/src/Commands/MigrateFresh.php b/src/Commands/MigrateFresh.php index 56a6047f..389883c3 100644 --- a/src/Commands/MigrateFresh.php +++ b/src/Commands/MigrateFresh.php @@ -26,20 +26,23 @@ final class MigrateFresh extends Command public function handle(): void { tenancy()->runForMultiple($this->getTenants(), function ($tenant) { - $this->info('Dropping tables.'); - $this->call('db:wipe', array_filter([ - '--database' => 'tenant', - '--drop-views' => $this->option('drop-views'), - '--force' => true, - ])); + $this->components->info("Tenant: {$tenant->getTenantKey()}"); - $this->info('Migrating.'); - $this->callSilent('tenants:migrate', [ - '--tenants' => [$tenant->getTenantKey()], - '--force' => true, - ]); + $this->components->task('Dropping tables', function () { + $this->callSilently('db:wipe', array_filter([ + '--database' => 'tenant', + '--drop-views' => $this->option('drop-views'), + '--force' => true, + ])); + }); + + $this->components->task('Migrating', function () use ($tenant){ + $this->callSilent('tenants:migrate', [ + '--tenants' => [$tenant->getTenantKey()], + '--force' => true, + ]); + }); }); - $this->info('Done.'); } } diff --git a/src/Commands/Rollback.php b/src/Commands/Rollback.php index d3989cc0..1e84ab12 100644 --- a/src/Commands/Rollback.php +++ b/src/Commands/Rollback.php @@ -37,7 +37,7 @@ class Rollback extends RollbackCommand } tenancy()->runForMultiple($this->getTenants(), function ($tenant) { - $this->line("Tenant: {$tenant->getTenantKey()}"); + $this->components->info("Tenant: {$tenant->getTenantKey()}"); event(new RollingBackDatabase($tenant)); diff --git a/src/Commands/Run.php b/src/Commands/Run.php index 9bb04716..3456026f 100644 --- a/src/Commands/Run.php +++ b/src/Commands/Run.php @@ -24,7 +24,7 @@ class Run extends Command $argvInput = $this->argvInput(); tenancy()->runForMultiple($this->getTenants(), function ($tenant) use ($argvInput) { - $this->line("Tenant: {$tenant->getTenantKey()}"); + $this->components->info("Tenant: {$tenant->getTenantKey()}"); $this->getLaravel() ->make(Kernel::class) diff --git a/src/Commands/Seed.php b/src/Commands/Seed.php index 496c04e6..8ed0b6d9 100644 --- a/src/Commands/Seed.php +++ b/src/Commands/Seed.php @@ -36,7 +36,7 @@ class Seed extends SeedCommand } tenancy()->runForMultiple($this->getTenants(), function ($tenant) { - $this->line("Tenant: {$tenant->getTenantKey()}"); + $this->components->info("Tenant: {$tenant->getTenantKey()}"); event(new SeedingDatabase($tenant)); diff --git a/src/Commands/TenantList.php b/src/Commands/TenantList.php index 9fd3f8bd..61be5d37 100644 --- a/src/Commands/TenantList.php +++ b/src/Commands/TenantList.php @@ -16,17 +16,40 @@ class TenantList extends Command public function handle(): void { - $this->info('Listing all tenants.'); - $tenants = tenancy()->query()->cursor(); + $this->components->info("Listing {$tenants->count()} tenants."); + foreach ($tenants as $tenant) { /** @var Model&Tenant $tenant */ - if ($tenant->domains) { - $this->line("[Tenant] {$tenant->getTenantKeyName()}: {$tenant->getTenantKey()} @ " . implode('; ', $tenant->domains->pluck('domain')->toArray() ?? [])); - } else { - $this->line("[Tenant] {$tenant->getTenantKeyName()}: {$tenant->getTenantKey()}"); - } + $this->components->twoColumnDetail($this->tenantCli($tenant), $this->domainsCli($tenant)); } } + + /** + * Generate the visual cli output for the tenant name + * + * @param Model $tenant + * @return string + */ + protected function tenantCli(Model $tenant): string + { + return "{$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 "{$tenant->domains->pluck('domain')->implode(' ; ')}"; + } } diff --git a/src/Commands/Up.php b/src/Commands/Up.php index a3f690c2..51e7eb0b 100644 --- a/src/Commands/Up.php +++ b/src/Commands/Up.php @@ -18,10 +18,10 @@ class Up extends Command public function handle(): void { tenancy()->runForMultiple($this->getTenants(), function ($tenant) { - $this->line("Tenant: {$tenant['id']}"); + $this->components->info("Tenant: {$tenant->getTenantKey()}"); $tenant->bringUpFromMaintenance(); }); - $this->comment('Tenants are now out of maintenance mode.'); + $this->components->info('Tenants are now out of maintenance mode.'); } }