You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+71-36Lines changed: 71 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -42,76 +42,111 @@ See the [adapter list](docs/guide/en/adapter-list.md) and follow the adapter-spe
42
42
> In this mode messages are processed immediately in the same process, so it won't provide true
43
43
> async execution, but the code stays the same when you switch to a real adapter.
44
44
45
-
### 2. Configure the queue
45
+
### 2. Prepare a message and handler
46
46
47
-
#### Configuration with [yiisoft/config](https://github.com/yiisoft/config)
48
-
49
-
**If you use [yiisoft/app](https://github.com/yiisoft/app) or [yiisoft/app-api](https://github.com/yiisoft/app-api)**
50
-
51
-
Add queue configuration to your application `$params` config. In [yiisoft/app](https://github.com/yiisoft/app)/[yiisoft/app-api](https://github.com/yiisoft/app-api) templates it's typically the `config/params.php` file.
52
-
_If your project structure differs, put it into any params config file that is loaded by [yiisoft/config](https://github.com/yiisoft/config)._
53
-
54
-
Minimal configuration example:
47
+
Define a message class for the work to be done — a simple value object with typed properties:
55
48
56
49
```php
57
-
return [
58
-
'yiisoft/queue' => [
59
-
'handlers' => [
60
-
'message-type' => [FooHandler::class, 'handle'],
61
-
],
62
-
],
63
-
];
64
-
```
50
+
use Yiisoft\Queue\Message\Message;
65
51
66
-
[Advanced configuration with `yiisoft/config`](docs/guide/en/configuration-with-config.md)
52
+
final class DownloadFileMessage extends Message
53
+
{
54
+
public const TYPE = 'download-file';
67
55
68
-
#### Manual configuration
56
+
public function __construct(
57
+
public readonly string $url,
58
+
public readonly string $destinationPath,
59
+
) {}
69
60
70
-
For setting up all classes manually, see the [Manual configuration](docs/guide/en/configuration-manual.md) guide.
61
+
public static function fromData(string $type, mixed $data): static
62
+
{
63
+
if ($type !== self::TYPE) {
64
+
throw new \InvalidArgumentException("Expected type \"" . self::TYPE . "\", got \"$type\".");
65
+
}
66
+
if (!is_array($data)
67
+
|| !is_string($data['url'] ?? null)
68
+
|| !is_string($data['destinationPath'] ?? null)
69
+
) {
70
+
throw new \InvalidArgumentException('Invalid data for ' . self::class . '.');
71
+
}
72
+
return new self($data['url'], $data['destinationPath']);
73
+
}
71
74
72
-
### 3. Prepare a handler
75
+
public function getType(): string
76
+
{
77
+
return self::TYPE;
78
+
}
73
79
74
-
You need to create a handler class that will process the queue messages. The most simple way is to implement the `MessageHandlerInterface`. Let's create an example for remote file processing:
#### Configuration with [yiisoft/config](https://github.com/yiisoft/config)
112
+
113
+
**If you use [yiisoft/app](https://github.com/yiisoft/app) or [yiisoft/app-api](https://github.com/yiisoft/app-api)**
114
+
115
+
Add queue configuration to your application `$params` config. In [yiisoft/app](https://github.com/yiisoft/app)/[yiisoft/app-api](https://github.com/yiisoft/app-api) templates it's typically the `config/params.php` file.
116
+
_If your project structure differs, put it into any params config file that is loaded by [yiisoft/config](https://github.com/yiisoft/config)._
99
117
100
-
To send a message to the queue, you need to get the queue instance and call the `push()` method. Typically, with Yii Framework you'll get a `Queue` instance as a dependency of a service.
new SendEmailMessage(…) →→→ Worker resolves handler → handles
18
+
(payload only) (logic only)
19
19
```
20
20
21
21
The producer only needs to know the message type and its data. It does not need to know anything about how the message will be processed, or even in which application.
@@ -30,21 +30,61 @@ This means the producer and consumer can be:
30
30
31
31
## Message: payload only
32
32
33
-
A message carries just enough data to perform the work:
33
+
A message carries just enough data to perform the work. Defining a dedicated class for each message type makes your code
34
+
self-documenting and type-safe:
34
35
35
36
```php
36
-
new \Yiisoft\Queue\Message\GenericMessage('send-email', [
37
-
'to' => 'user@example.com',
38
-
'subject' => 'Welcome',
39
-
]);
37
+
use Yiisoft\Queue\Message\Message;
38
+
39
+
final class SendEmailMessage extends Message
40
+
{
41
+
public const TYPE = 'send-email';
42
+
43
+
public function __construct(
44
+
public readonly string $to,
45
+
public readonly string $subject,
46
+
public readonly string $body,
47
+
) {}
48
+
49
+
public static function fromData(string $type, mixed $data): static
50
+
{
51
+
if ($type !== self::TYPE) {
52
+
throw new \InvalidArgumentException("Expected type \"" . self::TYPE . "\", got \"$type\".");
53
+
}
54
+
if (!is_array($data)
55
+
|| !is_string($data['to'] ?? null)
56
+
|| !is_string($data['subject'] ?? null)
57
+
|| !is_string($data['body'] ?? null)
58
+
) {
59
+
throw new \InvalidArgumentException('Invalid data for ' . self::class . '.');
60
+
}
61
+
return new self($data['to'], $data['subject'], $data['body']);
0 commit comments