8000 minor #8654 Updated the Workflow articles to Symfony 4 (javiereguiluz… · Nyholm/symfony-docs@7699ca2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7699ca2

Browse files
committed
minor symfony#8654 Updated the Workflow articles to Symfony 4 (javiereguiluz, weaverryan)
This PR was merged into the master branch. Discussion ---------- Updated the Workflow articles to Symfony 4 The new file config paths are pending of this: symfony/recipes#256 Commits ------- 70dda7c adding use f9bb412 minor tweaks 8c540ce Removed an unneeded comment (Flex now includes Console by default) 5088d0e Lots of fixes 0261149 Fixed a typo 9690dfd Updated the Workflow articles to Symfony 4
2 parents 3bdb916 + 70dda7c commit 7699ca2

File tree

4 files changed

+87
-37
lines changed

4 files changed

+87
-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, you can dump the dot file with the
24+
``workflow:dump`` command:
2525

2626
.. code-block:: terminal
2727

workflow/state-machines.rst

Lines changed: 26 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,29 @@ 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 $workflows;
202+
203+
public function __constructor(Registry $workflows)
204+
{
205+
$this->workflows = $workflows;
206+
}
207+
208+
public function someMethod()
209+
{
210+
$stateMachine = $this->workflows->get('pull_request');
211+
// ...
212+
}
194213

195-
$stateMachine = $this->container->get('state_machine.pull_request');
214+
// ...
215+
}
196216

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

workflow/usage.rst

Lines changed: 51 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,46 @@ 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;
173+
use App\Entity\BlogPost;
174+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
175+
use Symfony\Component\Workflow\Exception\LogicException;
163176

164-
$workflow = $this->container->get('workflow.blog_publishing');
165-
$workflow->can($post, 'publish'); // False
166-
$workflow->can($post, 'to_review'); // True
177+
class BlogController extends Controller
178+
{
179+
public function edit(Registry $workflows)
180+
{
181+
$post = new BlogPost();
182+
$workflow = $workflows->get($post);
167183

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

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

178203
Using Events
179204
------------
@@ -250,7 +275,8 @@ order:
250275
* ``workflow.[workflow name].announce``
251276
* ``workflow.[workflow name].announce.[transition name]``
252277

253-
Here is an example how to enable logging for every time a the "blog_publishing" workflow leaves a place::
278+
Here is an example of how to enable logging for every time the ``blog_publishing``
279+
workflow leaves a place::
254280

255281
use Psr\Log\LoggerInterface;
256282
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

0 commit comments

Comments
 (0)
0