-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Added docs for Workflow component #6871
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
d0701f0
91867c2
69bca59
f99fbb2
e797f0c
5b2a029
196baf9
763950d
cead2f7
e0089c5
48de43d
805b237
f57ec14
03925ff
e6bdee6
c7464c7
83d26c1
6e7a35f
4415466
866b25a
dceebec
b959f8a
b45edf2
fefdb5f
7f0f5b0
c681283
86ecf0a
d002a8b
b0a8855
4e7cf11
3aa433d
4f277dc
2511c21
c9b1656
2cc2934
47dc11d
3250621
c0bd6da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,14 @@ | |
How to Use the Workflow | ||
======================= | ||
|
||
The workflow component gives you an object oriented way to work with state | ||
machines. A state machine lets you define *places* and *transitions*. | ||
A transition describes the action to get from one place to another. | ||
Using the workflow component will help you to keep your domain logic as | ||
configuration. Having domain logic in one place gives you a better overview | ||
and it is easier to maintain whenever the domain requirement changes since | ||
you do not have to edit all your controllers, twig templates and services. | ||
|
||
A workflow is a process or a lifecycle that your objects go through. Each | ||
step or stage in the process is called a *place*. You do also define *transitions* | ||
to that describes the action to get from one place to another. | ||
|
||
.. image:: /_images/components/workflow/states_transitions.png | ||
|
||
|
@@ -16,32 +21,53 @@ instance of a :class:`Symfony\\Component\\Workflow\\MarkingStore\\MarkingStoreIn | |
|
||
Consider the following example for a blog post. A post can have places: | ||
'draft', 'review', 'rejected', 'published'. You can define the workflow | ||
like this:: | ||
|
||
use Symfony\Component\Workflow\Definition; | ||
use Symfony\Component\Workflow\Transition; | ||
use Symfony\Component\Workflow\Workflow; | ||
use Symfony\Component\Workflow\MarkingStore\ScalarMarkingStore; | ||
|
||
$states = ['draft', 'review', 'rejected', 'published']; | ||
$transitions[] = new Transition('to_review', ['draft', 'rejected'], 'review'); | ||
$transitions[] = new Transition('publish', 'review', 'published'); | ||
$transitions[] = new Transition('reject', 'review', 'rejected'); | ||
|
||
$definition = new Definition($states, $transitions); | ||
$definition->setInitialPlace('draft'); | ||
|
||
$marking = new ScalarMarkingStore('currentState'); | ||
$workflow = new Workflow($definition, $marking); | ||
like this: | ||
|
||
.. code-block: yaml | ||
|
||
framework: | ||
workflows: | ||
blog_publishing: | ||
marking_store: | ||
type: scalar # or 'property_accessor' | ||
arguments: | ||
- 'currentPlace' | ||
supports: | ||
- AppBundle\Entity\BlogPost | ||
places: | ||
- draft | ||
- review | ||
- rejected | ||
- published | ||
transitions: | ||
to_review: | ||
from: draft | ||
to: review | ||
publish: | ||
from: review | ||
to: published | ||
reject: | ||
from: review | ||
to: rejected | ||
|
||
.. code-block: php | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: should be a double colon |
||
|
||
class BlogPost | ||
{ | ||
// This property is used by the marking store | ||
public $currentPlace; | ||
public $title; | ||
public $content | ||
} | ||
|
||
The ``Workflow`` can now help you to decide what actions that are allowed | ||
on a blog post. | ||
With this workflow named ``blog_publishing`` you can get help to decide | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "[...] named |
||
what actions that are allowed on a blog post. | ||
|
||
.. code-block:: php | ||
|
||
// ... | ||
$post = new \stdClass(); | ||
$post->currentState = null; | ||
$post = new BlogPost(); | ||
|
||
$workflow = $this->get('workflow.blog_publishing'); | ||
$workflow->can($post, 'publish'); // False | ||
$workflow->can($post, 'to_draft'); // True | ||
|
||
|
@@ -69,17 +95,12 @@ events are dispatched: | |
|
||
See example to make sure no blog post without title is moved to "review":: | ||
|
||
$marking = new ScalarMarkingStore('currentState'); | ||
$workflow = new Workflow($definition, $marking, $dispatcher, 'blogpost'); | ||
|
||
.. code-block:: php | ||
|
||
class BlogPostReviewListener implements EventSubscriberInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing use statements (for the |
||
{ | ||
public function guardReview(GuardEvent $event) | ||
{ | ||
$post = $event->getSubject(); | ||
$title = $post->getTitle(); | ||
$title = $post->title; | ||
|
||
if (empty($title)) { | ||
// Posts with no title should not be allowed | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Workflow component