Skip to content

Commit 5999193

Browse files
authored
Introduce QueueProviderInterface::getNames() method (#265)
1 parent 37141e6 commit 5999193

16 files changed

+193
-44
lines changed

config/di.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use Yiisoft\Queue\Cli\LoopInterface;
77
use Yiisoft\Queue\Cli\SignalLoop;
88
use Yiisoft\Queue\Cli\SimpleLoop;
9-
use Yiisoft\Queue\Command\ListenAllCommand;
10-
use Yiisoft\Queue\Command\RunCommand;
119
use Yiisoft\Queue\Message\JsonMessageSerializer;
1210
use Yiisoft\Queue\Message\MessageSerializerInterface;
1311
use Yiisoft\Queue\Middleware\Consume\ConsumeMiddlewareDispatcher;
@@ -59,14 +57,4 @@
5957
'__construct()' => ['middlewareDefinitions' => $params['yiisoft/queue']['middlewares-fail']],
6058
],
6159
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-
],
7260
];

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/Command/ListenAllCommandTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Symfony\Component\Console\Output\OutputInterface;
1010
use Yiisoft\Queue\Cli\LoopInterface;
1111
use Yiisoft\Queue\Command\ListenAllCommand;
12-
use Yiisoft\Queue\Provider\QueueProviderInterface;
12+
use Yiisoft\Queue\Provider\PredefinedQueueProvider;
1313
use Yiisoft\Queue\QueueInterface;
1414

1515
final class ListenAllCommandTest extends TestCase
@@ -21,16 +21,17 @@ public function testExecute(): void
2121
$queue2 = $this->createMock(QueueInterface::class);
2222
$queue2->expects($this->once())->method('run');
2323

24-
$queueFactory = $this->createMock(QueueProviderInterface::class);
25-
$queueFactory->method('get')->willReturn($queue1, $queue2);
24+
$queueFactory = new PredefinedQueueProvider([
25+
'queue1' => $queue1,
26+
'queue2' => $queue2,
27+
]);
2628

2729
$loop = $this->createMock(LoopInterface::class);
2830
$loop->method('canContinue')->willReturn(true, false);
2931

3032
$command = new ListenAllCommand(
3133
$queueFactory,
3234
$loop,
33-
['channel1', 'channel2'],
3435
);
3536
$input = new ArrayInput([], $command->getNativeDefinition());
3637
$input->setOption('pause', 0);

0 commit comments

Comments
 (0)