|
4 | 4 |
|
5 | 5 | namespace Workflow\V2; |
6 | 6 |
|
| 7 | +use Carbon\CarbonInterval; |
7 | 8 | use Throwable; |
8 | 9 | use Workflow\Traits\ResolvesMethodDependencies; |
9 | 10 | use Workflow\V2\Models\WorkflowRun; |
10 | 11 | use Workflow\V2\Support\ChildWorkflowHandles; |
11 | 12 | use Workflow\V2\Support\HistoryBudget; |
12 | 13 |
|
| 14 | +/** |
| 15 | + * Base class for v2 workflows. |
| 16 | + * |
| 17 | + * Application workflows extend this class and implement an `execute` (or |
| 18 | + * `handle`) entry method. The class also exposes the v2 authoring API as |
| 19 | + * a static facade so workflow code can read like: |
| 20 | + * |
| 21 | + * use Workflow\V2\Workflow; |
| 22 | + * |
| 23 | + * class MyWorkflow extends Workflow |
| 24 | + * { |
| 25 | + * public function execute(): mixed |
| 26 | + * { |
| 27 | + * $result = Workflow::activity(MyActivity::class, 'arg'); |
| 28 | + * Workflow::timer('5 seconds'); |
| 29 | + * Workflow::upsertMemo(['stage' => 'finalizing']); |
| 30 | + * |
| 31 | + * return $result; |
| 32 | + * } |
| 33 | + * } |
| 34 | + * |
| 35 | + * Each static method is a thin delegate to the equivalent namespaced |
| 36 | + * helper under `Workflow\V2\*` — authors can pick either style. |
| 37 | + * |
| 38 | + * @api Stable v2 authoring API. Adding new static methods is an additive |
| 39 | + * (non-breaking) change; removing or renaming a documented method |
| 40 | + * requires a major version bump. See docs/api-stability.md. |
| 41 | + */ |
13 | 42 | abstract class Workflow |
14 | 43 | { |
15 | 44 | use ResolvesMethodDependencies; |
@@ -44,7 +73,16 @@ public function runId(): string |
44 | 73 | return $this->run->id; |
45 | 74 | } |
46 | 75 |
|
47 | | - public function child(): ?ChildWorkflowHandle |
| 76 | + /** |
| 77 | + * The handle for the most recently spawned child workflow, or null if |
| 78 | + * this workflow has not spawned any children yet. |
| 79 | + * |
| 80 | + * Renamed from `child()` in v2 so the `Workflow::child(...)` static |
| 81 | + * facade can spawn child workflows. Use `lastChild()` for the handle |
| 82 | + * lookup and `Workflow::child(...)` / `Workflow::executeChildWorkflow(...)` |
| 83 | + * to spawn a new child. |
| 84 | + */ |
| 85 | + public function lastChild(): ?ChildWorkflowHandle |
48 | 86 | { |
49 | 87 | $children = $this->children(); |
50 | 88 |
|
@@ -141,4 +179,220 @@ public function setCommandDispatchEnabled(bool $enabled): void |
141 | 179 | { |
142 | 180 | $this->commandDispatchEnabled = $enabled; |
143 | 181 | } |
| 182 | + |
| 183 | + // ── Static authoring facade ───────────────────────────────────────────── |
| 184 | + // |
| 185 | + // The static methods below delegate to the namespaced helpers under |
| 186 | + // `Workflow\V2\*` (see src/V2/functions.php). They exist so workflow |
| 187 | + // authors can use the more conventional `Workflow::timer(...)` form |
| 188 | + // without an extra import. See the class docblock for example usage. |
| 189 | + // |
| 190 | + // These are pure delegates. Behaviour, determinism guarantees, and |
| 191 | + // operand/return types come from the underlying helper functions. |
| 192 | + |
| 193 | + /** |
| 194 | + * Invoke an activity and wait for its result. |
| 195 | + * |
| 196 | + * @see activity() |
| 197 | + */ |
| 198 | + public static function activity(string $activity, mixed ...$arguments): mixed |
| 199 | + { |
| 200 | + return activity($activity, ...$arguments); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Alias for {@see activity()} matching Temporal's `executeActivity` name. |
| 205 | + */ |
| 206 | + public static function executeActivity(string $activity, mixed ...$arguments): mixed |
| 207 | + { |
| 208 | + return activity($activity, ...$arguments); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Invoke a child workflow and wait for its result. |
| 213 | + * |
| 214 | + * @see child() |
| 215 | + */ |
| 216 | + public static function child(string $workflow, mixed ...$arguments): mixed |
| 217 | + { |
| 218 | + return child($workflow, ...$arguments); |
| 219 | + } |
| 220 | + |
| 221 | + /** |
| 222 | + * Alias for {@see child()} matching Temporal's `executeChildWorkflow` name. |
| 223 | + */ |
| 224 | + public static function executeChildWorkflow(string $workflow, mixed ...$arguments): mixed |
| 225 | + { |
| 226 | + return child($workflow, ...$arguments); |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * Run a callable as an auto-generated child workflow. |
| 231 | + * |
| 232 | + * @see async() |
| 233 | + */ |
| 234 | + public static function async(callable $callback): mixed |
| 235 | + { |
| 236 | + return async($callback); |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * Await a list of concurrent calls and return their resolved results in |
| 241 | + * iteration order. |
| 242 | + * |
| 243 | + * @see all() |
| 244 | + */ |
| 245 | + public static function all(iterable $calls): mixed |
| 246 | + { |
| 247 | + return all($calls); |
| 248 | + } |
| 249 | + |
| 250 | + /** |
| 251 | + * Alias for {@see all()} matching the "run these in parallel" intent. |
| 252 | + */ |
| 253 | + public static function parallel(iterable $calls): mixed |
| 254 | + { |
| 255 | + return all($calls); |
| 256 | + } |
| 257 | + |
| 258 | + /** |
| 259 | + * Wait for a condition closure to become truthy, for a signal by name, |
| 260 | + * or for either plus a timeout. |
| 261 | + * |
| 262 | + * @see await() |
| 263 | + */ |
| 264 | + public static function await( |
| 265 | + callable|string $condition, |
| 266 | + int|string|CarbonInterval|null $timeout = null, |
| 267 | + ?string $conditionKey = null, |
| 268 | + ): mixed { |
| 269 | + return await($condition, $timeout, $conditionKey); |
| 270 | + } |
| 271 | + |
| 272 | + /** |
| 273 | + * Wait for a condition or signal with an explicit timeout, failing the |
| 274 | + * await when the timeout elapses. |
| 275 | + */ |
| 276 | + public static function awaitWithTimeout( |
| 277 | + int|string|CarbonInterval $timeout, |
| 278 | + callable|string $condition, |
| 279 | + ?string $conditionKey = null, |
| 280 | + ): mixed { |
| 281 | + return await($condition, $timeout, $conditionKey); |
| 282 | + } |
| 283 | + |
| 284 | + /** |
| 285 | + * Wait for a named signal. Equivalent to `await(<signal name>)`. |
| 286 | + */ |
| 287 | + public static function awaitSignal(string $name): mixed |
| 288 | + { |
| 289 | + return await($name); |
| 290 | + } |
| 291 | + |
| 292 | + /** |
| 293 | + * Suspend until a timer fires. |
| 294 | + * |
| 295 | + * @see timer() |
| 296 | + */ |
| 297 | + public static function timer(int|string|CarbonInterval $duration): mixed |
| 298 | + { |
| 299 | + return timer($duration); |
| 300 | + } |
| 301 | + |
| 302 | + /** |
| 303 | + * Capture the result of a side-effect closure in history so replay |
| 304 | + * returns the same value on subsequent attempts. |
| 305 | + * |
| 306 | + * @see sideEffect() |
| 307 | + */ |
| 308 | + public static function sideEffect(callable $callback): mixed |
| 309 | + { |
| 310 | + return sideEffect($callback); |
| 311 | + } |
| 312 | + |
| 313 | + /** |
| 314 | + * Terminate the current run by starting a new one with the provided |
| 315 | + * arguments, preserving workflow instance identity. |
| 316 | + * |
| 317 | + * @see continueAsNew() |
| 318 | + */ |
| 319 | + public static function continueAsNew(mixed ...$arguments): mixed |
| 320 | + { |
| 321 | + return continueAsNew(...$arguments); |
| 322 | + } |
| 323 | + |
| 324 | + /** |
| 325 | + * Declare a workflow-code versioning change and receive the negotiated |
| 326 | + * version for the current run. |
| 327 | + * |
| 328 | + * @see getVersion() |
| 329 | + */ |
| 330 | + public static function getVersion( |
| 331 | + string $changeId, |
| 332 | + int $minSupported = WorkflowStub::DEFAULT_VERSION, |
| 333 | + int $maxSupported = 1, |
| 334 | + ): mixed { |
| 335 | + return getVersion($changeId, $minSupported, $maxSupported); |
| 336 | + } |
| 337 | + |
| 338 | + /** |
| 339 | + * Upsert non-indexed memo metadata on the current workflow run. |
| 340 | + * |
| 341 | + * @param array<string, mixed> $entries |
| 342 | + * |
| 343 | + * @see upsertMemo() |
| 344 | + */ |
| 345 | + public static function upsertMemo(array $entries): void |
| 346 | + { |
| 347 | + upsertMemo($entries); |
| 348 | + } |
| 349 | + |
| 350 | + /** |
| 351 | + * Upsert indexed search attributes on the current workflow run. |
| 352 | + * |
| 353 | + * @param array<string, scalar|null> $attributes |
| 354 | + * |
| 355 | + * @see upsertSearchAttributes() |
| 356 | + */ |
| 357 | + public static function upsertSearchAttributes(array $attributes): void |
| 358 | + { |
| 359 | + upsertSearchAttributes($attributes); |
| 360 | + } |
| 361 | + |
| 362 | + // Timer sugar -------------------------------------------------------- |
| 363 | + |
| 364 | + public static function seconds(int $seconds): mixed |
| 365 | + { |
| 366 | + return seconds($seconds); |
| 367 | + } |
| 368 | + |
| 369 | + public static function minutes(int $minutes): mixed |
| 370 | + { |
| 371 | + return minutes($minutes); |
| 372 | + } |
| 373 | + |
| 374 | + public static function hours(int $hours): mixed |
| 375 | + { |
| 376 | + return hours($hours); |
| 377 | + } |
| 378 | + |
| 379 | + public static function days(int $days): mixed |
| 380 | + { |
| 381 | + return days($days); |
| 382 | + } |
| 383 | + |
| 384 | + public static function weeks(int $weeks): mixed |
| 385 | + { |
| 386 | + return weeks($weeks); |
| 387 | + } |
| 388 | + |
| 389 | + public static function months(int $months): mixed |
| 390 | + { |
| 391 | + return months($months); |
| 392 | + } |
| 393 | + |
| 394 | + public static function years(int $years): mixed |
| 395 | + { |
| 396 | + return years($years); |
| 397 | + } |
144 | 398 | } |
0 commit comments