Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass tenant to handle when using TenantAwareCommand trait #1216

Closed
gisostallenberg opened this issue Apr 26, 2024 · 2 comments
Closed

Pass tenant to handle when using TenantAwareCommand trait #1216

gisostallenberg opened this issue Apr 26, 2024 · 2 comments
Assignees
Labels
feature New feature or request

Comments

@gisostallenberg
Copy link

gisostallenberg commented Apr 26, 2024

Description

To get the current tenant in handle while using the TenantAwareCommand trait, we need to call

$tenant = tenant();

We could simply pass it using:

--- src/Concerns/TenantAwareCommand.php.orig	2024-04-14 10:12:18.000000000 +0200
+++ src/Concerns/TenantAwareCommand.php	2024-04-26 15:58:19.155120084 +0200
@@ -16,8 +16,8 @@
         $exitCode = 0;
 
         foreach ($tenants as $tenant) {
-            $result = (int) $tenant->run(function () {
-                return $this->laravel->call([$this, 'handle']);
+            $result = (int) $tenant->run(function (Tenant $tenant) {
+                return $this->laravel->call([$this, 'handle'], ['tenant' => $tenant]);
             });
 
             if ($result !== 0) {

An alternative would be to add a handleTenant method and call handle without argument to be 'more BC'

--- src/Concerns/TenantAwareCommand.php.orig	2024-04-14 10:12:18.000000000 +0200
+++ src/Concerns/TenantAwareCommand.php	2024-04-26 16:03:09.585756471 +0200
@@ -16,8 +16,8 @@
         $exitCode = 0;
 
         foreach ($tenants as $tenant) {
-            $result = (int) $tenant->run(function () {
-                return $this->laravel->call([$this, 'handle']);
+            $result = (int) $tenant->run(function (Tenant $tenant) {
+                return $this->laravel->call([$this, 'handleTenant']);
             });
 
             if ($result !== 0) {
@@ -28,6 +28,11 @@
         return $exitCode;
     }
 
+    protected function handleTenant(Tenant $tenant): int
+    {
+        return $this->handle();
+    }
+
     /**
      * Get an array of tenants for which the command should be executed.
      *

Would you be interested in a PR?

@gisostallenberg gisostallenberg added the feature New feature or request label Apr 26, 2024
@stancl
Copy link
Member

stancl commented Apr 26, 2024

Think this should allow it already?

// Make it possible to inject the current tenant by typehinting the Tenant contract.
$this->app->bind(Tenant::class, function ($app) {
return $app[Tenancy::class]->tenant;
});

@stancl
Copy link
Member

stancl commented Apr 26, 2024

I think handle() supports DI and that we have this Tenant binding (note that it's the contract, not your model) already, but haven't tested the combination.

@stancl stancl closed this as not planned Won't fix, can't repro, duplicate, stale May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants