Skip to content

Commit 7e141c5

Browse files
Remove withMiddlewares() and withMiddlewaresAdded() methods from QueueInterface (#316)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent ccc4368 commit 7e141c5

6 files changed

Lines changed: 11 additions & 90 deletions

File tree

docs/guide/en/middleware-pipelines.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,3 @@ See [Configuration with yiisoft/config](configuration-with-config.md) for exampl
152152
When configuring the component manually, you instantiate the middleware dispatchers and pass them to `Queue` / `Worker`.
153153

154154
See [Manual configuration](configuration-manual.md) for a full runnable example.
155-
156-
## Runtime overrides
157-
158-
You can override middleware stacks at runtime:
159-
160-
- `Queue::withMiddlewares(...)` replaces the whole push middleware stack for that queue instance.
161-
- `Queue::withMiddlewaresAdded(...)` appends middlewares to the existing stack.
162-
163-
These methods affect only the push pipeline of the `Queue` instance they are called on.

src/Debug/QueueDecorator.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,4 @@ public function getName(): string
5050
{
5151
return $this->queue->getName();
5252
}
53-
54-
public function withMiddlewares(mixed ...$middlewareDefinitions): self
55-
{
56-
return new self($this->queue->withMiddlewares(...$middlewareDefinitions), $this->collector);
57-
}
58-
59-
public function withMiddlewaresAdded(mixed ...$middlewareDefinitions): self
60-
{
61-
return new self($this->queue->withMiddlewaresAdded(...$middlewareDefinitions), $this->collector);
62-
}
6353
}

src/Queue.php

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,14 @@
2020

2121
final class Queue implements QueueInterface
2222
{
23-
/**
24-
* @var mixed[] Queue-specific middleware definitions.
25-
*/
26-
private array $middlewareDefinitions;
27-
2823
private string $name;
2924

3025
/**
31-
* @var PushMiddlewareDispatcher The dispatcher used for push messages, combining base dispatcher middleware with
32-
* queue-specific middleware.
26+
* @var PushMiddlewareDispatcher The dispatcher used for push messages, combining common middleware from
27+
* {@see PushMiddlewareConfig} with queue-specific middleware.
3328
*/
3429
private PushMiddlewareDispatcher $dispatcher;
3530

36-
/**
37-
* @var PushMiddlewareDispatcher The base dispatcher built from {@see PushMiddlewareConfig}.
38-
* Holds the common middleware applied to all queues.
39-
*/
40-
private PushMiddlewareDispatcher $baseDispatcher;
41-
4231
/**
4332
* @param WorkerInterface $worker The worker that processes messages.
4433
* @param LoopInterface $loop The loop for controlling message processing.
@@ -59,19 +48,16 @@ public function __construct(
5948
mixed ...$middlewareDefinitions,
6049
) {
6150
$this->name = StringNormalizer::normalize($name);
62-
$this->baseDispatcher = new PushMiddlewareDispatcher(
51+
$this->dispatcher = (new PushMiddlewareDispatcher(
6352
$middlewareConfig->middlewareFactory,
6453
$middlewareConfig->commonMiddlewareDefinitions,
6554
$this->createFinalPushHandler(),
66-
);
67-
$this->setMiddlewaresAndPrepareDispatcher($middlewareDefinitions);
55+
))->withMiddlewaresAdded($middlewareDefinitions);
6856
}
6957

7058
public function __clone()
7159
{
72-
$finalPushHandler = $this->createFinalPushHandler();
73-
$this->baseDispatcher = $this->baseDispatcher->withFinishHandler($finalPushHandler);
74-
$this->dispatcher = $this->dispatcher->withFinishHandler($finalPushHandler);
60+
$this->dispatcher = $this->dispatcher->withFinishHandler($this->createFinalPushHandler());
7561
}
7662

7763
public function getName(): string
@@ -164,29 +150,6 @@ public function status(string|int $id): MessageStatus
164150
return $this->adapter->status($id);
165151
}
166152

167-
public function withMiddlewares(mixed ...$middlewareDefinitions): self
168-
{
169-
$instance = clone $this;
170-
$instance->setMiddlewaresAndPrepareDispatcher($middlewareDefinitions);
171-
return $instance;
172-
}
173-
174-
public function withMiddlewaresAdded(mixed ...$middlewareDefinitions): self
175-
{
176-
$instance = clone $this;
177-
$instance->setMiddlewaresAndPrepareDispatcher([...array_values($instance->middlewareDefinitions), ...array_values($middlewareDefinitions)]);
178-
return $instance;
179-
}
180-
181-
/**
182-
* @param mixed[] $middlewareDefinitions
183-
*/
184-
private function setMiddlewaresAndPrepareDispatcher(array $middlewareDefinitions): void
185-
{
186-
$this->middlewareDefinitions = $middlewareDefinitions;
187-
$this->dispatcher = $this->baseDispatcher->withMiddlewaresAdded($middlewareDefinitions);
188-
}
189-
190153
private function handle(MessageInterface $message): bool
191154
{
192155
$this->worker->process($message, $this);

src/QueueInterface.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,4 @@ public function status(string|int $id): MessageStatus;
4040
* Returns the logical name of the queue.
4141
*/
4242
public function getName(): string;
43-
44-
/**
45-
* Creates a new instance with the specified middlewares. All the existing middlewares are replaced.
46-
*
47-
* @param mixed ...$middlewareDefinitions The middleware definitions.
48-
*/
49-
public function withMiddlewares(mixed ...$middlewareDefinitions): self;
50-
51-
/**
52-
* Creates a new instance with the specified middlewares added after the existing ones.
53-
*
54-
* @param mixed ...$middlewareDefinitions The middleware definitions.
55-
*/
56-
public function withMiddlewaresAdded(mixed ...$middlewareDefinitions): self;
5743
}

stubs/StubQueue.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,4 @@ public function getName(): string
3838
{
3939
return $this->name;
4040
}
41-
42-
public function withMiddlewares(mixed ...$middlewareDefinitions): self
43-
{
44-
return $this;
45-
}
46-
47-
public function withMiddlewaresAdded(mixed ...$middlewareDefinitions): self
48-
{
49-
return $this;
50-
}
5141
}

tests/Integration/MiddlewareTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ public function testFullStackPush(): void
6464
$this->createMock(LoopInterface::class),
6565
$this->createMock(LoggerInterface::class),
6666
$pushMiddlewareConfig,
67-
name: 'test',
67+
null,
68+
'test',
69+
new TestMiddleware('channel 1'),
70+
new TestMiddleware('channel 2'),
71+
new TestMiddleware('channel 3'),
72+
new TestMiddleware('channel 4'),
6873
);
69-
$queue = $queue
70-
->withMiddlewares(new TestMiddleware('Won\'t be executed'))
71-
->withMiddlewares(new TestMiddleware('channel 1'), new TestMiddleware('channel 2'))
72-
->withMiddlewaresAdded(new TestMiddleware('channel 3'), new TestMiddleware('channel 4'));
7374

7475
$message = new GenericMessage('test', ['initial']);
7576
$messagePushed = $queue->push($message);

0 commit comments

Comments
 (0)