@@ -236,35 +236,62 @@ what actions are allowed on a blog post::
236236Accessing the Workflow in a Class
237237---------------------------------
238238
239- To access workflow inside a class, use dependency injection and inject the
240- registry in the constructor::
239+ You can use the workflow inside a class by using
240+ :doc: `service autowiring </service_container/autowiring >` and using
241+ ``camelCased workflow name + Workflow `` as parameter name::
241242
242243 use App\Entity\BlogPost;
243- use Symfony\Component\Workflow\Registry ;
244+ use Symfony\Component\Workflow\WorkflowInterface ;
244245
245246 class MyClass
246247 {
247- private $workflowRegistry ;
248+ private $blogPublishingWorkflow ;
248249
249- public function __construct(Registry $workflowRegistry)
250+ // this injects the blog_publishing workflow configured before
251+ public function __construct(WorkflowInterface $blogPublishingWorkflow)
250252 {
251- $this->workflowRegistry = $workflowRegistry ;
253+ $this->blogPublishingWorkflow = $blogPublishingWorkflow ;
252254 }
253255
254256 public function toReview(BlogPost $post)
255257 {
256- $workflow = $this->workflowRegistry->get($post);
257-
258258 // Update the currentState on the post
259259 try {
260- $workflow ->apply($post, 'to_review');
260+ $this->blogPublishingWorkflow ->apply($post, 'to_review');
261261 } catch (LogicException $exception) {
262262 // ...
263263 }
264264 // ...
265265 }
266266 }
267267
268+ Alternatively, use the registry::
269+
270+ use App\Entity\BlogPost;
271+ use Symfony\Component\Workflow\Registry;
272+
273+ class MyClass
274+ {
275+ private $workflowRegistry;
276+
277+ public function __construct(Registry $workflowRegistry)
278+ {
279+ $this->workflowRegistry = $workflowRegistry;
280+ }
281+
282+ public function toReview(BlogPost $post)
283+ {
284+ $blogPublishingWorkflow = $this->workflowRegistry->get($post);
285+
286+ // ...
287+ }
288+ }
289+
290+ .. tip ::
291+
292+ You can find the list of available workflow services with the
293+ ``php bin/console debug:autowiring workflow `` command.
294+
268295Using Events
269296------------
270297
@@ -829,25 +856,23 @@ Then you can access this metadata in your controller as follows::
829856
830857 // src/App/Controller/BlogPostController.php
831858 use App\Entity\BlogPost;
832- use Symfony\Component\Workflow\Registry ;
859+ use Symfony\Component\Workflow\WorkflowInterface ;
833860 // ...
834861
835- public function myAction(Registry $registry , BlogPost $post)
862+ public function myAction(WorkflowInterface $blogPublishingWorkflow , BlogPost $post)
836863 {
837- $workflow = $registry->get($post);
838-
839- $title = $workflow
864+ $title = $blogPublishingWorkflow
840865 ->getMetadataStore()
841866 ->getWorkflowMetadata()['title'] ?? 'Default title'
842867 ;
843868
844- $maxNumOfWords = $workflow
869+ $maxNumOfWords = $blogPublishingWorkflow
845870 ->getMetadataStore()
846871 ->getPlaceMetadata('draft')['max_num_of_words'] ?? 500
847872 ;
848873
849- $aTransition = $workflow ->getDefinition()->getTransitions()[0];
850- $priority = $workflow
874+ $aTransition = $blogPublishingWorkflow ->getDefinition()->getTransitions()[0];
875+ $priority = $blogPublishingWorkflow
851876 ->getMetadataStore()
852877 ->getTransitionMetadata($aTransition)['priority'] ?? 0
853878 ;
@@ -870,7 +895,7 @@ In a :ref:`flash message <flash-messages>` in your controller::
870895
871896 // $transition = ...; (an instance of Transition)
872897
873- // $workflow is a Workflow instance retrieved from the Registry (see above)
898+ // $workflow is a Workflow instance retrieved from the Registry or injected directly (see above)
874899 $title = $workflow->getMetadataStore()->getMetadata('title', $transition);
875900 $this->addFlash('info', "You have successfully applied the transition with title: '$title'");
876901
0 commit comments