@@ -236,35 +236,62 @@ what actions are allowed on a blog post::
236
236
Accessing the Workflow in a Class
237
237
---------------------------------
238
238
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::
241
242
242
243
use App\Entity\BlogPost;
243
- use Symfony\Component\Workflow\Registry ;
244
+ use Symfony\Component\Workflow\WorkflowInterface ;
244
245
245
246
class MyClass
246
247
{
247
- private $workflowRegistry ;
248
+ private $blogPublishingWorkflow ;
248
249
249
- public function __construct(Registry $workflowRegistry)
250
+ // this injects the blog_publishing workflow configured before
251
+ public function __construct(WorkflowInterface $blogPublishingWorkflow)
250
252
{
251
- $this->workflowRegistry = $workflowRegistry ;
253
+ $this->blogPublishingWorkflow = $blogPublishingWorkflow ;
252
254
}
253
255
254
256
public function toReview(BlogPost $post)
255
257
{
256
- $workflow = $this->workflowRegistry->get($post);
257
-
258
258
// Update the currentState on the post
259
259
try {
260
- $workflow ->apply($post, 'to_review');
260
+ $this->blogPublishingWorkflow ->apply($post, 'to_review');
261
261
} catch (LogicException $exception) {
262
262
// ...
263
263
}
264
264
// ...
265
265
}
266
266
}
267
267
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
+
268
295
Using Events
269
296
------------
270
297
@@ -829,25 +856,23 @@ Then you can access this metadata in your controller as follows::
829
856
830
857
// src/App/Controller/BlogPostController.php
831
858
use App\Entity\BlogPost;
832
- use Symfony\Component\Workflow\Registry ;
859
+ use Symfony\Component\Workflow\WorkflowInterface ;
833
860
// ...
834
861
835
- public function myAction(Registry $registry , BlogPost $post)
862
+ public function myAction(WorkflowInterface $blogPublishingWorkflow , BlogPost $post)
836
863
{
837
- $workflow = $registry->get($post);
838
-
839
- $title = $workflow
864
+ $title = $blogPublishingWorkflow
840
865
->getMetadataStore()
841
866
->getWorkflowMetadata()['title'] ?? 'Default title'
842
867
;
843
868
844
- $maxNumOfWords = $workflow
869
+ $maxNumOfWords = $blogPublishingWorkflow
845
870
->getMetadataStore()
846
871
->getPlaceMetadata('draft')['max_num_of_words'] ?? 500
847
872
;
848
873
849
- $aTransition = $workflow ->getDefinition()->getTransitions()[0];
850
- $priority = $workflow
874
+ $aTransition = $blogPublishingWorkflow ->getDefinition()->getTransitions()[0];
875
+ $priority = $blogPublishingWorkflow
851
876
->getMetadataStore()
852
877
->getTransitionMetadata($aTransition)['priority'] ?? 0
853
878
;
@@ -870,7 +895,7 @@ In a :ref:`flash message <flash-messages>` in your controller::
870
895
871
896
// $transition = ...; (an instance of Transition)
872
897
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)
874
899
$title = $workflow->getMetadataStore()->getMetadata('title', $transition);
875
900
$this->addFlash('info', "You have successfully applied the transition with title: '$title'");
876
901
0 commit comments