Skip to content

Commit a303aa1

Browse files
committed
Fix order of filtered queues with wildcard
1 parent 40eb072 commit a303aa1

File tree

3 files changed

+36
-28
lines changed

3 files changed

+36
-28
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## [1.0.0-beta6] - 2025-01-23
9+
10+
### Fixed
11+
12+
- Order of filtered queues with wildcard
13+
814
## [1.0.0-beta5] - 2025-01-23
915

1016
### Added

src/QueueManager.php

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -68,40 +68,32 @@ public function filter(string ...$queue): static
6868
return $this;
6969
}
7070

71-
$queues = iterator_to_array($this->getQueues(), false);
72-
$queues = array_filter(
73-
$queues,
74-
function (QueueInterface $q) use ($queue): bool {
75-
foreach ($queue as $item) {
76-
if ($item == $q->getName()) {
77-
return true;
78-
}
79-
80-
// No wildcard
81-
if (false === str_contains($item, '*')) {
82-
continue;
83-
}
84-
85-
// Wildcard
86-
$regex = '/^' . str_replace('\*', '.*', preg_quote($item, '/')) . '$/';
87-
if (1 === preg_match($regex, $q->getName())) {
88-
return true;
89-
}
71+
$finalQueues = [];
72+
foreach ($queue as $name) {
73+
foreach ($this->getQueues() as $q) {
74+
if ($name == $q->getName()) {
75+
$finalQueues[$q->getName()] = $q;
76+
continue;
9077
}
9178

92-
return false;
93-
});
79+
// No wildcard
80+
if (false === str_contains($name, '*')) {
81+
continue;
82+
}
9483

95-
if (empty($queues)) {
96-
throw QueueManagerException::queueNotFound(...$queue);
84+
// Wildcard
85+
$regex = '/^' . str_replace('\*', '.*', preg_quote($name, '/')) . '$/';
86+
if (1 === preg_match($regex, $q->getName())) {
87+
$finalQueues[$q->getName()] = $q;
88+
}
89+
}
9790
}
9891

99-
usort(
100-
$queues,
101-
fn($a, $b) => array_search($a->getName(), $queue) <=> array_search($b->getName(), $queue)
102-
);
92+
if (empty($finalQueues)) {
93+
throw QueueManagerException::queueNotFound(...$queue);
94+
}
10395

104-
return new self(...$queues);
96+
return new self(...array_values($finalQueues));
10597
}
10698

10799
/**

tests/QueueManagerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ public function testFilterReturnsFilteredQueueManagerWithWildcard()
7070
$this->assertSame($this->secondaryQueueMock, $filteredQueues[0]);
7171
}
7272

73+
public function testFilterReturnsFilteredQueueManagerOrder()
74+
{
75+
$filteredQueueManager = $this->queueManager->filter('Secondary*', 'Primary*');
76+
77+
$filteredQueues = iterator_to_array($filteredQueueManager->getQueues(), false);
78+
$this->assertCount(2, $filteredQueues);
79+
$this->assertSame($this->secondaryQueueMock, $filteredQueues[0]);
80+
$this->assertSame($this->primaryQueueMock, $filteredQueues[1]);
81+
}
82+
7383
public function testFilterThrowsExceptionIfQueueNotFound()
7484
{
7585
$this->expectException(QueueManagerException::class);

0 commit comments

Comments
 (0)