From 8859c24b5d9099d75e87df83c91e3c259918e2ad Mon Sep 17 00:00:00 2001 From: Victor R Date: Wed, 16 Feb 2022 20:10:48 -0500 Subject: [PATCH] Add tenant dump command --- src/Commands/TenantDump.php | 77 +++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/Commands/TenantDump.php diff --git a/src/Commands/TenantDump.php b/src/Commands/TenantDump.php new file mode 100644 index 00000000..c10a16ab --- /dev/null +++ b/src/Commands/TenantDump.php @@ -0,0 +1,77 @@ +setName('tenants:dump'); + $this->specifyParameters(); + } + + /** + * Execute the console command. + * + * @param \Illuminate\Database\ConnectionResolverInterface $connections + * @param \Illuminate\Contracts\Events\Dispatcher $dispatcher + * @return int + * + * @throws \Throwable + */ + public function handle(ConnectionResolverInterface $connections, Dispatcher $dispatcher): int + { + $this->tenant()->run(fn() => parent::handle($connections, $dispatcher)); + + return Command::SUCCESS; + } + + /** + * Get tenant to use as a template for the schema dump. + * + * @return \Stancl\Tenancy\Contracts\Tenant + * + * @throws \Throwable + */ + public function tenant(): Tenant + { + $tenant = $this->option('tenant') + ?? tenant() + ?? tenancy()->query()->first() + ?? $this->ask('What tenant do you want to dump the schema for?'); + + if (! $tenant instanceof Tenant) { + $tenant = tenancy()->find($tenant); + } + + throw_if(! $tenant, 'Could not identify the tenant to use for dumping the schema.'); + + return $tenant; + } + + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions(): array + { + return array_merge([ + ['tenant', null, InputOption::VALUE_OPTIONAL, '', null], + ], parent::getOptions()); + } +}