@@ -294,32 +294,155 @@ what actions are allowed on a blog post::
294
294
// See a specific available transition for the post in the current state
295
295
$transition = $workflow->getEnabledTransition($post, 'publish');
296
296
297
- .. tip ::
297
+ Using Enums as Workflow Places
298
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
299
+
300
+ When using a state machine, you can use PHP backend enums as places in your workflows:
301
+
302
+ .. versionadded :: 7.4
303
+
304
+ The support for PHP backed enums as workflow places was introduced with Symfony 7.4.
305
+
306
+ First, define your enum with backed values::
307
+
308
+ // src/Enumeration/BlogPostStatus.php
309
+ namespace App\Enumeration;
310
+
311
+ enum BlogPostStatus: string
312
+ {
313
+ case Draft = 'draft';
314
+ case Reviewed = 'reviewed';
315
+ case Published = 'published';
316
+ case Rejected = 'rejected';
317
+ }
298
318
299
- In some specific cases, using PHP enums as places in your workflows might
300
- make sense and one can use them seamlessly with the Workflow component if
301
- they uses backed enumerations.
319
+ Then configure the workflow using the enum cases as places, initial marking,
320
+ and transitions:
321
+
322
+ .. configuration-block ::
302
323
303
- .. versionadded :: 7.4
324
+ .. code-block :: yaml
325
+
326
+ # config/packages/workflow.yaml
327
+ framework :
328
+ workflows :
329
+ blog_publishing :
330
+ type : ' workflow'
331
+ marking_store :
332
+ type : ' method'
333
+ property : ' status'
334
+ supports :
335
+ - App\Entity\BlogPost
336
+ initial_marking : !php/enum App\Enumeration\BlogPostStatus::Draft
337
+ places : !php/enum App\Enumeration\BlogPostStatus
338
+ transitions :
339
+ to_review :
340
+ from : !php/enum App\Enumeration\BlogPostStatus::Draft
341
+ to : !php/enum App\Enumeration\BlogPostStatus::Reviewed
342
+ publish :
343
+ from : !php/enum App\Enumeration\BlogPostStatus::Reviewed
344
+ to : !php/enum App\Enumeration\BlogPostStatus::Published
345
+ reject :
346
+ from : !php/enum App\Enumeration\BlogPostStatus::Reviewed
347
+ to : !php/enum App\Enumeration\BlogPostStatus::Rejected
304
348
305
- The support for PHP Backed enumerations as Workflow places was
306
- introduced with Symfony 7.4.
349
+ .. code-block :: xml
307
350
308
- First, define your enum with backed values::
351
+ <!-- config/packages/workflow.xml -->
352
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
353
+ <container xmlns =" http://symfony.com/schema/dic/services"
354
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
355
+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
356
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
357
+ https://symfony.com/schema/dic/services/services-1.0.xsd
358
+ http://symfony.com/schema/dic/symfony
359
+ https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
309
360
310
- // src/Enumeration/BlogPostStatus.php
311
- namespace App\Enumeration;
361
+ <framework : config >
362
+ <!-- or type="state_machine" -->
363
+ <framework : workflow name =" blog_publishing" type =" workflow" places =" App\Enumeration\BlogPostStatus::*" >
364
+ <framework : marking-store type =" single_state" >
365
+ <framework : argument >status</framework : argument >
366
+ </framework : marking-store >
367
+ <framework : support >App\Entity\BlogPost</framework : support >
368
+ <framework : initial-marking >draft</framework : initial-marking >
312
369
313
- enum BlogPostStatus: string
370
+ <framework : transition name =" to_review" >
371
+ <framework : from >draft</framework : from >
372
+ <framework : to >reviewed</framework : to >
373
+ </framework : transition >
374
+ <framework : transition name =" publish" >
375
+ <framework : from >reviewed</framework : from >
376
+ <framework : to >published</framework : to >
377
+ </framework : transition >
378
+ <framework : transition name =" reject" >
379
+ <framework : from >reviewed</framework : from >
380
+ <framework : to >rejected</framework : to >
381
+ </framework : transition >
382
+ </framework : workflow >
383
+ </framework : config >
384
+ </container >
385
+
386
+ .. code-block :: php
387
+
388
+ // config/packages/workflow.php
389
+ use App\Entity\BlogPost;
390
+ use App\Enumeration\BlogPostStatus;
391
+ use Symfony\Config\FrameworkConfig;
392
+
393
+ return static function (FrameworkConfig $framework): void {
394
+ $blogPublishing = $framework->workflows()->workflows('blog_publishing');
395
+ $blogPublishing
396
+ ->type('workflow')
397
+ ->supports([BlogPost::class])
398
+ ->initialMarking([BlogPostStatus::Draft]);
399
+
400
+ $blogPublishing->markingStore()
401
+ ->type('method')
402
+ ->property('status');
403
+
404
+ $blogPublishing->places(BlogPostStatus::cases());
405
+
406
+ $blogPublishing->transition()
407
+ ->name('to_review')
408
+ ->from(BlogPostStatus::Draft)
409
+ ->to([BlogPostStatus::Reviewed]);
410
+
411
+ $blogPublishing->transition()
412
+ ->name('publish')
413
+ ->from([BlogPostStatus::Reviewed])
414
+ ->to([BlogPostStatus::Published]);
415
+
416
+ $blogPublishing->transition()
417
+ ->name('reject')
418
+ ->from([BlogPostStatus::Reviewed])
419
+ ->to([BlogPostStatus::Rejected]);
420
+ };
421
+
422
+ The component will now transparently cast the enum to its backing value
423
+ when needed and vice-versa when working with your objects::
424
+
425
+ // src/Entity/BlogPost.php
426
+ namespace App\Entity;
427
+
428
+ class BlogPost
429
+ {
430
+ private BlogPostStatus $status;
431
+
432
+ public function getStatus(): BlogPostStatus
433
+ {
434
+ return $this->status;
435
+ }
436
+
437
+ public function setStatus(BlogPostStatus $status): void
314
438
{
315
- case Draft = 'draft';
316
- case Reviewed = 'reviewed';
317
- case Published = 'published';
318
- case Rejected = 'rejected';
439
+ $this->status = $status;
319
440
}
441
+ }
442
+
443
+ .. tip ::
320
444
321
- Then configure the workflow using the enum cases as places, initial
322
- marking, and transitions:
445
+ You can also use `glob patterns `_ of PHP constants and enums to list the places:
323
446
324
447
.. configuration-block ::
325
448
@@ -328,25 +451,14 @@ what actions are allowed on a blog post::
328
451
# config/packages/workflow.yaml
329
452
framework :
330
453
workflows :
331
- blog_publishing :
332
- type : ' workflow'
333
- marking_store :
334
- type : ' method'
335
- property : ' status'
336
- supports :
337
- - App\Entity\BlogPost
338
- initial_marking : !php/enum App\Enumeration\BlogPostStatus::Draft
339
- places : !php/enum App\Enumeration\BlogPostStatus
340
- transitions :
341
- to_review :
342
- from : !php/enum App\Enumeration\BlogPostStatus::Draft
343
- to : !php/enum App\Enumeration\BlogPostStatus::Reviewed
344
- publish :
345
- from : !php/enum App\Enumeration\BlogPostStatus::Reviewed
346
- to : !php/enum App\Enumeration\BlogPostStatus::Published
347
- reject :
348
- from : !php/enum App\Enumeration\BlogPostStatus::Reviewed
349
- to : !php/enum App\Enumeration\BlogPostStatus::Rejected
454
+ my_workflow_name :
455
+ # with constants:
456
+ places : ' App\Workflow\MyWorkflow::PLACE_*'
457
+
458
+ # with enums:
459
+ places : !php/enum App\Workflow\Places
460
+
461
+ # ...
350
462
351
463
.. code-block :: xml
352
464
@@ -361,26 +473,12 @@ what actions are allowed on a blog post::
361
473
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
362
474
363
475
<framework : config >
364
- <!-- or type="state_machine" -->
365
- <framework : workflow name =" blog_publishing" type =" workflow" places =" App\Enumeration\BlogPostStatus::*" >
366
- <framework : marking-store type =" single_state" >
367
- <framework : argument >status</framework : argument >
368
- </framework : marking-store >
369
- <framework : support >App\Entity\BlogPost</framework : support >
370
- <framework : initial-marking >draft</framework : initial-marking >
371
-
372
- <framework : transition name =" to_review" >
373
- <framework : from >draft</framework : from >
374
- <framework : to >reviewed</framework : to >
375
- </framework : transition >
376
- <framework : transition name =" publish" >
377
- <framework : from >reviewed</framework : from >
378
- <framework : to >published</framework : to >
379
- </framework : transition >
380
- <framework : transition name =" reject" >
381
- <framework : from >reviewed</framework : from >
382
- <framework : to >rejected</framework : to >
383
- </framework : transition >
476
+ <framework : workflow name =" my_workflow_name" type =" ..."
477
+ <!-- with constants: -->
478
+ places="App\Workflow\MyWorkflow::PLACE_*"
479
+ <!-- with enums: -->
480
+ places="App\Enumeration\BlogPostStatus::*">
481
+ <!-- ... -->
384
482
</framework : workflow >
385
483
</framework : config >
386
484
</container >
@@ -393,55 +491,17 @@ what actions are allowed on a blog post::
393
491
use Symfony\Config\FrameworkConfig;
394
492
395
493
return static function (FrameworkConfig $framework): void {
396
- $blogPublishing = $framework->workflows()->workflows('blog_publishing');
397
- $blogPublishing
398
- ->type('workflow')
399
- ->supports([BlogPost::class])
400
- ->initialMarking([BlogPostStatus::Draft]);
494
+ $blogPublishing = $framework->workflows()->workflows('my_workflow_name');
401
495
402
- $blogPublishing->markingStore()
403
- ->type('method')
404
- ->property('status');
496
+ // with constants:
497
+ $blogPublishing->places('App\Workflow\MyWorkflow::PLACE_*');
405
498
499
+ // with enums:
406
500
$blogPublishing->places(BlogPostStatus::cases());
407
501
408
- $blogPublishing->transition()
409
- ->name('to_review')
410
- ->from(BlogPostStatus::Draft)
411
- ->to([BlogPostStatus::Reviewed]);
412
-
413
- $blogPublishing->transition()
414
- ->name('publish')
415
- ->from([BlogPostStatus::Reviewed])
416
- ->to([BlogPostStatus::Published]);
417
-
418
- $blogPublishing->transition()
419
- ->name('reject')
420
- ->from([BlogPostStatus::Reviewed])
421
- ->to([BlogPostStatus::Rejected]);
502
+ // ...
422
503
};
423
504
424
- The component will now transparently cast the enum to its backing value
425
- when needed and vice-versa when working with your objects::
426
-
427
- // src/Entity/BlogPost.php
428
- namespace App\Entity;
429
-
430
- class BlogPost
431
- {
432
- private BlogPostStatus $status;
433
-
434
- public function getStatus(): BlogPostStatus
435
- {
436
- return $this->status;
437
- }
438
-
439
- public function setStatus(BlogPostStatus $status): void
440
- {
441
- $this->status = $status;
442
- }
443
- }
444
-
445
505
Using a multiple state marking store
446
506
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
447
507
@@ -1543,3 +1603,5 @@ Learn more
1543
1603
1544
1604
/workflow/workflow-and-state-machine
1545
1605
/workflow/dumping-workflows
1606
+
1607
+ .. _`glob patterns` : https://php.net/glob
0 commit comments