Workflows and State Machines

Workflows and State Machines

Workflows

A workflow is a model of a process in your application. It may be the process of how a blog post goes from draft to review and publish. Another example is when a user submits a series of different forms to complete a task. Such processes are best kept away from your models and should be defined in configuration.

A definition of a workflow consists of places and actions to get from one place to another. The actions are called transitions. A workflow also needs to know each object’s position in the workflow. The marking store writes the current place to a property on the object.

Note

The terminology above is commonly used when discussing workflows and Petri nets

Examples

The simplest workflow looks like this. It contains two places and one transition.

../_images/simple.png

Workflows could be more complicated when they describe a real business case. The workflow below describes the process to fill in a job application.

../_images/job_application.png

When you fill in a job application in this example there are 4 to 7 steps depending on the job you are applying for. Some jobs require personality tests, logic tests and/or formal requirements to be answered by the user. Some jobs don’t. The GuardEvent is used to decide what next steps are allowed for a specific application.

By defining a workflow like this, there is an overview how the process looks like. The process logic is not mixed with the controllers, models or view. The order of the steps can be changed by changing the configuration only.

State Machines

A state machine is a subset of a workflow and its purpose is to hold a state of your model. The most important differences between them are:

  • Workflows can be in more than one place at the same time, whereas state machines can’t;
  • In order to apply a transition, workflows require that the object is in all the previous places of the transition, whereas state machines only require that the object is at least in one of those places.

Example

A pull request starts in an initial “start” state, then a state “test” for e.g. running tests on continuous integration stack. When this is finished, the pull request is in the “review” state, where contributors can require changes, reject or accept the pull request. At any time, you can also “update” the pull request, which will result in another continuous integration run.

../_images/pull_request.png

Below is the configuration for the pull request state machine.

  • YAML

    1. # config/packages/workflow.yaml
    2. framework:
    3. workflows:
    4. pull_request:
    5. type: 'state_machine'
    6. marking_store:
    7. type: 'method'
    8. property: 'currentPlace'
    9. supports:
    10. - App\Entity\PullRequest
    11. initial_marking: start
    12. places:
    13. - start
    14. - coding
    15. - test
    16. - review
    17. - merged
    18. - closed
    19. transitions:
    20. submit:
    21. from: start
    22. to: test
    23. update:
    24. from: [coding, test, review]
    25. to: test
    26. wait_for_review:
    27. from: test
    28. to: review
    29. request_change:
    30. from: review
    31. to: coding
    32. accept:
    33. from: review
    34. to: merged
    35. reject:
    36. from: review
    37. to: closed
    38. reopen:
    39. from: closed
    40. to: review
  • XML

    1. <!-- config/packages/workflow.xml -->
    2. <?xml version="1.0" encoding="UTF-8" ?>
    3. <container xmlns="http://symfony.com/schema/dic/services"
    4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    5. xmlns:framework="http://symfony.com/schema/dic/symfony"
    6. xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
    7. http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd"
    8. >
    9. <framework:config>
    10. <framework:workflow name="pull_request" type="state_machine">
    11. <framework:marking-store>
    12. <framework:type>method</framework:type>
    13. <framework:property>currentPlace</framework:property>
    14. </framework:marking-store>
    15. <framework:support>App\Entity\PullRequest</framework:support>
    16. <framework:initial_marking>start</framework:initial_marking>
    17. <framework:place>start</framework:place>
    18. <framework:place>coding</framework:place>
    19. <framework:place>test</framework:place>
    20. <framework:place>review</framework:place>
    21. <framework:place>merged</framework:place>
    22. <framework:place>closed</framework:place>
    23. <framework:transition name="submit">
    24. <framework:from>start</framework:from>
    25. <framework:to>test</framework:to>
    26. </framework:transition>
    27. <framework:transition name="update">
    28. <framework:from>coding</framework:from>
    29. <framework:from>test</framework:from>
    30. <framework:from>review</framework:from>
    31. <framework:to>test</framework:to>
    32. </framework:transition>
    33. <framework:transition name="wait_for_review">
    34. <framework:from>test</framework:from>
    35. <framework:to>review</framework:to>
    36. </framework:transition>
    37. <framework:transition name="request_change">
    38. <framework:from>review</framework:from>
    39. <framework:to>coding</framework:to>
    40. </framework:transition>
    41. <framework:transition name="accept">
    42. <framework:from>review</framework:from>
    43. <framework:to>merged</framework:to>
    44. </framework:transition>
    45. <framework:transition name="reject">
    46. <framework:from>review</framework:from>
    47. <framework:to>closed</framework:to>
    48. </framework:transition>
    49. <framework:transition name="reopen">
    50. <framework:from>closed</framework:from>
    51. <framework:to>review</framework:to>
    52. </framework:transition>
    53. </framework:workflow>
    54. </framework:config>
    55. </container>
  • PHP

    1. // config/packages/workflow.php
    2. $container->loadFromExtension('framework', [
    3. // ...
    4. 'workflows' => [
    5. 'pull_request' => [
    6. 'type' => 'state_machine',
    7. 'marking_store' => [
    8. 'type' => 'method',
    9. 'property' => 'currentPlace',
    10. ],
    11. 'supports' => ['App\Entity\PullRequest'],
    12. 'initial_marking' => 'start',
    13. 'places' => [
    14. 'start',
    15. 'coding',
    16. 'test',
    17. 'review',
    18. 'merged',
    19. 'closed',
    20. ],
    21. 'transitions' => [
    22. 'submit'=> [
    23. 'from' => 'start',
    24. 'to' => 'test',
    25. ],
    26. 'update'=> [
    27. 'from' => ['coding', 'test', 'review'],
    28. 'to' => 'test',
    29. ],
    30. 'wait_for_review'=> [
    31. 'from' => 'test',
    32. 'to' => 'review',
    33. ],
    34. 'request_change'=> [
    35. 'from' => 'review',
    36. 'to' => 'coding',
    37. ],
    38. 'accept'=> [
    39. 'from' => 'review',
    40. 'to' => 'merged',
    41. ],
    42. 'reject'=> [
    43. 'from' => 'review',
    44. 'to' => 'closed',
    45. ],
    46. 'reopen'=> [
    47. 'from' => 'start',
    48. 'to' => 'review',
    49. ],
    50. ],
    51. ],
    52. ],
    53. ]);

In a Symfony application using the default services.yaml configuration, you can get this state machine by injecting the Workflow registry service:

  1. // ...
  2. use App\Entity\PullRequest;
  3. use Symfony\Component\Workflow\Registry;
  4. class SomeService
  5. {
  6. private $workflows;
  7. public function __construct(Registry $workflows)
  8. {
  9. $this->workflows = $workflows;
  10. }
  11. public function someMethod(PullRequest $pullRequest)
  12. {
  13. $stateMachine = $this->workflows->get($pullRequest, 'pull_request');
  14. $stateMachine->apply($pullRequest, 'wait_for_review');
  15. // ...
  16. }
  17. // ...
  18. }

Symfony automatically creates a service for each workflow (Symfony\Component\Workflow\Workflow) or state machine (Symfony\Component\Workflow\StateMachine) you have defined in your configuration. This means that you can use workflow.pull_request or state_machine.pull_request respectively in your service definitions to access the proper service:

  1. // ...
  2. use App\Entity\PullRequest;
  3. use Symfony\Component\Workflow\StateMachine;
  4. class SomeService
  5. {
  6. private $stateMachine;
  7. public function __construct(StateMachine $stateMachine)
  8. {
  9. $this->stateMachine = $stateMachine;
  10. }
  11. public function someMethod(PullRequest $pullRequest)
  12. {
  13. $this->stateMachine->apply($pullRequest, 'wait_for_review', [
  14. 'log_comment' => 'My logging comment for the wait for review transition.',
  15. ]);
  16. // ...
  17. }
  18. // ...
  19. }

Automatic and Manual Validation

During cache warmup, Symfony validates the workflows and state machines that are defined in configuration files. If your workflows or state machines are defined programmatically instead of in a configuration file, you can validate them with the Symfony\Component\Workflow\Validator\WorkflowValidator and Symfony\Component\Workflow\Validator\StateMachineValidator:

  1. // ...
  2. use Symfony\Component\Workflow\Definition;
  3. use Symfony\Component\Workflow\StateMachine;
  4. use Symfony\Component\Workflow\Validator\StateMachineValidator;
  5. $states = ['created', 'activated', 'deleted'];
  6. $stateTransitions = [
  7. new Transition('activate', 'created', 'activated'),
  8. // This duplicate event "from" the "created" state is invalid
  9. new Transition('activate', 'created', 'deleted'),
  10. new Transition('delete', 'activated', 'deleted'),
  11. ];
  12. // No validation is done upon initialization
  13. $definition = new Definition($states, $stateTransitions);
  14. $validator = new StateMachineValidator();
  15. // Throws InvalidDefinitionException in case of an invalid definition
  16. $validator->validate($definition, 'My First StateMachine');

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.