Skip to content

Commit

Permalink
fix: consumer middlewares work as expected [fixes #303] (#304)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander (SASh) Alexiev <[email protected]>
  • Loading branch information
sash and Alexander (SASh) Alexiev authored Aug 5, 2024
1 parent 0beaa3e commit 8bbfae0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Concerns/HandleConsumedMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait HandleConsumedMessage
{
private function handleConsumedMessage(ConsumerMessage $message, Handler|Closure $handler, ?MessageConsumer $consumer = null, array $middlewares = []): void
{
$middlewares = array_map($this->wrapMiddleware(...), $middlewares);
$middlewares = array_map(fn ($middleware) => $this->wrapMiddleware($middleware, $consumer), $middlewares);
$middlewares = array_reverse($middlewares);

foreach ($middlewares as $middleware) {
Expand Down
5 changes: 3 additions & 2 deletions src/Concerns/PrepareMiddlewares.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Junges\Kafka\Concerns;

use Junges\Kafka\Contracts\MessageConsumer;
use Junges\Kafka\Contracts\Middleware;

/** @internal */
trait PrepareMiddlewares
{
/** Wrap the message with a given middleware. */
private function wrapMiddleware(Middleware|string|callable $middleware): callable
private function wrapMiddleware(Middleware|string|callable $middleware, ?MessageConsumer $consumer = null): callable
{
$middleware = match(true) {
is_string($middleware) && is_subclass_of($middleware, Middleware::class) => new $middleware(),
Expand All @@ -17,6 +18,6 @@ private function wrapMiddleware(Middleware|string|callable $middleware): callabl
default => throw new \LogicException('Invalid middleware.')
};

return static fn (callable $handler) => static fn ($message) => $middleware($message, $handler);
return static fn (callable $handler) => static fn ($message) => $middleware($message, fn($message) => $handler($message, $consumer));
}
}

0 comments on commit 8bbfae0

Please sign in to comment.