From 7b8ab13f3d4045c5bc834042724885a490ff4d30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 18 Oct 2022 18:37:41 +0200 Subject: [PATCH] improve Install & TenantList commands --- assets/config.php | 8 +-- src/Commands/Install.php | 139 ++++++++++++++++++++++++------------ src/Commands/TenantList.php | 28 +++----- 3 files changed, 108 insertions(+), 67 deletions(-) diff --git a/assets/config.php b/assets/config.php index 6130bade..c30c5ff6 100644 --- a/assets/config.php +++ b/assets/config.php @@ -2,16 +2,14 @@ declare(strict_types=1); -use Stancl\Tenancy\Database\Models\Domain; -use Stancl\Tenancy\Database\Models\Tenant; use Stancl\Tenancy\Middleware; use Stancl\Tenancy\Resolvers; return [ - 'tenant_model' => Tenant::class, - 'id_generator' => Stancl\Tenancy\UUIDGenerator::class, + 'tenant_model' => Stancl\Tenancy\Database\Models\Tenant::class, + 'domain_model' => Stancl\Tenancy\Database\Models\Domain::class, - 'domain_model' => Domain::class, + 'id_generator' => Stancl\Tenancy\UUIDGenerator::class, /** * The list of domains hosting your central app. diff --git a/src/Commands/Install.php b/src/Commands/Install.php index dd9b4153..424aa261 100644 --- a/src/Commands/Install.php +++ b/src/Commands/Install.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Stancl\Tenancy\Commands; +use Closure; use Illuminate\Console\Command; class Install extends Command @@ -14,54 +15,42 @@ class Install extends Command public function handle(): int { - $this->newLine(); + $this->step( + name: 'Publishing config file', + tag: 'config', + file: 'config/tenancy.php', + newLineBefore: true, + ); - $this->components->task('Publishing config file [config/tenancy.php]', function () { - $this->callSilent('vendor:publish', [ - '--provider' => 'Stancl\Tenancy\TenancyServiceProvider', - '--tag' => 'config', - ]); - }); + $this->step( + name: 'Publishing routes [routes/tenant.php]', + tag: 'routes', + file: 'routes/tenant.php', + ); - $this->newLine(); + $this->step( + name: 'Publishing service provider', + tag: 'providers', + file: 'app/Providers/TenancyServiceProvider.php', + ); - if (! file_exists(base_path('routes/tenant.php'))) { - $this->components->task('Publishing routes [routes/tenant.php]', function () { - $this->callSilent('vendor:publish', [ - '--provider' => 'Stancl\Tenancy\TenancyServiceProvider', - '--tag' => 'routes', - ]); - }); - $this->newLine(); - } else { - $this->components->warn('File [routes/tenant.php] already exists.'); - } + $this->step( + name: 'Publishing migrations', + tag: 'migrations', + files: [ + 'database/migrations/2019_09_15_000010_create_tenants_table.php', + 'database/migrations/2019_09_15_000020_create_domains_table.php', + ], + warning: 'Migrations already exist', + ); - $this->components->task('Publishing service provider [app/Providers/TenancyServiceProvider.php]', function () { - $this->callSilent('vendor:publish', [ - '--provider' => 'Stancl\Tenancy\TenancyServiceProvider', - '--tag' => 'providers', - ]); - }); - - $this->newLine(); - - $this->components->task('Publishing migrations', function () { - $this->callSilent('vendor:publish', [ - '--provider' => 'Stancl\Tenancy\TenancyServiceProvider', - '--tag' => 'migrations', - ]); - }); - - $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 exists.'); - } + $this->step( + name: 'Creating [database/migrations/tenant] folder', + task: fn () => mkdir(database_path('migrations/tenant')), + unless: is_dir(database_path('migrations/tenant')), + warning: 'Folder [database/migrations/tenant] already exists.', + newLineAfter: true, + ); $this->components->info('✨️ Tenancy for Laravel successfully installed.'); @@ -70,6 +59,68 @@ class Install extends Command return 0; } + /** + * Run a step of the installation process. + * + * @param string $name The name of the step. + * @param Closure|null $task The task code. + * @param bool $unless Condition specifying when the task should NOT run. + * @param string|null $warning Warning shown when the $unless condition is true. + * @param string|null $file Name of the file being added. + * @param string|null $tag The tag being published. + * @param array|null $files Names of files being added. + * @param bool $newLineBefore Should a new line be printed after the step. + * @param bool $newLineAfter Should a new line be printed after the step. + */ + protected function step( + string $name, + Closure $task = null, + bool $unless = false, + string $warning = null, + string $file = null, + string $tag = null, + array $files = null, + bool $newLineBefore = false, + bool $newLineAfter = false, + ): void { + if ($file) { + $name .= " [$file]"; // Append clickable path to the task name + $unless = file_exists(base_path($file)); // Make the condition a check for the file's existence + $warning = "File [$file] already exists."; // Make the warning a message about the file already existing + } + + if ($tag) { + $task = fn () => $this->callSilent('vendor:publish', [ + '--provider' => 'Stancl\Tenancy\TenancyServiceProvider', + '--tag' => $tag, + ]); + } + + if ($files) { + // Show a warning if any of the files already exist + $unless = count(array_filter($files, fn ($file) => file_exists(base_path($file)))) !== 0; + } + + if (! $unless) { + if ($newLineBefore) { + $this->newLine(); + } + + $this->components->task($name, $task ?? fn () => null); + + if ($files) { + // Print out a clickable list of the added files + $this->components->bulletList(array_map(fn (string $file) => "[$file]", $files)); + } + + if ($newLineAfter) { + $this->newLine(); + } + } else { + $this->components->warn($warning); + } + } + /** If the user accepts, opens the GitHub project in the browser. */ public function askForSupport(): void { diff --git a/src/Commands/TenantList.php b/src/Commands/TenantList.php index 6e23b995..c008ba59 100644 --- a/src/Commands/TenantList.php +++ b/src/Commands/TenantList.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Stancl\Tenancy\Commands; use Illuminate\Console\Command; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Stancl\Tenancy\Contracts\Tenant; @@ -22,36 +23,27 @@ class TenantList extends Command foreach ($tenants as $tenant) { /** @var Model&Tenant $tenant */ - $this->components->twoColumnDetail($this->tenantCli($tenant), $this->domainsCli($tenant)); + $this->components->twoColumnDetail($this->tenantCLI($tenant), $this->domainsCLI($tenant->domains)); } + $this->newLine(); + return 0; } - /** - * Generate the visual cli output for the tenant name - * - * @param Model $tenant - * @return string - */ - protected function tenantCli(Model $tenant): string + /** Generate the visual CLI output for the tenant name. */ + protected function tenantCLI(Model&Tenant $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 + /** Generate the visual CLI output for the domain names. */ + protected function domainsCLI(?Collection $domains): ?string { - if (! $tenant->domains) { - + if (! $domains) { return null; } - return "{$tenant->domains->pluck('domain')->implode(' ; ')}"; + return "{$domains->pluck('domain')->implode(' / ')}"; } }