The Workflow Component

The Workflow Component

The Workflow component provides tools for managing a workflow or finite state machine.

Installation

  1. $ composer require symfony/workflow

Note

If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.

Creating a Workflow

The workflow component gives you an object oriented way to define a process or a life cycle that your object goes through. Each step or stage in the process is called a place. You do also define transitions that describe the action to get from one place to another.

../_images/states_transitions.png

A set of places and transitions creates a definition. A workflow needs a Definition and a way to write the states to the objects (i.e. an instance of a Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface).

Consider the following example for a blog post. A post can have one of a number of predefined statuses (draft, reviewed, rejected, published). In a workflow, these statuses are called places. You can define the workflow like this:

  1. use Symfony\Component\Workflow\DefinitionBuilder;
  2. use Symfony\Component\Workflow\MarkingStore\MethodMarkingStore;
  3. use Symfony\Component\Workflow\Transition;
  4. use Symfony\Component\Workflow\Workflow;
  5. $definitionBuilder = new DefinitionBuilder();
  6. $definition = $definitionBuilder->addPlaces(['draft', 'reviewed', 'rejected', 'published'])
  7. // Transitions are defined with a unique name, an origin place and a destination place
  8. ->addTransition(new Transition('to_review', 'draft', 'reviewed'))
  9. ->addTransition(new Transition('publish', 'reviewed', 'published'))
  10. ->addTransition(new Transition('reject', 'reviewed', 'rejected'))
  11. ->build()
  12. ;
  13. $singleState = true; // true if the subject can be in only one state at a given time
  14. $property = 'currentState'; // subject property name where the state is stored
  15. $marking = new MethodMarkingStore($singleState, $property);
  16. $workflow = new Workflow($definition, $marking);

The Workflow can now help you to decide what transitions (actions) are allowed on a blog post depending on what place (state) it is in. This will keep your domain logic in one place and not spread all over your application.

When you define multiple workflows you should consider using a Registry, which is an object that stores and provides access to different workflows. A registry will also help you to decide if a workflow supports the object you are trying to use it with:

  1. use Acme\Entity\BlogPost;
  2. use Acme\Entity\Newsletter;
  3. use Symfony\Component\Workflow\Registry;
  4. use Symfony\Component\Workflow\SupportStrategy\InstanceOfSupportStrategy;
  5. $blogPostWorkflow = ...;
  6. $newsletterWorkflow = ...;
  7. $registry = new Registry();
  8. $registry->addWorkflow($blogPostWorkflow, new InstanceOfSupportStrategy(BlogPost::class));
  9. $registry->addWorkflow($newsletterWorkflow, new InstanceOfSupportStrategy(Newsletter::class));

Usage

When you have configured a Registry with your workflows, you can retrieve a workflow from it and use it as follows:

  1. // ...
  2. // Consider that $blogPost is in place "draft" by default
  3. $blogPost = new BlogPost();
  4. $workflow = $registry->get($blogPost);
  5. $workflow->can($blogPost, 'publish'); // False
  6. $workflow->can($blogPost, 'to_review'); // True
  7. $workflow->apply($blogPost, 'to_review'); // $blogPost is now in place "reviewed"
  8. $workflow->can($blogPost, 'publish'); // True
  9. $workflow->getEnabledTransitions($blogPost); // $blogPost can perform transition "publish" or "reject"

Initialization

If the property of your object is null and you want to set it with the initial_marking from the configuration, you can call the `getMarking() method to initialize the object property:

  1. // ...
  2. $blogPost = new BlogPost();
  3. $workflow = $registry->get($blogPost);
  4. // initiate workflow
  5. $workflow->getMarking($blogPost);

Learn more

Read more about the usage of the Workflow component inside a Symfony application.

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