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: packages/workflow.md
+68-1Lines changed: 68 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -337,6 +337,73 @@ if ($result->skip()->contains('optionalJob')) {
337
337
338
338
---
339
339
340
+
## Dependency Injection
341
+
342
+
Workflow integrates with [chevere/container](https://chevere.org/packages/container) to provide automatic dependency injection for Action classes. When your jobs use Action classes with constructor dependencies, you can provide a container that will automatically resolve and inject those dependencies.
343
+
344
+
### Passing a Container
345
+
346
+
Pass a `ContainerInterface` instance as the second argument to `run()`:
347
+
348
+
```php
349
+
use Chevere\Container\Container;
350
+
use function Chevere\Workflow\run;
351
+
352
+
// Create container with dependencies
353
+
$container = new Container(
354
+
logger: new Logger(),
355
+
database: new Database()
356
+
);
357
+
358
+
// Run workflow with container
359
+
$result = run($workflow, $container, ...$vars);
360
+
```
361
+
362
+
When a job references an Action class, Workflow uses the container to:
363
+
364
+
1.**Auto-inject dependencies** - Automatically resolve constructor parameters from the container
365
+
2.**Validate availability** - Ensure all required dependencies are present before execution
366
+
3.**Support nested dependencies** - Recursively resolve dependencies of dependencies
367
+
368
+
### Example with Action Dependencies
369
+
370
+
```php
371
+
use Chevere\Action\Action;
372
+
373
+
class SendNotification extends Action
374
+
{
375
+
// Dependencies injected automatically
376
+
public function __construct(
377
+
private LoggerInterface $logger,
378
+
private MailerInterface $mailer
379
+
) {}
380
+
381
+
public function __invoke(string $email, string $message): bool
Control whether a job runs using `withRunIf()` (run when conditions are met) or `withRunIfNot()` (skip when conditions are met). Both methods accept the same kinds of conditions and are evaluated at run-time.
@@ -346,7 +413,7 @@ Control whether a job runs using `withRunIf()` (run when conditions are met) or
346
413
*`boolean` literal — evaluated directly
347
414
*`variable('name')` — runtime argument coerced to boolean
348
415
*`response('job')` or `response('job', 'key')` — uses another job's output
349
-
*`Closure` — invokes a closure passing the current `RunInterface` context
416
+
*`callable` — invokes a callable passing the current `RunInterface` context argument
0 commit comments