Skip to content

Commit ef045c6

Browse files
committed
Tweaks
1 parent 5fe96de commit ef045c6

File tree

1 file changed

+161
-99
lines changed

1 file changed

+161
-99
lines changed

workflow.rst

Lines changed: 161 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -294,32 +294,155 @@ what actions are allowed on a blog post::
294294
// See a specific available transition for the post in the current state
295295
$transition = $workflow->getEnabledTransition($post, 'publish');
296296

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+
}
298318

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::
302323

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
304348
305-
The support for PHP Backed enumerations as Workflow places was
306-
introduced with Symfony 7.4.
349+
.. code-block:: xml
307350
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">
309360
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>
312369
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
314438
{
315-
case Draft = 'draft';
316-
case Reviewed = 'reviewed';
317-
case Published = 'published';
318-
case Rejected = 'rejected';
439+
$this->status = $status;
319440
}
441+
}
442+
443+
.. tip::
320444

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:
323446

324447
.. configuration-block::
325448

@@ -328,25 +451,14 @@ what actions are allowed on a blog post::
328451
# config/packages/workflow.yaml
329452
framework:
330453
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+
# ...
350462
351463
.. code-block:: xml
352464
@@ -361,26 +473,12 @@ what actions are allowed on a blog post::
361473
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
362474
363475
<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+
<!-- ... -->
384482
</framework:workflow>
385483
</framework:config>
386484
</container>
@@ -393,55 +491,17 @@ what actions are allowed on a blog post::
393491
use Symfony\Config\FrameworkConfig;
394492
395493
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');
401495
402-
$blogPublishing->markingStore()
403-
->type('method')
404-
->property('status');
496+
// with constants:
497+
$blogPublishing->places('App\Workflow\MyWorkflow::PLACE_*');
405498
499+
// with enums:
406500
$blogPublishing->places(BlogPostStatus::cases());
407501
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+
// ...
422503
};
423504
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-
445505
Using a multiple state marking store
446506
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
447507

@@ -1543,3 +1603,5 @@ Learn more
15431603

15441604
/workflow/workflow-and-state-machine
15451605
/workflow/dumping-workflows
1606+
1607+
.. _`glob patterns`: https://php.net/glob

0 commit comments

Comments
 (0)