From 1ffecfe8d53270b884532f41e5d3e28518cbecde Mon Sep 17 00:00:00 2001 From: tornike Date: Fri, 2 Aug 2024 14:59:49 +0400 Subject: [PATCH] make deferred service providers operation scoped --- src/ApplicationFactory.php | 9 ++++++++- src/Listeners/RegisterOperationServiceProviders.php | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/ApplicationFactory.php b/src/ApplicationFactory.php index e5398e642..b0ae446db 100644 --- a/src/ApplicationFactory.php +++ b/src/ApplicationFactory.php @@ -45,7 +45,14 @@ public function bootstrap(Application $app, array $initialInstances = []): Appli $app->bootstrapWith($this->getBootstrappers($app)); - $app->loadDeferredProviders(); + // We will register all but operation scoped deferred providers right away so + // they are resolved once, during boot. Remaining operation providers will + // be registered with classical approach, during the occasions of need. + foreach ($app->getDeferredServices() as $service => $provider) { + if (!in_array($provider, $app->make('config')->get('octane.op_service_providers', []))) { + $app->loadDeferredProvider($service); + } + } return $app; } diff --git a/src/Listeners/RegisterOperationServiceProviders.php b/src/Listeners/RegisterOperationServiceProviders.php index bbb865c8b..01c16a9c0 100644 --- a/src/Listeners/RegisterOperationServiceProviders.php +++ b/src/Listeners/RegisterOperationServiceProviders.php @@ -14,6 +14,8 @@ public function handle($event): void $resolved_providers = []; foreach ($event->sandbox->make('config')->get('octane.op_service_providers', []) as $provider) { + $provider = $event->sandbox->resolveProvider($provider); + if ($provider->isDeferred()) continue; $resolved_providers[] = $event->sandbox->register($provider, boot: false); }