The symfony workflow component provides a mechanism for defining a life cycle or process which your objects move through, and checking if an object can move to a certain state, and updating the state of the object. This lightning talk introduces the component and how we can use it.
2. @MICHAELPEACOCK
• Consultant CTO
• Working with early stage SaaS businesses to build products and teams
• Part of the PHPNE Team
• Occasional speaker and author
3. WORKFLOW COMPONENT
A mechanism for defining a life cycle or process which your
objects move through, and checking if an object can move
to a certain state, and updating the state of the object.
4. DEFINITIONS
Term Meaning
Place State / status / location of your object in the workflow
Transition The action from moving from one place to another
Definition A set of places and transitions
Marking
Store
Mechanism for getting and setting the place on your
object
Registry Stores multiple workflows
16. REGISTRY
Registry lets you store a number of workflows, and
retrieve the appropriate one for a given object
17.
18. WORKFLOWS AS A STATE MACHINE
• Ensure an object can only be in one place at one time
• Introduce cyclic paths e.g. “unpublish”
19. EVENT LISTENING
SymfonyComponentEventDispatcherEventDispatcherInterface
Pass it to the workflow
Actions trigger three events, one for every workflow, one for the specific workflow and one for the
workflow and transition
For instance, when an object has completed a transition, the following events are raised:
• workflow.completed
• workflow.[workflow name].completed
• workflow.[workflow name].completed.[transition name]
20. EVENTS
workflow.guard Validate to see if the transition is allowed
workflow.leave Object is leaving a place
workflow.transition Object going through a specific transition
workflow.enter Object is about to enter a new place
workflow.entered Object has entered new place, and marking updated
workflow.completed Object has completed transition
workflow.announce Triggered for transitions which are now available to the object
A mechanism for defining a life cycle or process which your objects move through, and checking if an object can move to a certain state, and updating the state of the object. For example, if you have a blog, you might define a publishing workflow, if it’s a news website, you could even include editorial checks as part of your workflow.
Place = status
Transition = Moving from one place to another. Definition = Set of places and transitions
Marking store = setting and getting the place on your object
Registry = stores multiple workflows
Install via composer.
Lets use an example of a site which lets uses upload videos, as this is something I’ve started dabbling with for PHPNE videos.
Firstly, I’m going to create a laravel migration: statuses and videos. Different statuses
New (user has created a video record, but not yet uploaded)
Uploading. Uploaded. Transcoding. Transcoded. Draft. Published.
Video entity has a status.
Simple models - video and statuses
Transitions: Name, starting state, subsequent state. Definition is a set of places and transitions, so we tell it all the possible states for a video (ideally, dynamic from our DB), and the transitions. Transitions have a name, a starting place and a subsequent place. They show which places can transition to which other places.
Persisting state:
SingleStateMarkingStore
MultipleStateMarkingStore – objects which can be in multiple states
Implement MarkingStoreInterface
Marking store interface. Marking is a wrapper around the place name.
Example marker when you have a related status table.
Once we have definitions and a marking store, we pass them along to our workflow.
We can then check to see if an object is permitted to transition to a particular place, with the can method.
Apply will transition an object.
Registry.
Sample registry.
In order to use workflows as a state machine, you need to ensure an object can only be in one place at a time (so no multi place), and introduce cyclic paths which tend to exist in state machines but not in workflows. E.g. from published you might have ‘unpublish’
Event listening. Key actions trigger three different events, which allows you to listen for all workflow tasks, all tasks on a specific workflow and all actions related to a specific workflows transition.
Different events
Guarding. Use the event listener, listen for the guard events, and set the event to be blocked.