WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit a6c4698

Browse files
add event-subscriber to throw flushed workflow events
1 parent 91b8b90 commit a6c4698

File tree

5 files changed

+156
-10
lines changed

5 files changed

+156
-10
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Sulu.
7+
*
8+
* (c) Sulu GmbH
9+
*
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
12+
*/
13+
14+
namespace Sulu\Bundle\ContentBundle\Content\Infrastructure\Symfony\Workflow;
15+
16+
use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface;
17+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19+
use Symfony\Component\Workflow\Event\Event;
20+
21+
/**
22+
* @internal
23+
*/
24+
class FlushedEventSubscriber implements EventSubscriberInterface
25+
{
26+
/**
27+
* @return string[]
28+
*/
29+
public static function getSubscribedEvents()
30+
{
31+
return [
32+
'workflow.completed' => 'onCompleted',
33+
];
34+
}
35+
36+
/**
37+
* @var EventDispatcherInterface
38+
*/
39+
private $eventDispatcher;
40+
41+
/**
42+
* @var mixed[]
43+
*/
44+
private $eventsToDispatch = [];
45+
46+
public function __construct(EventDispatcherInterface $eventDispatcher)
47+
{
48+
$this->eventDispatcher = $eventDispatcher;
49+
}
50+
51+
public function onCompleted(Event $event): void
52+
{
53+
if (WorkflowInterface::WORKFLOW_DEFAULT_NAME !== $event->getWorkflowName()) {
54+
return;
55+
}
56+
57+
$eventName = sprintf('workflow.%s.flushed', $event->getWorkflowName());
58+
59+
$this->eventsToDispatch[] = [$event, $eventName];
60+
$this->eventsToDispatch[] = [$event, sprintf('%s.%s', $eventName, $event->getTransition()->getName())];
61+
}
62+
63+
public function postFlush(): void
64+
{
65+
foreach ($this->eventsToDispatch as $item) {
66+
$this->eventDispatcher->dispatch($item[0], $item[1]);
67+
}
68+
69+
$this->eventsToDispatch = [];
70+
}
71+
72+
public function onClear(): void
73+
{
74+
$this->eventsToDispatch = [];
75+
}
76+
}

DependencyInjection/SuluContentExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\DependencyInjection\Extension\Extension;
2020
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
2121
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
22+
use Symfony\Component\HttpKernel\Bundle\Bundle;
2223

2324
class SuluContentExtension extends Extension implements PrependExtensionInterface
2425
{
@@ -54,6 +55,7 @@ public function load(array $configs, ContainerBuilder $container): void
5455
if ($container->hasParameter('kernel.bundles')) {
5556
// TODO FIXME add test here
5657
// @codeCoverageIgnoreStart
58+
/** @var array<string, Bundle> $bundles */
5759
$bundles = $container->getParameter('kernel.bundles');
5860

5961
if (\array_key_exists('SuluAutomationBundle', $bundles)) {

Resources/config/services.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,13 @@
166166
</service>
167167

168168
<service id="Sulu\Bundle\ContentBundle\Content\Application\ContentManager\ContentManagerInterface" alias="sulu_content.content_manager"/>
169+
170+
<service id="sulu_content.workflow.flushed_listener" class="Sulu\Bundle\ContentBundle\Content\Infrastructure\Symfony\Workflow\FlushedEventSubscriber">
171+
<argument type="service" id="event_dispatcher"/>
172+
173+
<tag name="kernel.event_subscriber"/>
174+
<tag name="doctrine.event_listener" event="postFlush"/>
175+
<tag name="doctrine.event_listener" event="onClear"/>
176+
</service>
169177
</services>
170178
</container>

Tests/Application/config/config.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
swiftmailer:
22
disable_delivery: true
33

4-
doctrine:
5-
orm:
6-
mappings:
7-
gedmo_tree:
8-
type: xml
9-
prefix: Gedmo\Tree\Entity
10-
dir: "%kernel.project_dir%/../../vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Entity"
11-
alias: GedmoTree
12-
is_bundle: false
13-
144
#sulu_core:
155
# content:
166
# structure:
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of Sulu.
7+
*
8+
* (c) Sulu GmbH
9+
*
10+
* This source file is subject to the MIT license that is bundled
11+
* with this source code in the file LICENSE.
12+
*/
13+
14+
namespace Sulu\Bundle\ContentBundle\Tests\Unit\Content\Infrastructure\Symfony\Workflow;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Prophecy\Argument;
18+
use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface;
19+
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Symfony\Workflow\FlushedEventSubscriber;
20+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Component\Workflow\Event\Event;
22+
use Symfony\Component\Workflow\Transition;
23+
24+
class FlushedEventSubscriberTest extends TestCase
25+
{
26+
protected function createFlushedEventSubscriber(EventDispatcherInterface $eventDispatcher): FlushedEventSubscriber
27+
{
28+
return new FlushedEventSubscriber($eventDispatcher);
29+
}
30+
31+
public function testOnCompleteAndFlush(): void
32+
{
33+
$transition = $this->prophesize(Transition::class);
34+
$transition->getName()->willReturn('published');
35+
36+
$event = $this->prophesize(Event::class);
37+
$event->getWorkflowName()->willReturn(WorkflowInterface::WORKFLOW_DEFAULT_NAME);
38+
$event->getTransition()->willReturn($transition->reveal());
39+
40+
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
41+
$eventDispatcher->dispatch(Argument::any())->shouldNotBeCalled();
42+
43+
$subscriber = $this->createFlushedEventSubscriber($eventDispatcher->reveal());
44+
$subscriber->onCompleted($event->reveal());
45+
46+
$eventName = sprintf('workflow.%s.flushed', WorkflowInterface::WORKFLOW_DEFAULT_NAME);
47+
$eventDispatcher->dispatch($event->reveal(), $eventName)->shouldBeCalledOnce();
48+
$eventDispatcher->dispatch($event->reveal(), $eventName . '.published')->shouldBeCalledOnce();
49+
50+
$subscriber->postFlush();
51+
}
52+
53+
public function testOnCompleteAndClear(): void
54+
{
55+
$transition = $this->prophesize(Transition::class);
56+
$transition->getName()->willReturn('published');
57+
58+
$event = $this->prophesize(Event::class);
59+
$event->getWorkflowName()->willReturn(WorkflowInterface::WORKFLOW_DEFAULT_NAME);
60+
$event->getTransition()->willReturn($transition->reveal());
61+
62+
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
63+
$eventDispatcher->dispatch(Argument::any())->shouldNotBeCalled();
64+
65+
$subscriber = $this->createFlushedEventSubscriber($eventDispatcher->reveal());
66+
$subscriber->onCompleted($event->reveal());
67+
68+
$subscriber->onClear();
69+
}
70+
}

0 commit comments

Comments
 (0)