Skip to content

Commit a8daa56

Browse files
roxblnfkjsundai
andauthored
PHP: Add Dynamic components section (#3571)
* PHP: Add message-passing / Dynamic components * Add keywords * Update message-passing.mdx * removing some unnecessary commas and consistency edits --------- Co-authored-by: Jwahir Sundai <jwahir.sundai@temporal.io>
1 parent e039ecd commit a8daa56

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

docs/develop/php/index.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Send messages to read the state of Workflow Executions.
7373
- [How to develop with Updates](/develop/php/message-passing#updates)
7474
- [Message handler patterns](/develop/php/message-passing#message-handler-patterns)
7575
- [Message handler troubleshooting](/develop/php/message-passing#message-handler-troubleshooting)
76+
- [How to develop with Dynamic Handlers](/develop/php/message-passing#dynamic-handler)
7677

7778
## [Interrupt a Workflow feature guide](/develop/php/cancellation)
7879

docs/develop/php/message-passing.mdx

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ keywords:
88
- signals
99
- queries
1010
- updates
11+
- dynamic signals
12+
- dynamic queries
13+
- dynamic updates
1114
tags:
1215
- Workflows
1316
- Messages
@@ -839,3 +842,83 @@ When working with Queries, you may encounter these errors:
839842

840843
- **The handler caused the Workflow Task to fail.**
841844
This would happen, for example, if the Query handler blocks the thread for too long without yielding.
845+
846+
## Dynamic components {#dynamic-handler}
847+
848+
Temporal supports Dynamic Queries, Signals, and Updates.
849+
These are unnamed handlers that are invoked if no other statically defined handler with the given name exists.
850+
851+
Dynamic Handlers provide flexibility to handle cases where the names of Queries, Signals, or Updates aren't known at run time.
852+
853+
:::caution
854+
855+
Dynamic Handlers should be used judiciously as a fallback mechanism rather than the primary approach.
856+
Overusing them can lead to maintainability and debugging issues down the line.
857+
858+
Instead, Signals, or Queries should be defined statically whenever possible, with clear names that indicate their purpose.
859+
Use static definitions as the primary way of structuring your Workflows.
860+
861+
Reserve Dynamic Handlers for cases where the handler names are not known at development time and need to be looked up dynamically at runtime.
862+
They are meant to handle edge cases and act as a catch-all, not as the main way of invoking logic.
863+
864+
:::
865+
866+
### How to set a Dynamic Query {#set-a-dynamic-query}
867+
868+
A Dynamic Query in Temporal is a Query method that is invoked dynamically at runtime if no other Query with the same name is registered.
869+
Use [`Workflow::registerDynamicQuery()`](https://php.temporal.io/classes/Temporal-Workflow.html#method_registerDynamicQuery) to set a dynamic Query handler.
870+
871+
The Query Handler parameters must accept a `string` name and [`ValuesInterface`](https://php.temporal.io/classes/Temporal-DataConverter-ValuesInterface.html) for the arguments.
872+
873+
```php
874+
Workflow::registerDynamicQuery(function (string $name, ValuesInterface $arguments): string {
875+
return \sprintf(
876+
'Got query `%s` with %d arguments',
877+
$name,
878+
$arguments->count(),
879+
);
880+
});
881+
```
882+
883+
### How to set a Dynamic Signal {#set-a-dynamic-signal}
884+
885+
A Dynamic Signal in Temporal is a Signal that is invoked dynamically at runtime if no other Signal with the same input is registered.
886+
Use [`Workflow::registerDynamicSignal()`](https://php.temporal.io/classes/Temporal-Workflow.html#method_registerDynamicSignal) to set a dynamic Signal handler.
887+
888+
The Signal Handler parameters must accept a `string` name and [`ValuesInterface`](https://php.temporal.io/classes/Temporal-DataConverter-ValuesInterface.html) for the arguments.
889+
890+
```php
891+
Workflow::registerDynamicSignal(function (string $name, ValuesInterface $arguments): void {
892+
Workflow::getLogger()->info(\sprintf(
893+
'Executed signal `%s` with %d arguments',
894+
$name,
895+
$arguments->count(),
896+
));
897+
});
898+
```
899+
900+
### How to set a Dynamic Update {#set-a-dynamic-update}
901+
902+
A Dynamic Update in Temporal is an Update that is invoked dynamically at runtime if no other Update with the same input is registered.
903+
Use [`Workflow::registerDynamicUpdate()`](https://php.temporal.io/classes/Temporal-Workflow.html#method_registerDynamicUpdate) to set a dynamic Update handler.
904+
905+
The method accepts two arguments:
906+
907+
- Update Handler
908+
- Update Validator (optional) that should throw an exception if the validation fails
909+
910+
Both the Handler and the Validator must accept a `string` name and [`ValuesInterface`](https://php.temporal.io/classes/Temporal-DataConverter-ValuesInterface.html) for the arguments.
911+
912+
```php
913+
Workflow::registerDynamicUpdate(
914+
static fn(string $name, ValuesInterface $arguments): string => \sprintf(
915+
'Got update `%s` with %d arguments',
916+
$name,
917+
$arguments->count(),
918+
),
919+
static fn(string $name, ValuesInterface $arguments) => \str_starts_with(
920+
$name,
921+
'update_',
922+
) or throw new \InvalidArgumentException('Invalid update name'),
923+
);
924+
```

0 commit comments

Comments
 (0)