-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Updated the Workflow articles to Symfony 4 #8654
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 2 commits
9690dfd
0261149
5088d0e
8c540ce
f9bb412
70dda7c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
.. index:: | ||
single: Workflow; Usage | ||
|
||
How to Use the Workflow | ||
======================= | ||
How to Create and Use Workflows | ||
=============================== | ||
|
||
Before creating your first workflow, execute this command to install the | ||
:doc:`Workflow component </components/workflow>` in your application: | ||
|
||
.. code-block:: terminal | ||
|
||
$ composer require workflow | ||
|
||
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* | ||
|
@@ -14,15 +21,15 @@ A set of places and transitions creates a **definition**. A workflow needs | |
a ``Definition`` and a way to write the states to the objects (i.e. an | ||
instance of a :class:`Symfony\\Component\\Workflow\\MarkingStore\\MarkingStoreInterface`.) | ||
|
||
Consider the following example for a blog post. A post can have places: | ||
'draft', 'review', 'rejected', 'published'. You can define the workflow | ||
Consider the following example for a blog post that can have these places: | ||
``draft``, ``review``, ``rejected``, ``published``. You can define the workflow | ||
like this: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/config.yml | ||
# config/packages/workflow.yaml | ||
framework: | ||
workflows: | ||
blog_publishing: | ||
|
@@ -51,7 +58,7 @@ like this: | |
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/config.xml --> | ||
<!-- config/packages/workflow.xml --> | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
|
@@ -98,7 +105,7 @@ like this: | |
|
||
.. code-block:: php | ||
|
||
// app/config/config.php | ||
// config/packages/workflow.php | ||
|
||
$container->loadFromExtension('framework', array( | ||
// ... | ||
|
@@ -152,28 +159,43 @@ like this: | |
|
||
.. tip:: | ||
|
||
The ``type`` (default value ``single_state``) and ``arguments`` (default value ``marking``) | ||
attributes of the ``marking_store`` option are optional. If omitted, their default values | ||
will be used. | ||
The ``type`` (default value ``single_state``) and ``arguments`` (default | ||
value ``marking``) attributes of the ``marking_store`` option are optional. | ||
If omitted, their default values will be used. | ||
|
||
With this workflow named ``blog_publishing``, you can get help to decide | ||
what actions are allowed on a blog post:: | ||
With this workflow named ``blog_publishing``, you can now decide what actions | ||
are allowed on a blog post. For example, inside a controller of an application | ||
using the :ref:`default services.yaml configuration <service-container-services-load-example>`, | ||
you can get the workflow by injecting the Workflow registry service:: | ||
|
||
$post = new \App\Entity\BlogPost(); | ||
// ... | ||
use Symfony\Component\Workflow\Registry; | ||
|
||
$workflow = $this->container->get('workflow.blog_publishing'); | ||
$workflow->can($post, 'publish'); // False | ||
$workflow->can($post, 'to_review'); // True | ||
class BlogController | ||
{ | ||
public function edit(Registry $workflows) | ||
{ | ||
$post = new \App\Entity\BlogPost(); | ||
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 add a use statement for the class instead of using the FQCN here. |
||
$workflow = $workflows->get($post); | ||
|
||
// Update the currentState on the post | ||
try { | ||
$workflow->apply($post, 'to_review'); | ||
} catch (LogicException $e) { | ||
// ... | ||
} | ||
// if there are multiple workflows for the same class, | ||
// pass the workflow name as the second argument | ||
// $workflow = $workflows->get($post, 'blog_publishing'); | ||
|
||
// See all the available transition for the post in the current state | ||
$transitions = $workflow->getEnabledTransitions($post); | ||
$workflow->can($post, 'publish'); // False | ||
$workflow->can($post, 'to_review'); // True | ||
|
||
// Update the currentState on the post | ||
try { | ||
$workflow->apply($post, 'to_review'); | ||
} catch (LogicException $e) { | ||
// ... | ||
} | ||
|
||
// See all the available transition for the post in the current state | ||
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. transitions |
||
$transitions = $workflow->getEnabledTransitions($post); | ||
} | ||
} | ||
|
||
Using Events | ||
------------ | ||
|
@@ -250,7 +272,8 @@ order: | |
* ``workflow.[workflow name].announce`` | ||
* ``workflow.[workflow name].announce.[transition name]`` | ||
|
||
Here is an example how to enable logging for every time a the "blog_publishing" workflow leaves a place:: | ||
Here is an example how to enable logging for every time the ``blog_publishing`` | ||
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. [...] an example of [...] |
||
workflow leaves a place:: | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
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 would not do that inside the constructor. You'll never know when your service is initialized and if the registry is ready at this point which could lead to undesired side effects.