Skip to content

Commit a9cdbf5

Browse files
author
Mathieu Lemoine
committed
Reduced dependency to voryx/Thruway
1 parent db28199 commit a9cdbf5

14 files changed

+425
-150
lines changed

AbstractAsyncEventDispatcher.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Enqueue\AsyncEventDispatcher;
4+
5+
use Symfony\Component\EventDispatcher\Event;
6+
use Symfony\Component\EventDispatcher\EventDispatcher;
7+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
8+
use Symfony\Contracts\EventDispatcher\Event as ContractEvent;
9+
10+
abstract class AbstractAsyncEventDispatcher extends EventDispatcher implements EventDispatcherInterface
11+
{
12+
/**
13+
* @var EventDispatcherInterface
14+
*/
15+
protected $trueEventDispatcher;
16+
17+
/**
18+
* @var AsyncListener
19+
*/
20+
protected $asyncListener;
21+
22+
/**
23+
* @param EventDispatcherInterface $trueEventDispatcher
24+
* @param AsyncListener $asyncListener
25+
*/
26+
public function __construct(EventDispatcherInterface $trueEventDispatcher, AsyncListener $asyncListener)
27+
{
28+
$this->trueEventDispatcher = $trueEventDispatcher;
29+
$this->asyncListener = $asyncListener;
30+
}
31+
32+
/**
33+
* This method dispatches only those listeners that were marked as async.
34+
*
35+
* @param string $eventName
36+
* @param ContractEvent|Event|null $event
37+
*/
38+
public function dispatchAsyncListenersOnly($eventName, $event = null)
39+
{
40+
try {
41+
$this->asyncListener->syncMode($eventName);
42+
43+
$this->parentDispatch($event, $eventName);
44+
} finally {
45+
$this->asyncListener->resetSyncMode();
46+
}
47+
}
48+
49+
abstract protected function parentDispatch($event, $eventName);
50+
}

AbstractAsyncListener.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Enqueue\AsyncEventDispatcher;
4+
5+
use Interop\Queue\Context;
6+
use Interop\Queue\Queue;
7+
use Symfony\Component\EventDispatcher\Event;
8+
9+
abstract class AbstractAsyncListener
10+
{
11+
/**
12+
* @var Context
13+
*/
14+
protected $context;
15+
16+
/**
17+
* @var Registry
18+
*/
19+
protected $registry;
20+
21+
/**
22+
* @var Queue
23+
*/
24+
protected $eventQueue;
25+
26+
/**
27+
* @var bool
28+
*/
29+
protected $syncMode;
30+
31+
/**
32+
* @param Context $context
33+
* @param Registry $registry
34+
* @param Queue|string $eventQueue
35+
*/
36+
public function __construct(Context $context, Registry $registry, $eventQueue)
37+
{
38+
$this->context = $context;
39+
$this->registry = $registry;
40+
$this->eventQueue = $eventQueue instanceof Queue ? $eventQueue : $context->createQueue($eventQueue);
41+
}
42+
43+
public function resetSyncMode()
44+
{
45+
$this->syncMode = [];
46+
}
47+
48+
/**
49+
* @param string $eventName
50+
*/
51+
public function syncMode($eventName)
52+
{
53+
$this->syncMode[$eventName] = true;
54+
}
55+
56+
/**
57+
* @param string $eventName
58+
*
59+
* @return bool
60+
*/
61+
public function isSyncMode($eventName)
62+
{
63+
return isset($this->syncMode[$eventName]);
64+
}
65+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Enqueue\AsyncEventDispatcher;
4+
5+
use Interop\Queue\Context;
6+
use Interop\Queue\Message;
7+
use Symfony\Component\EventDispatcher\Event;
8+
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
9+
use Symfony\Contracts\EventDispatcher\Event as ContractEvent;
10+
11+
abstract class AbstractPhpSerializerEventTransformer
12+
{
13+
/**
14+
* @var Context
15+
*/
16+
protected $context;
17+
18+
/**
19+
* @param Context $context
20+
*/
21+
public function __construct(Context $context)
22+
{
23+
$this->context = $context;
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function toEvent($eventName, Message $message)
30+
{
31+
return unserialize($message->getBody());
32+
}
33+
}

AsyncEventDispatcher.php

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,69 @@
33
namespace Enqueue\AsyncEventDispatcher;
44

55
use Symfony\Component\EventDispatcher\Event;
6-
use Symfony\Component\EventDispatcher\EventDispatcher;
7-
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
6+
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
87

9-
class AsyncEventDispatcher extends EventDispatcher
10-
{
8+
if (class_exists(Event::class) && !class_exists(LegacyEventDispatcherProxy::class)) {
119
/**
12-
* @var EventDispatcherInterface
10+
* Symfony < 4.3
1311
*/
14-
private $trueEventDispatcher;
12+
class AsyncEventDispatcher extends AbstractAsyncEventDispatcher
13+
{
14+
/**
15+
* {@inheritdoc}
16+
*/
17+
public function dispatch($eventName, Event $event = null)
18+
{
19+
$this->parentDispatch($event, $eventName);
1520

16-
/**
17-
* @var AsyncListener
18-
*/
19-
private $asyncListener;
21+
$this->trueEventDispatcher->dispatch($eventName, $event);
22+
}
2023

21-
/**
22-
* @param EventDispatcherInterface $trueEventDispatcher
23-
* @param AsyncListener $asyncListener
24-
*/
25-
public function __construct(EventDispatcherInterface $trueEventDispatcher, AsyncListener $asyncListener)
26-
{
27-
$this->trueEventDispatcher = $trueEventDispatcher;
28-
$this->asyncListener = $asyncListener;
24+
protected function parentDispatch($event, $eventName)
25+
{
26+
parent::dispatch($eventName, $event);
27+
}
2928
}
30-
29+
} elseif (class_exists(Event::class)) {
3130
/**
32-
* This method dispatches only those listeners that were marked as async.
33-
*
34-
* @param string $eventName
35-
* @param Event|null $event
31+
* Symfony >= 4.3 and < 5.0
3632
*/
37-
public function dispatchAsyncListenersOnly($eventName, Event $event = null)
33+
class AsyncEventDispatcher extends AbstractAsyncEventDispatcher
3834
{
39-
try {
40-
$this->asyncListener->syncMode($eventName);
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function dispatch($event, $eventName = null)
39+
{
40+
$this->parentDispatch($event, $eventName);
4141

42-
parent::dispatch($eventName, $event);
43-
} finally {
44-
$this->asyncListener->resetSyncMode();
42+
return $this->trueEventDispatcher->dispatch($event, $eventName);
4543
}
46-
}
4744

45+
protected function parentDispatch($event, $eventName)
46+
{
47+
parent::dispatch($event, $eventName);
48+
}
49+
}
50+
} else {
4851
/**
49-
* {@inheritdoc}
52+
* Symfony >= 5.0
5053
*/
51-
public function dispatch($eventName, Event $event = null)
54+
class AsyncEventDispatcher extends AbstractAsyncEventDispatcher
5255
{
53-
parent::dispatch($eventName, $event);
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
public function dispatch(object $event, string $eventName = null): object
60+
{
61+
$this->parentDispatch($event, $eventName);
62+
63+
return $this->trueEventDispatcher->dispatch($event, $eventName);
64+
}
5465

55-
$this->trueEventDispatcher->dispatch($eventName, $event);
66+
protected function parentDispatch($event, $eventName)
67+
{
68+
return parent::dispatch($event, $eventName);
69+
}
5670
}
5771
}

0 commit comments

Comments
 (0)