-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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 |
---|---|---|
|
@@ -21,4 +21,109 @@ You can install the component in 2 different ways: | |
|
||
For more information, see the code in the Git Repository. | ||
|
||
Usage | ||
----- | ||
|
||
The workflow component gives you an object oriented way to work with state machines. A state machine lets you | ||
define *places* (or *states*) and *transactions*. A transaction describes the action to get from one place to another. | ||
|
||
.. image:: /_images/components/workflow/states_transactions.png | ||
|
||
A set of places and ``Transaction``s form a ``Definition``. A ``Workflow`` needs a ``Definition`` and a way to write | ||
the states to the objects, ie a ``MarkingStoreInterface``. | ||
|
||
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. You should also the initial place with something like: |
||
Consider the following example for a blog post. A post can have places: 'draft', 'review', 'rejected', 'published'. You | ||
can define the workflow like this: | ||
|
||
$states = ['draft', 'review', 'rejected', 'published']; | ||
$transactions[] = new Transition('to_review', ['draft', 'rejected'], 'review'); | ||
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. This will not work. See symfony/symfony#19605 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. please add use statements for classes in the first code example you're using them. |
||
$transactions[] = new Transition('publish', 'review', 'published'); | ||
$transactions[] = new Transition('reject', 'review', 'rejected'); | ||
|
||
$definition = new Definition($states, $transactions); | ||
$definition->setInitialPlace('draft'); | ||
|
||
$marking = new ScalarMarkingStore('currentState'); | ||
$workflow = new Workflow($definition, $marking); | ||
|
||
The ``Workflow`` can now help you do decide what actions that are allowed on a blog post. | ||
|
||
|
||
$post = new \stdClass(); | ||
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. let's use an imaginary 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. Please prepend this example with 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. I would rather show the definition of the BlogPost class since it is important that there is a field called |
||
$post->currentState = null; | ||
$workflow->can($post, 'publish'); // False | ||
$workflow->can($post, 'to_draft'); // True | ||
|
||
// Update the currentState on the post | ||
try { | ||
$workflow->apply($post, 'to_review'); | ||
} catch (LogicException $e) { | ||
// ... | ||
} | ||
|
||
// See all the available transaction for the post in the current state | ||
$transactions = $workflow->getEnabledTransitions($post); | ||
|
||
|
||
|
||
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. please remove the empty lines (except from one) |
||
Using events | ||
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. "Using Events" (in our title case, all words are capitialized except from closed-class words) |
||
------------ | ||
|
||
To make your workflows even more powerful you could construct the ``Workflow`` object with an ``EventDispatcher``. You | ||
can now create event listeners to block transactions ie depending on the data in the blog post. The following events | ||
are dispatched: | ||
|
||
* workflow.guard | ||
* workflow.[workflow name].guard | ||
* workflow.[workflow name].guard.[transaction name] | ||
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. can you please put these event names in literal blocks? (double backticks) |
||
|
||
See example to make sure no blog post without title is moved to "review":: | ||
|
||
class BlogPostReviewListener implements EventSubscriberInterface | ||
{ | ||
public function guardReview(GuardEvent $event) | ||
{ | ||
$post = $event->getSubject(); | ||
$title = $post->getTitle(); | ||
|
||
if (empty($title)) { | ||
// Posts with no title should not be allowed | ||
$event->setBlocked(true); | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return array( | ||
'workflow.blogpost.guad.to_review' => array('guardReview'), | ||
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. I guess there is a typo here, isn't it ? - 'workflow.blogpost.guad.to_review'
+ 'workflow.blogpost.guard.to_review' Is it related to the 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. Thank you. No it is not related to the guard component. It is just named that way. I was also confused in the beginning. |
||
); | ||
} | ||
} | ||
|
||
With help from the ``EventDispatcher`` and the ``AuditTrailListener`` you could easily enable logging:: | ||
|
||
$logger = new PSR3Logger() | ||
$subscriber = new AuditTrailListener($logger); | ||
$dispatcher->addSubscriber($subscriber); | ||
|
||
Dumper | ||
------ | ||
|
||
To help you debug you could dump a representation of your workflow with the use of a ``DumperInterface``. Use the | ||
``GraphvizDumper`` to create a PNG image of the workflow defined above:: | ||
|
||
// dump-graph.php | ||
$dumper = new GraphvizDumper(); | ||
echo $dumper->dump($definition); | ||
|
||
.. code-block:: bash | ||
|
||
$ php dump-graph-php > out.dot | ||
$ dot -Tpng out.dot -o graph.png | ||
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. We should explain what is 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. You are very correct. I added a note about graphviz. Yes, is available for Linux, Solaris, Windows and Mac. |
||
|
||
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. Please remove the double empty line |
||
The result will look like this: | ||
|
||
.. image:: /_images/components/workflow/blogpost.png | ||
|
||
|
||
.. _Packagist: https://packagist.org/packages/symfony/workflow |
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.
I think you should replace "transaction" with "transition" everywhere.