Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5f4b3b

Browse files
committed
Fixed workflows
1 parent 9c21325 commit a5f4b3b

File tree

4 files changed

+253
-265
lines changed

4 files changed

+253
-265
lines changed

messenger/multiple_buses.rst

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -74,33 +74,25 @@ an **event bus**. The event bus could have zero or more subscribers.
7474
.. code-block:: php
7575
7676
// config/packages/messenger.php
77-
$container->loadFromExtension('framework', [
78-
'messenger' => [
79-
// The bus that is going to be injected when injecting MessageBusInterface
80-
'default_bus' => 'command.bus',
81-
'buses' => [
82-
'command.bus' => [
83-
'middleware' => [
84-
'validation',
85-
'doctrine_transaction',
86-
],
87-
],
88-
'query.bus' => [
89-
'middleware' => [
90-
'validation',
91-
],
92-
],
93-
'event.bus' => [
94-
// the 'allow_no_handlers' middleware allows to have no handler
95-
// configured for this bus without throwing an exception
96-
'default_middleware' => 'allow_no_handlers',
97-
'middleware' => [
98-
'validation',
99-
],
100-
],
101-
],
102-
],
103-
]);
77+
use Symfony\Config\FrameworkConfig;
78+
79+
return static function (FrameworkConfig $framework) {
80+
// The bus that is going to be injected when injecting MessageBusInterface
81+
$framework->messenger()->defaultBus('command.bus');
82+
83+
$commandBus = $framework->messenger()->bus('command.bus');
84+
$commandBus->middleware()->id('validation');
85+
$commandBus->middleware()->id('doctrine_transaction');
86+
87+
$queryBus = $framework->messenger()->bus('query.bus');
88+
$queryBus->middleware()->id('validation');
89+
90+
$eventBus = $framework->messenger()->bus('event.bus');
91+
// the 'allow_no_handlers' middleware allows to have no handler
92+
// configured for this bus without throwing an exception
93+
$eventBus->defaultMiddleware('allow_no_handlers');
94+
$eventBus->middleware()->id('validation');
95+
};
10496
10597
This will create three new services:
10698

workflow.rst

Lines changed: 115 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -120,43 +120,40 @@ like this:
120120
121121
// config/packages/workflow.php
122122
use App\Entity\BlogPost;
123-
124-
$container->loadFromExtension('framework', [
125-
'workflows' => [
126-
'blog_publishing' => [
127-
'type' => 'workflow', // or 'state_machine'
128-
'audit_trail' => [
129-
'enabled' => true
130-
],
131-
'marking_store' => [
132-
'type' => 'method',
133-
'property' => 'currentPlace',
134-
],
135-
'supports' => [BlogPost::class],
136-
'initial_marking' => 'draft',
137-
'places' => [
138-
'draft',
139-
'reviewed',
140-
'rejected',
141-
'published',
142-
],
143-
'transitions' => [
144-
'to_review' => [
145-
'from' => 'draft',
146-
'to' => 'reviewed',
147-
],
148-
'publish' => [
149-
'from' => 'reviewed',
150-
'to' => 'published',
151-
],
152-
'reject' => [
153-
'from' => 'reviewed',
154-
'to' => 'rejected',
155-
],
156-
],
157-
],
158-
],
159-
]);
123+
use Symfony\Config\FrameworkConfig;
124+
125+
return static function (FrameworkConfig $framework) {
126+
$blogPublishing = $framework->workflows()->workflows('blog_publishing');
127+
$blogPublishing
128+
->type('workflow') // or 'state_machine'
129+
->supports([BlogPost::class])
130+
->initialMarking(['draft']);
131+
132+
$blogPublishing->auditTrail()->enabled(true);
133+
$blogPublishing->markingStore()
134+
->type('method')
135+
->property('currentPlace');
136+
137+
$blogPublishing->place()->name('draft');
138+
$blogPublishing->place()->name('reviewed');
139+
$blogPublishing->place()->name('rejected');
140+
$blogPublishing->place()->name('published');
141+
142+
$blogPublishing->transition()
143+
->name('to_review')
144+
->from(['draft'])
145+
->to(['reviewed']);
146+
147+
$blogPublishing->transition()
148+
->name('publish')
149+
->from(['reviewed'])
150+
->to(['published']);
151+
152+
$blogPublishing->transition()
153+
->name('reject')
154+
->from(['reviewed'])
155+
->to(['rejected']);
156+
};
160157
161158
.. tip::
162159

@@ -553,23 +550,25 @@ to :ref:`Guard events <workflow-usage-guard-events>`, which are always fired:
553550
.. code-block:: php
554551
555552
// config/packages/workflow.php
556-
$container->loadFromExtension('framework', [
553+
use Symfony\Config\FrameworkConfig;
554+
555+
return static function (FrameworkConfig $framework) {
557556
// ...
558-
'workflows' => [
559-
'blog_publishing' => [
560-
// you can pass one or more event names
561-
'events_to_dispatch' => [
562-
'workflow.leave',
563-
'workflow.completed',
564-
],
565-
566-
// pass an empty array to not dispatch any event
567-
'events_to_dispatch' => [],
568-
569-
// ...
570-
],
571-
],
572-
]);
557+
558+
$blogPublishing = $framework->workflows()->workflows('blog_publishing');
559+
560+
// ...
561+
// you can pass one or more event names
562+
$blogPublishing->eventsToDispatch([
563+
'workflow.leave',
564+
'workflow.completed',
565+
]);
566+
567+
// pass an empty array to not dispatch any event
568+
$blogPublishing->eventsToDispatch([]);
569+
570+
// ...
571+
};
573572
574573
You can also disable a specific event from being fired when applying a transition::
575574

@@ -731,36 +730,33 @@ transition. The value of this option is any valid expression created with the
731730
.. code-block:: php
732731
733732
// config/packages/workflow.php
734-
use App\Entity\BlogPost;
735-
736-
$container->loadFromExtension('framework', [
737-
'workflows' => [
738-
'blog_publishing' => [
739-
// ... previous configuration
740-
741-
'transitions' => [
742-
'to_review' => [
743-
// the transition is allowed only if the current user has the ROLE_REVIEWER role.
744-
'guard' => 'is_granted("ROLE_REVIEWER")',
745-
'from' => 'draft',
746-
'to' => 'reviewed',
747-
],
748-
'publish' => [
749-
// or "is_anonymous", "is_remember_me", "is_fully_authenticated", "is_granted"
750-
'guard' => 'is_authenticated',
751-
'from' => 'reviewed',
752-
'to' => 'published',
753-
],
754-
'reject' => [
755-
// or any valid expression language with "subject" referring to the post
756-
'guard' => 'is_granted("ROLE_ADMIN") and subject.isStatusReviewed()',
757-
'from' => 'reviewed',
758-
'to' => 'rejected',
759-
],
760-
],
761-
],
762-
],
763-
]);
733+
use Symfony\Config\FrameworkConfig;
734+
735+
return static function (FrameworkConfig $framework) {
736+
$blogPublishing = $framework->workflows()->workflows('blog_publishing');
737+
// ... previous configuration
738+
739+
$blogPublishing->transition()
740+
->name('to_review')
741+
// the transition is allowed only if the current user has the ROLE_REVIEWER role.
742+
->guard('is_granted("ROLE_REVIEWER")')
743+
->from(['draft'])
744+
->to(['reviewed']);
745+
746+
$blogPublishing->transition()
747+
->name('publish')
748+
// or "is_anonymous", "is_remember_me", "is_fully_authenticated", "is_granted"
749+
->guard('is_authenticated')
750+
->from(['reviewed'])
751+
->to(['published']);
752+
753+
$blogPublishing->transition()
754+
->name('reject')
755+
// or any valid expression language with "subject" referring to the post
756+
->guard('is_granted("ROLE_ADMIN") and subject.isStatusReviewed()')
757+
->from(['reviewed'])
758+
->to(['rejected']);
759+
};
764760
765761
You can also use transition blockers to block and return a user-friendly error
766762
message when you stop a transition from happening.
@@ -945,42 +941,43 @@ be only the title of the workflow or very complex objects:
945941
.. code-block:: php
946942
947943
// config/packages/workflow.php
948-
$container->loadFromExtension('framework', [
944+
use Symfony\Config\FrameworkConfig;
945+
946+
return static function (FrameworkConfig $framework) {
947+
$blogPublishing = $framework->workflows()->workflows('blog_publishing');
948+
// ... previous configuration
949+
950+
$blogPublishing->metadata([
951+
'title' => 'Blog Publishing Workflow'
952+
]);
953+
949954
// ...
950-
'workflows' => [
951-
'blog_publishing' => [
952-
'metadata' => [
953-
'title' => 'Blog Publishing Workflow',
954-
],
955-
// ...
956-
'places' => [
957-
'draft' => [
958-
'metadata' => [
959-
'max_num_of_words' => 500,
960-
],
961-
],
962-
// ...
963-
],
964-
'transitions' => [
965-
'to_review' => [
966-
'from' => 'draft',
967-
'to' => 'review',
968-
'metadata' => [
969-
'priority' => 0.5,
970-
],
971-
],
972-
'publish' => [
973-
'from' => 'reviewed',
974-
'to' => 'published',
975-
'metadata' => [
976-
'hour_limit' => 20,
977-
'explanation' => 'You can not publish after 8 PM.',
978-
],
979-
],
980-
],
981-
],
982-
],
983-
]);
955+
956+
$blogPublishing->place()
957+
->name('draft')
958+
->metadata([
959+
'max_num_of_words' => 500,
960+
]);
961+
962+
// ...
963+
964+
$blogPublishing->transition()
965+
->name('to_review')
966+
->from(['draft'])
967+
->to(['reviewed'])
968+
->metadata([
969+
'priority' => 0.5,
970+
]);
971+
972+
$blogPublishing->transition()
973+
->name('publish')
974+
->from(['reviewed'])
975+
->to(['published'])
976+
->metadata([
977+
'hour_limit' => 20,
978+
'explanation' => 'You can not publish after 8 PM.',
979+
]);
980+
};
984981
985982
Then you can access this metadata in your controller as follows::
986983

0 commit comments

Comments
 (0)