From 140248749f461b87426773513306eb4ba86d1303 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 11 Jan 2024 13:46:55 -0600 Subject: [PATCH 1/4] flush context on all log channels, just not default --- src/Illuminate/Log/LogManager.php | 16 ++++++++++++++++ src/Illuminate/Queue/QueueServiceProvider.php | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Log/LogManager.php b/src/Illuminate/Log/LogManager.php index 60498f4ef06d..15df806d55e6 100644 --- a/src/Illuminate/Log/LogManager.php +++ b/src/Illuminate/Log/LogManager.php @@ -501,6 +501,22 @@ public function sharedContext() return $this->sharedContext; } + /** + * Flush the log context on all currently resolved channels. + * + * @return $this + */ + public function withoutContext() + { + foreach ($this->channels as $channel) { + if (method_exists($channel, 'withoutContext')) { + $channel->withoutContext(); + } + } + + return $this; + } + /** * Flush the shared context. * diff --git a/src/Illuminate/Queue/QueueServiceProvider.php b/src/Illuminate/Queue/QueueServiceProvider.php index 2f94d28a13ce..2bcd16c2ff1f 100755 --- a/src/Illuminate/Queue/QueueServiceProvider.php +++ b/src/Illuminate/Queue/QueueServiceProvider.php @@ -198,7 +198,7 @@ protected function registerWorker() }; $resetScope = function () use ($app) { - if (method_exists($app['log']->driver(), 'withoutContext')) { + if (method_exists($app['log'], 'withoutContext')) { $app['log']->withoutContext(); } From e75374205ffc4379a1398df713649a368002e48f Mon Sep 17 00:00:00 2001 From: taylorotwell Date: Thu, 11 Jan 2024 19:47:33 +0000 Subject: [PATCH 2/4] Update facade docblocks --- src/Illuminate/Support/Facades/Log.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Support/Facades/Log.php b/src/Illuminate/Support/Facades/Log.php index 1fb2cfca082b..37e03961ebd5 100755 --- a/src/Illuminate/Support/Facades/Log.php +++ b/src/Illuminate/Support/Facades/Log.php @@ -9,6 +9,7 @@ * @method static \Psr\Log\LoggerInterface driver(string|null $driver = null) * @method static \Illuminate\Log\LogManager shareContext(array $context) * @method static array sharedContext() + * @method static \Illuminate\Log\LogManager withoutContext() * @method static \Illuminate\Log\LogManager flushSharedContext() * @method static string|null getDefaultDriver() * @method static void setDefaultDriver(string $name) @@ -26,7 +27,6 @@ * @method static void log(mixed $level, string $message, array $context = []) * @method static void write(string $level, \Illuminate\Contracts\Support\Arrayable|\Illuminate\Contracts\Support\Jsonable|\Illuminate\Support\Stringable|array|string $message, array $context = []) * @method static \Illuminate\Log\Logger withContext(array $context = []) - * @method static \Illuminate\Log\Logger withoutContext() * @method static void listen(\Closure $callback) * @method static \Psr\Log\LoggerInterface getLogger() * @method static \Illuminate\Contracts\Events\Dispatcher getEventDispatcher() From 8bb98e39b6dea70bc0a2e8fea51eb4ed55f6ef8b Mon Sep 17 00:00:00 2001 From: Frankie Jarrett Date: Thu, 11 Jan 2024 14:33:23 -0600 Subject: [PATCH 3/4] dispatchIf() and dispatchUnless() for batches (#49639) --- src/Illuminate/Bus/PendingBatch.php | 22 +++++++ tests/Bus/BusPendingBatchTest.php | 98 +++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/src/Illuminate/Bus/PendingBatch.php b/src/Illuminate/Bus/PendingBatch.php index 60ff3884c8b8..b9622f8cc064 100644 --- a/src/Illuminate/Bus/PendingBatch.php +++ b/src/Illuminate/Bus/PendingBatch.php @@ -344,4 +344,26 @@ protected function dispatchExistingBatch($batch) new BatchDispatched($batch) ); } + + /** + * Dispatch the batch if the given truth test passes. + * + * @param bool|\Closure $boolean + * @return \Illuminate\Bus\Batch|null + */ + public function dispatchIf($boolean) + { + return value($boolean) ? $this->dispatch() : null; + } + + /** + * Dispatch the batch unless the given truth test passes. + * + * @param bool|\Closure $boolean + * @return \Illuminate\Bus\Batch|null + */ + public function dispatchUnless($boolean) + { + return ! value($boolean) ? $this->dispatch() : null; + } } diff --git a/tests/Bus/BusPendingBatchTest.php b/tests/Bus/BusPendingBatchTest.php index 7cd5f7e3a80e..6a6e9186e686 100644 --- a/tests/Bus/BusPendingBatchTest.php +++ b/tests/Bus/BusPendingBatchTest.php @@ -88,4 +88,102 @@ public function test_batch_is_deleted_from_storage_if_exception_thrown_during_ba $pendingBatch->dispatch(); } + + public function test_batch_is_dispatched_when_dispatchif_is_true() + { + $container = new Container; + + $eventDispatcher = m::mock(Dispatcher::class); + $eventDispatcher->shouldReceive('dispatch')->once(); + $container->instance(Dispatcher::class, $eventDispatcher); + + $job = new class + { + use Batchable; + }; + + $pendingBatch = new PendingBatch($container, new Collection([$job])); + + $repository = m::mock(BatchRepository::class); + $repository->shouldReceive('store')->once()->andReturn($batch = m::mock(stdClass::class)); + $batch->shouldReceive('add')->once()->andReturn($batch = m::mock(Batch::class)); + + $container->instance(BatchRepository::class, $repository); + + $result = $pendingBatch->dispatchIf(true); + + $this->assertInstanceOf(Batch::class, $result); + } + + public function test_batch_is_not_dispatched_when_dispatchif_is_false() + { + $container = new Container; + + $eventDispatcher = m::mock(Dispatcher::class); + $eventDispatcher->shouldNotReceive('dispatch'); + $container->instance(Dispatcher::class, $eventDispatcher); + + $job = new class + { + use Batchable; + }; + + $pendingBatch = new PendingBatch($container, new Collection([$job])); + + $repository = m::mock(BatchRepository::class); + $container->instance(BatchRepository::class, $repository); + + $result = $pendingBatch->dispatchIf(false); + + $this->assertNull($result); + } + + public function test_batch_is_dispatched_when_dispatchunless_is_false() + { + $container = new Container; + + $eventDispatcher = m::mock(Dispatcher::class); + $eventDispatcher->shouldReceive('dispatch')->once(); + $container->instance(Dispatcher::class, $eventDispatcher); + + $job = new class + { + use Batchable; + }; + + $pendingBatch = new PendingBatch($container, new Collection([$job])); + + $repository = m::mock(BatchRepository::class); + $repository->shouldReceive('store')->once()->andReturn($batch = m::mock(stdClass::class)); + $batch->shouldReceive('add')->once()->andReturn($batch = m::mock(Batch::class)); + + $container->instance(BatchRepository::class, $repository); + + $result = $pendingBatch->dispatchUnless(false); + + $this->assertInstanceOf(Batch::class, $result); + } + + public function test_batch_is_not_dispatched_when_dispatchunless_is_true() + { + $container = new Container; + + $eventDispatcher = m::mock(Dispatcher::class); + $eventDispatcher->shouldNotReceive('dispatch'); + $container->instance(Dispatcher::class, $eventDispatcher); + + $job = new class + { + use Batchable; + }; + + $pendingBatch = new PendingBatch($container, new Collection([$job])); + + $repository = m::mock(BatchRepository::class); + $container->instance(BatchRepository::class, $repository); + + $result = $pendingBatch->dispatchUnless(true); + + $this->assertNull($result); + } } From dbce9d05c940a4b3e669709d1b7f8792a48fba62 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 11 Jan 2024 16:12:00 -0600 Subject: [PATCH 4/4] flush shared context --- src/Illuminate/Queue/QueueServiceProvider.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Illuminate/Queue/QueueServiceProvider.php b/src/Illuminate/Queue/QueueServiceProvider.php index 2bcd16c2ff1f..478352ae7e76 100755 --- a/src/Illuminate/Queue/QueueServiceProvider.php +++ b/src/Illuminate/Queue/QueueServiceProvider.php @@ -198,6 +198,8 @@ protected function registerWorker() }; $resetScope = function () use ($app) { + $app['log']->flushSharedContext(); + if (method_exists($app['log'], 'withoutContext')) { $app['log']->withoutContext(); }