Skip to content

Commit e0fdf16

Browse files
committed
document di
1 parent cfa62ad commit e0fdf16

1 file changed

Lines changed: 68 additions & 1 deletion

File tree

packages/workflow.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,73 @@ if ($result->skip()->contains('optionalJob')) {
337337

338338
---
339339

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
382+
{
383+
$this->logger->info("Sending email to {$email}");
384+
return $this->mailer->send($email, $message);
385+
}
386+
}
387+
388+
// Provide dependencies in container
389+
$container = new Container(
390+
logger: new ConsoleLogger(),
391+
mailer: new SmtpMailer()
392+
);
393+
394+
$workflow = workflow(
395+
notify: sync(
396+
SendNotification::class, // Dependencies auto-injected
397+
email: variable('userEmail'),
398+
message: 'Welcome!'
399+
)
400+
);
401+
402+
$result = run($workflow, $container, userEmail: 'user@example.com');
403+
```
404+
405+
---
406+
340407
## Conditional Execution
341408

342409
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
346413
* `boolean` literal — evaluated directly
347414
* `variable('name')` — runtime argument coerced to boolean
348415
* `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
350417

351418
---
352419

0 commit comments

Comments
 (0)