Skip to content

Commit 4e18f9b

Browse files
committed
Introduce QueueProviderInterface::getNames() method
1 parent 37141e6 commit 4e18f9b

14 files changed

+169
-14
lines changed

config/di.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,4 @@
5959
'__construct()' => ['middlewareDefinitions' => $params['yiisoft/queue']['middlewares-fail']],
6060
],
6161
MessageSerializerInterface::class => JsonMessageSerializer::class,
62-
RunCommand::class => [
63-
'__construct()' => [
64-
'queues' => array_keys($params['yiisoft/queue']['queues']),
65-
],
66-
],
67-
ListenAllCommand::class => [
68-
'__construct()' => [
69-
'queues' => array_keys($params['yiisoft/queue']['queues']),
70-
],
71-
],
7262
];

src/Command/ListenAllCommand.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ final class ListenAllCommand extends Command
2525
public function __construct(
2626
private readonly QueueProviderInterface $queueProvider,
2727
private readonly LoopInterface $loop,
28-
private readonly array $queues,
2928
) {
3029
parent::__construct();
3130
}
@@ -39,7 +38,7 @@ public function configure(): void
3938
'queue',
4039
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
4140
'Queue name list to connect to',
42-
$this->queues,
41+
$this->queueProvider->getNames(),
4342
)
4443
->addOption(
4544
'pause',

src/Command/RunCommand.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ final class RunCommand extends Command
2020
{
2121
public function __construct(
2222
private readonly QueueProviderInterface $queueProvider,
23-
private readonly array $queues,
2423
) {
2524
parent::__construct();
2625
}
@@ -31,7 +30,7 @@ public function configure(): void
3130
'queue',
3231
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
3332
'Queue name list to connect to.',
34-
$this->queues,
33+
$this->queueProvider->getNames(),
3534
)
3635
->addOption(
3736
'maximum',

src/Debug/QueueProviderInterfaceProxy.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@ public function has(string|BackedEnum $name): bool
2626
{
2727
return $this->queueProvider->has($name);
2828
}
29+
30+
public function getNames(): array
31+
{
32+
return $this->queueProvider->getNames();
33+
}
2934
}

src/Provider/AdapterFactoryQueueProvider.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Yiisoft\Queue\QueueInterface;
1414

1515
use function array_key_exists;
16+
use function array_keys;
1617
use function sprintf;
1718

1819
/**
@@ -30,6 +31,11 @@ final class AdapterFactoryQueueProvider implements QueueProviderInterface
3031

3132
private readonly StrictFactory $factory;
3233

34+
/**
35+
* @psalm-var list<string>
36+
*/
37+
private readonly array $names;
38+
3339
/**
3440
* @param QueueInterface $baseQueue Base queue for queues creation.
3541
* @param array $definitions Adapter definitions indexed by queue names.
@@ -45,6 +51,7 @@ public function __construct(
4551
?ContainerInterface $container = null,
4652
bool $validate = true,
4753
) {
54+
$this->names = array_keys($definitions);
4855
try {
4956
$this->factory = new StrictFactory($definitions, $container, $validate);
5057
} catch (InvalidConfigException $exception) {
@@ -70,6 +77,11 @@ public function has(string|BackedEnum $name): bool
7077
return $this->factory->has($name);
7178
}
7279

80+
public function getNames(): array
81+
{
82+
return $this->names;
83+
}
84+
7385
/**
7486
* @throws InvalidQueueConfigException
7587
*/

src/Provider/CompositeQueueProvider.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
use BackedEnum;
88
use Yiisoft\Queue\QueueInterface;
99

10+
use function array_merge;
11+
use function array_unique;
12+
use function array_values;
13+
1014
/**
1115
* Composite queue provider.
1216
*/
@@ -45,4 +49,13 @@ public function has(string|BackedEnum $name): bool
4549
}
4650
return false;
4751
}
52+
53+
public function getNames(): array
54+
{
55+
$names = [];
56+
foreach ($this->providers as $provider) {
57+
$names[] = $provider->getNames();
58+
}
59+
return array_values(array_unique(array_merge(...$names)));
60+
}
4861
}

src/Provider/PredefinedQueueProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Yiisoft\Queue\StringNormalizer;
1010

1111
use function array_key_exists;
12+
use function array_keys;
1213
use function get_debug_type;
1314
use function sprintf;
1415

@@ -62,4 +63,9 @@ public function has(string|BackedEnum $name): bool
6263
$name = StringNormalizer::normalize($name);
6364
return array_key_exists($name, $this->queues);
6465
}
66+
67+
public function getNames(): array
68+
{
69+
return array_keys($this->queues);
70+
}
6571
}

src/Provider/QueueFactoryProvider.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Yiisoft\Queue\StringNormalizer;
1313

1414
use function array_key_exists;
15+
use function array_keys;
1516
use function sprintf;
1617

1718
/**
@@ -29,6 +30,11 @@ final class QueueFactoryProvider implements QueueProviderInterface
2930

3031
private readonly StrictFactory $factory;
3132

33+
/**
34+
* @psalm-var list<string>
35+
*/
36+
private readonly array $names;
37+
3238
/**
3339
* @param array $definitions Queue definitions indexed by queue names.
3440
* @param ContainerInterface|null $container Container to use for dependencies resolving.
@@ -43,6 +49,7 @@ public function __construct(
4349
?ContainerInterface $container = null,
4450
bool $validate = true,
4551
) {
52+
$this->names = array_keys($definitions);
4653
try {
4754
$this->factory = new StrictFactory($definitions, $container, $validate);
4855
} catch (InvalidConfigException $exception) {
@@ -68,6 +75,11 @@ public function has(string|BackedEnum $name): bool
6875
return $this->factory->has($name);
6976
}
7077

78+
public function getNames(): array
79+
{
80+
return $this->names;
81+
}
82+
7183
/**
7284
* @throws InvalidQueueConfigException
7385
*/

src/Provider/QueueProviderInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,13 @@ public function get(string|BackedEnum $name): QueueInterface;
3535
* @return bool Whether the queue exists.
3636
*/
3737
public function has(string|BackedEnum $name): bool;
38+
39+
/**
40+
* Returns a list of queue names.
41+
*
42+
* @return string[] Queue names.
43+
*
44+
* @psalm-return list<string>
45+
*/
46+
public function getNames(): array;
3847
}

tests/Unit/Debug/QueueProviderInterfaceProxyTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,14 @@ public function testHas(): void
3333

3434
$this->assertTrue($factory->has('test'));
3535
}
36+
37+
public function testGetNames(): void
38+
{
39+
$queueFactory = $this->createMock(QueueProviderInterface::class);
40+
$queueFactory->expects($this->once())->method('getNames')->willReturn(['queue1', 'queue2']);
41+
$collector = new QueueCollector();
42+
$factory = new QueueProviderInterfaceProxy($queueFactory, $collector);
43+
44+
$this->assertSame(['queue1', 'queue2'], $factory->getNames());
45+
}
3646
}

0 commit comments

Comments
 (0)