From acb45b5e62345a340de51db9093eecc1c86cd2ed Mon Sep 17 00:00:00 2001 From: jradtilbrook Date: Wed, 26 Nov 2025 13:19:04 +0800 Subject: [PATCH 1/3] Pass Laravel context through with schedule tasks --- src/Illuminate/Console/Scheduling/Event.php | 5 ++++- src/Illuminate/Log/Context/ContextServiceProvider.php | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Console/Scheduling/Event.php b/src/Illuminate/Console/Scheduling/Event.php index 7cda4f054413..f458e3109202 100644 --- a/src/Illuminate/Console/Scheduling/Event.php +++ b/src/Illuminate/Console/Scheduling/Event.php @@ -11,6 +11,7 @@ use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Contracts\Mail\Mailer; +use Illuminate\Log\Context\Repository; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Date; use Illuminate\Support\Stringable; @@ -197,8 +198,10 @@ protected function start($container) */ protected function execute($container) { + $context = escapeshellarg(json_encode($container[Repository::class]->dehydrate())); + return Process::fromShellCommandline( - $this->buildCommand(), base_path(), null, null, null + $this->buildCommand(), base_path(), ['__LARAVEL_CONTEXT' => $context], null, null )->run( laravel_cloud() ? fn ($type, $line) => fwrite($type === 'out' ? STDOUT : STDERR, $line) diff --git a/src/Illuminate/Log/Context/ContextServiceProvider.php b/src/Illuminate/Log/Context/ContextServiceProvider.php index 7167518a1b19..c80dced55e3c 100644 --- a/src/Illuminate/Log/Context/ContextServiceProvider.php +++ b/src/Illuminate/Log/Context/ContextServiceProvider.php @@ -5,6 +5,7 @@ use Illuminate\Contracts\Log\ContextLogProcessor as ContextLogProcessorContract; use Illuminate\Queue\Events\JobProcessing; use Illuminate\Queue\Queue; +use Illuminate\Support\Env; use Illuminate\Support\Facades\Context; use Illuminate\Support\ServiceProvider; @@ -29,6 +30,10 @@ public function register() */ public function boot() { + $env = Env::get('__LARAVEL_CONTEXT', ''); + /** @phpstan-ignore staticMethod.notFound */ + Context::hydrate(json_decode($env, true)); + Queue::createPayloadUsing(function ($connection, $queue, $payload) { /** @phpstan-ignore staticMethod.notFound */ $context = Context::dehydrate(); From 7f10d82646d2521f1fdcc2343e05ab0bcc49f6db Mon Sep 17 00:00:00 2001 From: jradtilbrook Date: Wed, 26 Nov 2025 14:03:21 +0800 Subject: [PATCH 2/3] Hydrate only in console when resolving context --- src/Illuminate/Console/Scheduling/Event.php | 2 +- .../Log/Context/ContextServiceProvider.php | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Console/Scheduling/Event.php b/src/Illuminate/Console/Scheduling/Event.php index f458e3109202..a8bef27de9d1 100644 --- a/src/Illuminate/Console/Scheduling/Event.php +++ b/src/Illuminate/Console/Scheduling/Event.php @@ -198,7 +198,7 @@ protected function start($container) */ protected function execute($container) { - $context = escapeshellarg(json_encode($container[Repository::class]->dehydrate())); + $context = json_encode($container[Repository::class]->dehydrate()); return Process::fromShellCommandline( $this->buildCommand(), base_path(), ['__LARAVEL_CONTEXT' => $context], null, null diff --git a/src/Illuminate/Log/Context/ContextServiceProvider.php b/src/Illuminate/Log/Context/ContextServiceProvider.php index c80dced55e3c..e1f6a901d842 100644 --- a/src/Illuminate/Log/Context/ContextServiceProvider.php +++ b/src/Illuminate/Log/Context/ContextServiceProvider.php @@ -20,6 +20,15 @@ public function register() { $this->app->scoped(Repository::class); + if ($this->app->runningInConsole()) { + $this->app->resolving(Repository::class, function (Repository $repository) { + $context = Env::get('__LARAVEL_CONTEXT'); + if ($context && $context = json_decode($context, associative: true)) { + $repository->hydrate($context); + } + }); + } + $this->app->bind(ContextLogProcessorContract::class, fn () => new ContextLogProcessor()); } @@ -30,10 +39,6 @@ public function register() */ public function boot() { - $env = Env::get('__LARAVEL_CONTEXT', ''); - /** @phpstan-ignore staticMethod.notFound */ - Context::hydrate(json_decode($env, true)); - Queue::createPayloadUsing(function ($connection, $queue, $payload) { /** @phpstan-ignore staticMethod.notFound */ $context = Context::dehydrate(); From c51e08c02bdd6e3dd8e788da6f5dd3d9813e9918 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 26 Nov 2025 08:28:48 -0600 Subject: [PATCH 3/3] Update ContextServiceProvider.php --- src/Illuminate/Log/Context/ContextServiceProvider.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Illuminate/Log/Context/ContextServiceProvider.php b/src/Illuminate/Log/Context/ContextServiceProvider.php index e1f6a901d842..dd748321d17f 100644 --- a/src/Illuminate/Log/Context/ContextServiceProvider.php +++ b/src/Illuminate/Log/Context/ContextServiceProvider.php @@ -23,6 +23,7 @@ public function register() if ($this->app->runningInConsole()) { $this->app->resolving(Repository::class, function (Repository $repository) { $context = Env::get('__LARAVEL_CONTEXT'); + if ($context && $context = json_decode($context, associative: true)) { $repository->hydrate($context); }