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

improve Install & TenantList commands

This commit is contained in:
Samuel Štancl 2022-10-18 18:37:41 +02:00
parent 64d1daf93a
commit 7b8ab13f3d
3 changed files with 108 additions and 67 deletions

View file

@ -2,16 +2,14 @@
declare(strict_types=1); declare(strict_types=1);
use Stancl\Tenancy\Database\Models\Domain;
use Stancl\Tenancy\Database\Models\Tenant;
use Stancl\Tenancy\Middleware; use Stancl\Tenancy\Middleware;
use Stancl\Tenancy\Resolvers; use Stancl\Tenancy\Resolvers;
return [ return [
'tenant_model' => Tenant::class, 'tenant_model' => Stancl\Tenancy\Database\Models\Tenant::class,
'id_generator' => Stancl\Tenancy\UUIDGenerator::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. * The list of domains hosting your central app.

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Commands; namespace Stancl\Tenancy\Commands;
use Closure;
use Illuminate\Console\Command; use Illuminate\Console\Command;
class Install extends Command class Install extends Command
@ -14,54 +15,42 @@ class Install extends Command
public function handle(): int 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->step(
$this->callSilent('vendor:publish', [ name: 'Publishing routes [routes/tenant.php]',
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider', tag: 'routes',
'--tag' => 'config', 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->step(
$this->components->task('Publishing routes [routes/tenant.php]', function () { name: 'Publishing migrations',
$this->callSilent('vendor:publish', [ tag: 'migrations',
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider', files: [
'--tag' => 'routes', 'database/migrations/2019_09_15_000010_create_tenants_table.php',
]); 'database/migrations/2019_09_15_000020_create_domains_table.php',
}); ],
$this->newLine(); warning: 'Migrations already exist',
} else { );
$this->components->warn('File [routes/tenant.php] already exists.');
}
$this->components->task('Publishing service provider [app/Providers/TenancyServiceProvider.php]', function () { $this->step(
$this->callSilent('vendor:publish', [ name: 'Creating [database/migrations/tenant] folder',
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider', task: fn () => mkdir(database_path('migrations/tenant')),
'--tag' => 'providers', unless: is_dir(database_path('migrations/tenant')),
]); warning: 'Folder [database/migrations/tenant] already exists.',
}); newLineAfter: true,
);
$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->components->info('✨️ Tenancy for Laravel successfully installed.'); $this->components->info('✨️ Tenancy for Laravel successfully installed.');
@ -70,6 +59,68 @@ class Install extends Command
return 0; 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. */ /** If the user accepts, opens the GitHub project in the browser. */
public function askForSupport(): void public function askForSupport(): void
{ {

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Commands; namespace Stancl\Tenancy\Commands;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Contracts\Tenant;
@ -22,36 +23,27 @@ class TenantList extends Command
foreach ($tenants as $tenant) { foreach ($tenants as $tenant) {
/** @var Model&Tenant $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; return 0;
} }
/** /** Generate the visual CLI output for the tenant name. */
* Generate the visual cli output for the tenant name protected function tenantCLI(Model&Tenant $tenant): string
*
* @param Model $tenant
* @return string
*/
protected function tenantCli(Model $tenant): string
{ {
return "<fg=yellow>{$tenant->getTenantKeyName()}: {$tenant->getTenantKey()}</>"; return "<fg=yellow>{$tenant->getTenantKeyName()}: {$tenant->getTenantKey()}</>";
} }
/** /** Generate the visual CLI output for the domain names. */
* Generate the visual cli output for the domain names protected function domainsCLI(?Collection $domains): ?string
*
* @param Model $tenant
* @return string|null
*/
protected function domainsCli(Model $tenant): ?string
{ {
if (! $tenant->domains) { if (! $domains) {
return null; return null;
} }
return "<fg=blue;options=bold>{$tenant->domains->pluck('domain')->implode(' ; ')}</>"; return "<fg=blue;options=bold>{$domains->pluck('domain')->implode(' / ')}</>";
} }
} }