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: docs/guide/en/callable-definitions-extended.md
+6-5Lines changed: 6 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -57,9 +57,7 @@ As you can see in the [PHP documentation](https://www.php.net/manual/en/language
57
57
58
58
## Type 2: Callable definition extensions (via container)
59
59
60
-
Under the hood, extended callable definitions behave exactly like native callables. But there is a major difference:
61
-
all the objects are instantiated automatically by a PSR-11 DI container with all their dependencies
62
-
and in a lazy way (only when they are needed).
60
+
The difference from native PHP callables is that you don't need to instantiate objects yourself: you pass a class name or alias, and the DI container resolves the instance lazily, only when the callable is actually invoked.
63
61
Ways to define an extended callable:
64
62
65
63
- An object method through a class name or alias:
@@ -76,14 +74,17 @@ Ways to define an extended callable:
76
74
77
75
$callable = [Foo::class, 'bar'];
78
76
```
79
-
Here is a simplified example of how it works:
77
+
Here is a simplified example of how it works (for non-static methods):
80
78
```php
81
79
if ($container->has($callable[0])) {
82
80
$callable[0] = $container->get($callable[0])
83
81
}
84
-
82
+
85
83
$callable();
86
84
```
85
+
86
+
> [!NOTE]
87
+
> If `bar` is declared `static`, no object is instantiated — the method is called statically on the class.
87
88
- Class name of an object with [the `__invoke` method](https://www.php.net/manual/en/language.oop5.magic.php#object.invoke) implemented:
Copy file name to clipboardExpand all lines: docs/guide/en/console-commands.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,8 @@ If you are using [yiisoft/config](https://github.com/yiisoft/config) and [yiisof
6
6
7
7
If you are using [symfony/console](https://github.com/symfony/console) directly, you should register the commands manually.
8
8
9
+
> **Note:** The default queue name list (used when no queue names are passed to a command) is only available when using [yiisoft/config](https://github.com/yiisoft/config) and [yiisoft/yii-console](https://github.com/yiisoft/yii-console). Without them, you must pass the queue name list explicitly to the command constructor.
10
+
9
11
In [yiisoft/app](https://github.com/yiisoft/app) the `yii` console binary is provided out of the box.
10
12
If you are using [yiisoft/console](https://github.com/yiisoft/console) or `symfony/console` without that template, invoke these commands the same way you invoke other console commands in your application.
11
13
@@ -15,7 +17,7 @@ The command `queue:run` obtains and handles messages until the queue is empty, t
15
17
16
18
You can also narrow the scope of processed messages by specifying queue name(s) and maximum number of messages to process:
17
19
18
-
- Specify one or more queue names to process. Messages from other queues will be ignored. Default is all registered queue names (in case of using [yiisoft/config](https://github.com/yiisoft/config) and [yiisoft/yii-console](https://github.com/yiisoft/yii-console), otherwise pass the default queue name list to the command constructor).
20
+
- Specify one or more queue names to process. Messages from other queues will be ignored. Defaults to all registered queue names.
19
21
- Use `--limit` to limit the number of messages processed. When set, command will exit either when all the messages are processed or when the maximum count is reached.
20
22
21
23
The full command signature is:
@@ -35,12 +37,10 @@ yii queue:listen [queueName]
35
37
36
38
The following command iterates through multiple queues and is meant to be used in development environment only, as it consumes a lot of CPU for iterating through queues. You can pass to it:
37
39
38
-
- `queueName` argument(s). Specify one or more queue names to process. Messages from other queues will be ignored. Default is all registered queue names (in case of using [yiisoft/config](https://github.com/yiisoft/config) and [yiisoft/yii-console](https://github.com/yiisoft/yii-console), otherwise pass the default queue name list to the command constructor).
40
+
- `queueName` argument(s). Specify one or more queue names to process. Messages from other queues will be ignored. Defaults to all registered queue names.
39
41
- `--limit` option to limit the number of messages processed before switching to another queue. E.g. you set`--limit` to 500 and right now you have 1000 messages in`queue1`. This command will consume only 500 of them, then it will switch to `queue2` to see if there are any messages there. Defaults to `0` (no limit).
40
42
- `--pause` option to specify the number of seconds to pause between checking queues when no messages are found. Defaults to `1`.
41
43
42
-
`queue:listen` does not have a `--limit` option. If you need to stop after processing a certain number of messages, use `queue:run --limit=...`.
Copy file name to clipboardExpand all lines: docs/guide/en/message-handler.md
+12-13Lines changed: 12 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
# Message handler advanced
2
2
3
-
This page covers named handler definitions, callable formats, pitfalls, and recommended implementation styles.
3
+
This page covers named handler definitions, callable formats, pitfalls, and valid handler signatures.
4
4
5
-
For the zero-config FQCN approach (same-app producer and consumer), see [Message handler: simple setup](message-handler-simple.md).
5
+
If you haven't read [Message handler: simple setup](message-handler-simple.md) yet, start there — it introduces handler classes and the zero-config FQCN approach.
6
6
For a conceptual overview of what messages and handlers are, see [Messages and handlers: concepts](messages-and-handlers.md).
7
7
8
8
Handler definitions are configured in:
@@ -36,9 +36,8 @@ return [
36
36
];
37
37
```
38
38
39
-
Handler definition should be either an [extended callable definition](./callable-definitions-extended.md) or a string for your DI container to resolve a `MessageHandlerInterface` instance.
39
+
Handler definition should be either an [extended callable definition](./callable-definitions-extended.md) or a container identifier that resolves to a `MessageHandlerInterface` instance.
40
40
41
-
For the simpler FQCN-based approach that requires no mapping, see [Message handler: simple setup](message-handler-simple.md).
42
41
43
42
## When mapping by short names is a better idea
44
43
@@ -65,18 +64,18 @@ This way external producers never need to know your internal PHP class names.
65
64
66
65
## Common pitfalls and unsupported formats
67
66
68
-
- A class-string that is not resolvable via `$container->has()` will not be auto-instantiated.
67
+
- A PHP class name that is not registered in the DI container will not be auto-instantiated.
69
68
-[yiisoft/definitions](https://github.com/yiisoft/definitions) array format (like `['class' => ..., '__construct()' => ...]`) is **not** supported for handlers.
70
69
71
-
## Recommended handler implementation styles
70
+
## Valid handler signatures
72
71
73
-
- Prefer a dedicated handler class registered in DI.
74
-
- For maximal compatibility with the worker resolution rules either:
75
-
- Implement `MessageHandlerInterface`
76
-
- Make the handler invokable (`__invoke(MessageInterface $message): void`)
77
-
- Provide `[HandlerClass::class, 'handle']` and keep `handle(MessageInterface $message): void` as the entry point
-`MessageHandlerInterface` — implement the interface; the worker calls `handle(MessageInterface $message): void` directly (covered in [simple setup](message-handler-simple.md)).
75
+
- Invokable class — add `__invoke(MessageInterface $message): void`.
76
+
- Explicit method — reference as `[HandlerClass::class, 'handle']` with `handle(MessageInterface $message): void` as the entry point.
77
+
78
+
## Config location (yiisoft/config)
80
79
81
80
When using [yiisoft/config](https://github.com/yiisoft/config), configure handlers under the [`yiisoft/queue`](https://github.com/yiisoft/queue) params key:
82
81
@@ -90,4 +89,4 @@ return [
90
89
];
91
90
```
92
91
93
-
This config is consumed by the DI definitions from `config/di.php` where the `Worker` is constructed with `$params['yiisoft/queue']['handlers']`.
92
+
This config is consumed by the DI definitions from [`config/di.php`](../../../config/di.php) where the `Worker` is constructed with `$params['yiisoft/queue']['handlers']`.
- Accepts multiple providers and queries them in order.
72
72
- The first provider whose `has()` returns true for the queue name wins.
73
-
- Useful for mixing strict registries with pre-built queues.
73
+
- Useful for mixing multiple providers, for example combining adapter-based and pre-built queues.
74
74
75
75
Example:
76
76
@@ -87,8 +87,8 @@ $provider = new CompositeQueueProvider(
87
87
$queueForEmails = $provider->get('emails');
88
88
```
89
89
90
-
## Extending the registry
90
+
## Implementing a custom provider
91
91
92
-
- Implement `QueueProviderInterface` if you need bespoke selection logic (e.g., tenant-specific registries, remote lookups, or metrics-aware routing).
92
+
- Implement `QueueProviderInterface` if you need bespoke selection logic (e.g., tenant-specific routing, remote lookups, or metrics-aware routing).
93
93
- Register your provider in the DI container and swap it in wherever `QueueProviderInterface` is used.
94
-
- Consider exposing diagnostics (e.g., list of available queues) through `getNames()`, console commands, or health checks so operators can verify the registry at runtime.
94
+
- Consider exposing diagnostics (e.g., list of available queues) through `getNames()`, console commands, or health checks so operators can verify the available queues at runtime.
0 commit comments