From f34986b29ff4954f0c94a9a4568f049251c2db28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 10 Nov 2019 12:32:26 +0100 Subject: [PATCH] [2.2.0] Tenant aware commands (#221) * wip * Apply fixes from StyleCI * Return value when a single tenant is passed * array map -> foreach * bug fixes --- src/Traits/HasATenantArgument.php | 29 ++++++++++++++++++++++++ src/Traits/HasATenantsOption.php | 12 ++++++++++ src/Traits/TenantAwareCommand.php | 37 +++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 src/Traits/HasATenantArgument.php create mode 100644 src/Traits/TenantAwareCommand.php 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; +}