The activity() function executes an activity within a workflow. Activities are functions that perform side effects or interact with external systems.
Signature
function activity($activity, ...$arguments): PromiseInterface
Parameters
The activity class or name to execute
Optional arguments to pass to the activity
Returns
PromiseInterface
React\Promise\PromiseInterface
A promise that resolves to the activity’s result
Usage
use Workflow\Workflow;
use function Workflow\activity;
class MyWorkflow extends Workflow
{
public function execute()
{
// Execute an activity
$result = yield activity(SendEmailActivity::class);
// Execute an activity with arguments
$result = yield activity(ProcessPaymentActivity::class, $userId, $amount);
return $result;
}
}
Example with Multiple Activities
use Workflow\Workflow;
use function Workflow\{activity, all};
class OrderWorkflow extends Workflow
{
public function execute($orderId)
{
// Execute activities sequentially
$payment = yield activity(ProcessPaymentActivity::class, $orderId);
$shipping = yield activity(CreateShipmentActivity::class, $orderId);
$notification = yield activity(SendNotificationActivity::class, $orderId);
return [
'payment' => $payment,
'shipping' => $shipping,
'notification' => $notification,
];
}
}
- child() - Execute a child workflow
- async() - Execute asynchronous code