@@ -327,26 +327,31 @@ if you are using Doctrine, the matching column definition should use the type ``
327
327
Accessing the Workflow in a Class
328
328
---------------------------------
329
329
330
- You can use the workflow inside a class by using
331
- :doc: `service autowiring </service_container/autowiring >` and using
332
- ``camelCased workflow name + Workflow `` as parameter name. If it is a state
333
- machine type, use ``camelCased workflow name + StateMachine ``::
330
+ Symfony creates a service for each workflow you define. You have two ways of
331
+ injecting each workflow in any service or controller:
332
+
333
+ **(1) Use a specific argument name **
334
+
335
+ Type-hint your construtor/method argument with ``WorkflowInterface `` and name the
336
+ argument using this pattern: "workflow name in camelCase" + ``Workflow `` suffix.
337
+ If it is a state machine type, use the ``StateMachine `` suffix.
338
+
339
+ For example, to inject the ``blog_publishing `` workflow defined earlier::
334
340
335
341
use App\Entity\BlogPost;
336
342
use Symfony\Component\Workflow\WorkflowInterface;
337
343
338
344
class MyClass
339
345
{
340
346
public function __construct(
341
- // Symfony will inject the 'blog_publishing' workflow configured before
342
347
private WorkflowInterface $blogPublishingWorkflow,
343
348
) {
344
349
}
345
350
346
351
public function toReview(BlogPost $post): void
347
352
{
348
- // Update the currentState on the post
349
353
try {
354
+ // update the currentState on the post
350
355
$this->blogPublishingWorkflow->apply($post, 'to_review');
351
356
} catch (LogicException $exception) {
352
357
// ...
@@ -355,28 +360,27 @@ machine type, use ``camelCased workflow name + StateMachine``::
355
360
}
356
361
}
357
362
358
- Workflows can also be injected thanks to their name and the
359
- :class: `Symfony\\ Component\\ DependencyInjection\\ Attribute\\ Target `
360
- attribute::
363
+ **(2) Use the ``#[Target]`` attribute **
364
+
365
+ When :ref: `dealing with multiple implementations of the same type <autowiring-multiple-implementations-same-type >`
366
+ the ``#[Target] `` attribute helps you select which one to inject. Symfony creates
367
+ a target with the same name as each workflow.
368
+
369
+ For example, to select the ``blog_publishing `` lock defined earlier::
361
370
362
- use App\Entity\BlogPost;
363
371
use Symfony\Component\DependencyInjection\Attribute\Target;
364
372
use Symfony\Component\Workflow\WorkflowInterface;
365
373
366
374
class MyClass
367
375
{
368
376
public function __construct(
369
- #[Target('blog_publishing')]
370
- private WorkflowInterface $workflow
377
+ #[Target('blog_publishing')] private WorkflowInterface $workflow,
371
378
) {
372
379
}
373
380
374
381
// ...
375
382
}
376
383
377
- This allows you to decorrelate the argument name of any implementation
378
- name.
379
-
380
384
.. versionadded :: 6.2
381
385
382
386
All workflows and state machines services are tagged since in Symfony 6.2.
@@ -402,6 +406,36 @@ name.
402
406
You can find the list of available workflow services with the
403
407
``php bin/console debug:autowiring workflow `` command.
404
408
409
+ Injecting Multiple Workflows
410
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
411
+
412
+ Use the :ref: `AutowireLocator <service-locator_autowire-locator >` attribute to
413
+ inject several workflows into the same class::
414
+
415
+ use Symfony\Component\DependencyInjection\ServiceLocator;
416
+ use Symfony\Component\DependencyInjection\Attribute\AutowireLocator;
417
+
418
+ class MyClass
419
+ {
420
+ public function __construct(
421
+ #[AutowireLocator('blog_publishing', 'user_registration')]
422
+ private ServiceLocator $workflows,
423
+ ) {
424
+ }
425
+
426
+ public function someMethod(): void
427
+ {
428
+ $workflow = $this->workflows->get('user_registration');
429
+
430
+ // ...
431
+ }
432
+ }
433
+
434
+ .. tip ::
435
+
436
+ Injecting multiple workflows into your :ref: `workflow listeners <workflow_using-events >`
437
+ can help you simplify shared logic.
438
+
405
439
.. _workflow_using-events :
406
440
407
441
Using Events
0 commit comments