diff --git a/src/Bootstrappers/QueueTenancyBootstrapper.php b/src/Bootstrappers/QueueTenancyBootstrapper.php index 0db24730..b8015d75 100644 --- a/src/Bootstrappers/QueueTenancyBootstrapper.php +++ b/src/Bootstrappers/QueueTenancyBootstrapper.php @@ -6,6 +6,7 @@ namespace Stancl\Tenancy\Bootstrappers; use Illuminate\Config\Repository; use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Contracts\Foundation\Application; use Illuminate\Queue\Events\JobProcessing; use Illuminate\Queue\QueueManager; use Illuminate\Support\Testing\Fakes\QueueFake; @@ -25,19 +26,27 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper /** @var Dispatcher */ protected $event; - public function __construct(Repository $config, QueueManager $queue, Dispatcher $event) + /** + * The normal constructor is only executed after tenancy is bootstrapped. + * However, we're registering a hook to initialize tenancy. Therefore, + * we need to register the hook at service provider execution time. + */ + public static function __constructStatic(Application $app) + { + static::setUpJobListener($app->make(Dispatcher::class)); + } + + public function __construct(Repository $config, QueueManager $queue) { $this->config = $config; $this->queue = $queue; - $this->event = $event; - $this->setUpJobListener(); $this->setUpPayloadGenerator(); } - protected function setUpJobListener() + protected static function setUpJobListener($dispatcher) { - $this->event->listen(JobProcessing::class, function ($event) { + $dispatcher->listen(JobProcessing::class, function ($event) { $tenantId = $event->job->payload()['tenant_id'] ?? null; // The job is not tenant-aware diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index c7d0a322..533d2b64 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -41,6 +41,10 @@ class TenancyServiceProvider extends ServiceProvider // Make sure bootstrappers are stateful (singletons). foreach ($this->app['config']['tenancy.bootstrappers'] ?? [] as $bootstrapper) { + if (method_exists($bootstrapper, '__constructStatic')) { + $bootstrapper::__constructStatic($this->app); + } + $this->app->singleton($bootstrapper); }