Skip to content

Commit 9690dfd

Browse files
committed
Updated the Workflow articles to Symfony 4
1 parent e92386c commit 9690dfd

File tree

4 files changed

+78
-37
lines changed

4 files changed

+78
-37
lines changed

workflow.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,14 @@ By defining a workflow like this, there is an overview how the process looks lik
4141
logic is not mixed with the controllers, models or view. The order of the steps can be changed
4242
by changing the configuration only.
4343

44+
Learn more
45+
----------
46+
4447
.. toctree::
4548
:maxdepth: 1
46-
:glob:
4749

48-
workflow/*
50+
workflow/usage
51+
workflow/state-machines
52+
workflow/dumping-workflows
4953

5054
.. _Petri nets: https://en.wikipedia.org/wiki/Petri_net

workflow/dumping-workflows.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
How to Dump Workflows
55
=====================
66

7-
To help you debug your workflows, you can dump a representation of your workflow with
8-
the use of a ``DumperInterface``. Use the ``GraphvizDumper`` to create a
7+
To help you debug your workflows, you can dump a representation of your workflow
8+
with the use of a ``DumperInterface``. Use the ``GraphvizDumper`` to create a
99
PNG image of the workflow defined above::
1010

1111
// dump-graph.php
@@ -20,8 +20,8 @@ The result will look like this:
2020

2121
.. image:: /_images/components/workflow/blogpost.png
2222

23-
If you have configured your workflow with the Symfony framework, you may dump the dot file
24-
with the ``WorkflowDumpCommand``:
23+
Inside a Symfony application with both Workflow and Console support, you can
24+
dump the dot file with the ``workflow:dump`` command:
2525

2626
.. code-block:: terminal
2727

workflow/state-machines.rst

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Below is the configuration for the pull request state machine.
2828

2929
.. code-block:: yaml
3030
31-
# app/config/config.yml
31+
# config/packages/workflow.yaml
3232
framework:
3333
workflows:
3434
pull_request:
@@ -67,7 +67,7 @@ Below is the configuration for the pull request state machine.
6767
6868
.. code-block:: xml
6969
70-
<!-- app/config/config.xml -->
70+
<!-- # config/packages/workflow.xml -->
7171
<?xml version="1.0" encoding="utf-8" ?>
7272
<container xmlns="http://symfony.com/schema/dic/services"
7373
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -140,8 +140,7 @@ Below is the configuration for the pull request state machine.
140140
141141
.. code-block:: php
142142
143-
// app/config/config.php
144-
143+
// # config/packages/workflow.php
145144
$container->loadFromExtension('framework', array(
146145
// ...
147146
'workflows' => array(
@@ -190,8 +189,23 @@ Below is the configuration for the pull request state machine.
190189
),
191190
));
192191
193-
You can now use this state machine by getting the ``state_machine.pull_request`` service::
192+
In a Symfony application using the
193+
:ref:`default services.yaml configuration <service-container-services-load-example>`,
194+
you can get this state machine by injecting the Workflow registry service::
195+
196+
// ...
197+
use Symfony\Component\Workflow\Registry;
198+
199+
class SomeService
200+
{
201+
private $stateMachine;
202+
203+
public function __constructor(Registry $workflows)
204+
{
205+
$this->stateMachine = $workflows->get('pull_request');
206+
}
194207

195-
$stateMachine = $this->container->get('state_machine.pull_request');
208+
// ...
209+
}
196210

197211
.. _Petri net: https://en.wikipedia.org/wiki/Petri_net

workflow/usage.rst

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
.. index::
22
single: Workflow; Usage
33

4-
How to Use the Workflow
5-
=======================
4+
How to Create and Use Workflows
5+
===============================
6+
7+
Before creating your first workflow, execute this command to install the
8+
:doc:`Workflow component </components/workflow>` in your application:
9+
10+
.. code-block:: terminal
11+
12+
$ composer require workflow
613
714
A workflow is a process or a lifecycle that your objects go through. Each
815
step or stage in the process is called a *place*. You do also define *transitions*
@@ -14,15 +21,15 @@ A set of places and transitions creates a **definition**. A workflow needs
1421
a ``Definition`` and a way to write the states to the objects (i.e. an
1522
instance of a :class:`Symfony\\Component\\Workflow\\MarkingStore\\MarkingStoreInterface`.)
1623

17-
Consider the following example for a blog post. A post can have places:
18-
'draft', 'review', 'rejected', 'published'. You can define the workflow
24+
Consider the following example for a blog post that can have these places:
25+
``draft``, ``review``, ``rejected``, ``published``. You can define the workflow
1926
like this:
2027

2128
.. configuration-block::
2229

2330
.. code-block:: yaml
2431
25-
# app/config/config.yml
32+
# config/packages/workflow.yaml
2633
framework:
2734
workflows:
2835
blog_publishing:
@@ -51,7 +58,7 @@ like this:
5158
5259
.. code-block:: xml
5360
54-
<!-- app/config/config.xml -->
61+
<!-- config/packages/workflow.xml -->
5562
<?xml version="1.0" encoding="utf-8" ?>
5663
<container xmlns="http://symfony.com/schema/dic/services"
5764
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -98,7 +105,7 @@ like this:
98105
99106
.. code-block:: php
100107
101-
// app/config/config.php
108+
// config/packages/workflow.php
102109
103110
$container->loadFromExtension('framework', array(
104111
// ...
@@ -152,28 +159,43 @@ like this:
152159

153160
.. tip::
154161

155-
The ``type`` (default value ``single_state``) and ``arguments`` (default value ``marking``)
156-
attributes of the ``marking_store`` option are optional. If omitted, their default values
157-
will be used.
162+
The ``type`` (default value ``single_state``) and ``arguments`` (default
163+
value ``marking``) attributes of the ``marking_store`` option are optional.
164+
If omitted, their default values will be used.
158165

159-
With this workflow named ``blog_publishing``, you can get help to decide
160-
what actions are allowed on a blog post::
166+
With this workflow named ``blog_publishing``, you can now decide what actions
167+
are allowed on a blog post. For example, inside a controller of an application
168+
using the :ref:`default services.yaml configuration <service-container-services-load-example>`,
169+
you can get the workflow by injecting the Workflow registry service::
161170

162-
$post = new \App\Entity\BlogPost();
171+
// ...
172+
use Symfony\Component\Workflow\Registry;
163173

164-
$workflow = $this->container->get('workflow.blog_publishing');
165-
$workflow->can($post, 'publish'); // False
166-
$workflow->can($post, 'to_review'); // True
174+
class BlogController
175+
{
176+
public function edit(Registry $workflows)
177+
{
178+
$post = new \App\Entity\BlogPost();
179+
$workflow = $workflows->get($post);
167180

168-
// Update the currentState on the post
169-
try {
170-
$workflow->apply($post, 'to_review');
171-
} catch (LogicException $e) {
172-
// ...
173-
}
181+
// if there are multiple workflows for the same class,
182+
// pass the workflow name as the second argument
183+
// $workflow = $workflows->get($post, 'blog_publishing');
174184

175-
// See all the available transition for the post in the current state
176-
$transitions = $workflow->getEnabledTransitions($post);
185+
$workflow->can($post, 'publish'); // False
186+
$workflow->can($post, 'to_review'); // True
187+
188+
// Update the currentState on the post
189+
try {
190+
$workflow->apply($post, 'to_review');
191+
} catch (LogicException $e) {
192+
// ...
193+
}
194+
195+
// See all the available transition for the post in the current state
196+
$transitions = $workflow->getEnabledTransitions($post);
197+
}
198+
}
177199

178200
Using Events
179201
------------
@@ -250,7 +272,8 @@ order:
250272
* ``workflow.[workflow name].announce``
251273
* ``workflow.[workflow name].announce.[transition name]``
252274

253-
Here is an example how to enable logging for every time a the "blog_publishing" workflow leaves a place::
275+
Here is an example how to enable logging for every time the ``blog_publishing``
276+
workflow leaves a place::
254277

255278
use Psr\Log\LoggerInterface;
256279
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

0 commit comments

Comments
 (0)