Skip to content

Commit ae5867a

Browse files
committed
Improve the channels.md guide
1 parent 23e7997 commit ae5867a

1 file changed

Lines changed: 86 additions & 2 deletions

File tree

docs/guide/en/channels.md

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,82 @@ $emailsQueue = $provider->get('emails');
4949
$emailsQueue->push(new \Yiisoft\Queue\Message\Message('send-email', ['to' => 'user@example.com']));
5050
```
5151

52+
You can also check if a channel exists before trying to get it:
53+
54+
```php
55+
if ($provider->has('emails')) {
56+
$emailsQueue = $provider->get('emails');
57+
}
58+
```
59+
60+
`QueueProviderInterface` accepts both strings and `BackedEnum` values (they are normalized to a string channel name).
61+
62+
`QueueProviderInterface::get()` may throw:
63+
64+
- `Yiisoft\Queue\Provider\ChannelNotFoundException`
65+
- `Yiisoft\Queue\Provider\InvalidQueueConfigException`
66+
- `Yiisoft\Queue\Provider\QueueProviderException`
67+
68+
## Providers
69+
70+
`QueueProviderInterface` is the component responsible for returning a `QueueInterface` instance bound to a particular channel.
71+
72+
Out of the box, this package provides three implementations:
73+
74+
- `Yiisoft\Queue\Provider\AdapterFactoryQueueProvider`
75+
- `Yiisoft\Queue\Provider\PrototypeQueueProvider`
76+
- `Yiisoft\Queue\Provider\CompositeQueueProvider`
77+
78+
### `AdapterFactoryQueueProvider`
79+
80+
This provider creates channel-specific `QueueInterface` instances based on adapter definitions.
81+
82+
It uses [`yiisoft/factory`](https://github.com/yiisoft/factory) to resolve adapter definitions.
83+
84+
This approach is recommended when you want:
85+
86+
- Separate configuration per channel.
87+
- Stronger validation (unknown channels are not silently accepted).
88+
89+
### `PrototypeQueueProvider`
90+
91+
This provider always returns a queue by taking a base queue + base adapter and only changing the channel name.
92+
93+
This can be useful when all channels use the same adapter and only differ by channel name.
94+
95+
This strategy is not recommended as it does not give you any protection against typos and mistakes in channel names.
96+
97+
Example:
98+
99+
```php
100+
use Yiisoft\Queue\Provider\PrototypeQueueProvider;
101+
102+
$provider = new PrototypeQueueProvider($queue, $adapter);
103+
104+
$queueForEmails = $provider->get('emails');
105+
$queueForCritical = $provider->get('critical');
106+
```
107+
108+
### `CompositeQueueProvider`
109+
110+
This provider combines multiple providers into one.
111+
112+
It tries to resolve a channel by calling `has()`/`get()` on each provider in the order they are passed to the constructor.
113+
The first provider that reports it has the channel wins.
114+
115+
Example:
116+
117+
```php
118+
use Yiisoft\Queue\Provider\CompositeQueueProvider;
119+
120+
$provider = new CompositeQueueProvider(
121+
$providerA,
122+
$providerB,
123+
);
124+
125+
$queue = $provider->get('emails');
126+
```
127+
52128
## Configuration with yiisoft/config
53129

54130
When using [yiisoft/config](https://github.com/yiisoft/config), channel configuration is stored in params under `yiisoft/queue.channels`.
@@ -101,10 +177,15 @@ For multiple channels without `yiisoft/config`, you can create a provider manual
101177

102178
```php
103179
use Yiisoft\Queue\Provider\AdapterFactoryQueueProvider;
180+
use Yiisoft\Queue\Adapter\SynchronousAdapter;
104181

105182
$definitions = [
106-
'channel1' => new \Yiisoft\Queue\Adapter\SynchronousAdapter($worker, $queue),
107-
'channel2' => static fn (\Yiisoft\Queue\Adapter\SynchronousAdapter $adapter) => $adapter->withChannel('channel2'),
183+
'channel1' => new SynchronousAdapter($worker, $queue),
184+
'channel2' => static fn (SynchronousAdapter $adapter) => $adapter->withChannel('channel2'),
185+
'channel3' => [
186+
'class' => SynchronousAdapter::class,
187+
'__constructor' => ['channel' => 'channel3'],
188+
],
108189
];
109190

110191
$provider = new AdapterFactoryQueueProvider(
@@ -115,4 +196,7 @@ $provider = new AdapterFactoryQueueProvider(
115196

116197
$queueForChannel1 = $provider->get('channel1');
117198
$queueForChannel2 = $provider->get('channel2');
199+
$queueForChannel3 = $provider->get('channel3');
118200
```
201+
202+
For more information about the definition formats available, see the [`yiisoft/factory` documentation](https://github.com/yiisoft/factory).

0 commit comments

Comments
 (0)