Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
d0701f0
Added docs for Workflow component
Nyholm Aug 12, 2016
91867c2
Moved images
Nyholm Aug 12, 2016
69bca59
Fixes
Nyholm Aug 12, 2016
f99fbb2
Fixes
Nyholm Aug 12, 2016
e797f0c
Syntax error
Nyholm Aug 12, 2016
5b2a029
Cleanup and minor fixes
Nyholm Aug 16, 2016
196baf9
Separated docs for component
Nyholm Aug 16, 2016
763950d
Updating docs to using symfony services
Nyholm Aug 16, 2016
cead2f7
Added docs about registry
Nyholm Aug 16, 2016
e0089c5
Added placeholders
Nyholm Aug 16, 2016
48de43d
Added note about state machines
Nyholm Aug 16, 2016
805b237
Added example with twig
Nyholm Aug 16, 2016
f57ec14
Show Twig function workflow_transitions
Nyholm Aug 16, 2016
03925ff
syntax fix
Nyholm Aug 16, 2016
e6bdee6
Some syntax fixes and a better "why do we need this"
Nyholm Nov 7, 2016
c7464c7
typos
Nyholm Nov 7, 2016
83d26c1
toctree fix
Nyholm Nov 7, 2016
6e7a35f
Fixed typos and comments
Nyholm Nov 8, 2016
4415466
Added usage example on the component
Nyholm Nov 8, 2016
866b25a
Added example how to dump with Symfony
Nyholm Nov 8, 2016
dceebec
Added examples of workflows
Nyholm Nov 8, 2016
b959f8a
Updated state machine with an example
Nyholm Nov 8, 2016
b45edf2
simplify job_application
Nyholm Nov 8, 2016
fefdb5f
syntax
Nyholm Nov 9, 2016
7f0f5b0
Added comment about the service name
Nyholm Nov 9, 2016
c681283
Updated accoding to feedback
Nyholm Nov 9, 2016
86ecf0a
Added PHP config
Nyholm Nov 9, 2016
d002a8b
Added xml and PHP config
Nyholm Nov 9, 2016
b0a8855
Added workflow under guides
Nyholm Nov 9, 2016
4e7cf11
Removed the *why workflows* from the usage page.
Nyholm Nov 9, 2016
3aa433d
Documented support for DefinitionBuilder
Nyholm Nov 9, 2016
4f277dc
fixed typo
Nyholm Nov 9, 2016
2511c21
Merge pull request #1 from Nyholm/workflow-definition-builder
Nyholm Nov 9, 2016
c9b1656
Updated to support changes in https://github.com/symfony/symfony/pull…
Nyholm Nov 9, 2016
2cc2934
Updated twig example
Nyholm Nov 9, 2016
47dc11d
show how to configure the DI extension config
Nyholm Nov 11, 2016
3250621
Fixed typos
Nyholm Nov 11, 2016
c0bd6da
Use imperative instead of past tense.
Nyholm Nov 11, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Updating docs to using symfony services
  • Loading branch information
Nyholm committed Nov 7, 2016
commit 763950d37c1e1dade13e736f76fe2ee61f26abc9
83 changes: 52 additions & 31 deletions workflow/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Workflow component

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

Expand All @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"[...] named blog_publishing, you can [...]"

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

Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing use statements (for the GuardEvent, EventSubscriberInterface

{
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
Expand Down