diff --git a/src/Traits/HasATenantArgument.php b/src/Traits/HasATenantArgument.php new file mode 100644 index 00000000..7ad0c846 --- /dev/null +++ b/src/Traits/HasATenantArgument.php @@ -0,0 +1,29 @@ +find($this->argument('tenant'))]; + } + + public function __construct() + { + parent::__construct(); + + $this->specifyParameters(); + } +} diff --git a/src/Traits/HasATenantsOption.php b/src/Traits/HasATenantsOption.php index 4d6b247d..103a3b91 100644 --- a/src/Traits/HasATenantsOption.php +++ b/src/Traits/HasATenantsOption.php @@ -14,4 +14,16 @@ trait HasATenantsOption ['tenants', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, '', null], ], parent::getOptions()); } + + protected function getTenants(): array + { + return tenancy()->all($this->option('tenants'))->all(); + } + + public function __construct() + { + parent::__construct(); + + $this->specifyParameters(); + } } diff --git a/src/Traits/TenantAwareCommand.php b/src/Traits/TenantAwareCommand.php new file mode 100644 index 00000000..6cdb22e8 --- /dev/null +++ b/src/Traits/TenantAwareCommand.php @@ -0,0 +1,37 @@ +getTenants(); + + if (count($tenants) === 1) { + return $tenants[0]->run(function () { + return $this->laravel->call([$this, 'handle']); + }); + } + + foreach ($tenants as $tenant) { + $tenant->run(function () { + $this->laravel->call([$this, 'handle']); + }); + } + } + + /** + * Get an array of tenants for which the command should be executed. + * + * @return Tenant[] + */ + abstract protected function getTenants(): array; +}