Skip to content

Commit cc33fa8

Browse files
committed
add quick tour for async event dispatcher.
1 parent 1783753 commit cc33fa8

File tree

3 files changed

+120
-1
lines changed

3 files changed

+120
-1
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Async event dispatcher (Symfony)
2+
3+
The doc shows how you can setup async event dispatching in plain PHP.
4+
If you are looking for the ways to use it in Symfony application [read this post instead](../bundle/async_events.md)
5+
6+
* [Installation](#installation)
7+
* [Configuration](#configuration)
8+
* [Dispatch event](#dispatch-event)
9+
* [Process async events](#process-async-events)
10+
11+
## Installation
12+
13+
You need the async dispatcher library and a one of [the supported transports](../transport)
14+
15+
```bash
16+
$ composer require enqueue/async-event-dispatcher enqueue/fs
17+
```
18+
19+
## Configuration
20+
21+
```php
22+
<?php
23+
24+
// config.php
25+
26+
use Enqueue\AsyncEventDispatcher\AsyncListener;
27+
use Enqueue\AsyncEventDispatcher\AsyncProcessor;
28+
use Enqueue\AsyncEventDispatcher\PhpSerializerEventTransformer;
29+
use Enqueue\AsyncEventDispatcher\AsyncEventDispatcher;
30+
use Enqueue\AsyncEventDispatcher\SimpleRegistry;
31+
use Enqueue\Fs\FsConnectionFactory;
32+
use Symfony\Component\EventDispatcher\EventDispatcher;
33+
34+
require_once __DIR__.'/vendor/autoload.php';
35+
36+
// it could be any other enqueue/psr-queue compatible context.
37+
$context = (new FsConnectionFactory('file://'.__DIR__.'/queues'))->createContext();
38+
$eventQueue = $context->createQueue('symfony_events');
39+
40+
$registry = new SimpleRegistry(
41+
['the_event' => 'default'],
42+
['default' => new PhpSerializerEventTransformer($context, true)]
43+
);
44+
45+
$asyncListener = new AsyncListener($context, $registry, $eventQueue);
46+
47+
$dispatcher = new EventDispatcher();
48+
49+
// the listener sends even as a message through MQ
50+
$dispatcher->addListener('the_event', $asyncListener);
51+
52+
$asyncDispatcher = new AsyncEventDispatcher($dispatcher, $asyncListener);
53+
54+
// the listener is executed on consumer side.
55+
$asyncDispatcher->addListener('the_event', function() {
56+
});
57+
58+
$asyncProcessor = new AsyncProcessor($registry, $asyncDispatcher);
59+
```
60+
61+
## Dispatch event
62+
63+
```php
64+
<?php
65+
66+
// send.php
67+
68+
use Symfony\Component\EventDispatcher\GenericEvent;
69+
70+
require_once __DIR__.'/vendor/autoload.php';
71+
72+
include __DIR__.'/config.php';
73+
74+
$dispatcher->dispatch('the_event', new GenericEvent('theSubject'));
75+
```
76+
77+
## Process async events
78+
79+
```php
80+
<?php
81+
82+
// consume.php
83+
84+
use Enqueue\Psr\PsrProcessor;
85+
86+
require_once __DIR__.'/vendor/autoload.php';
87+
include __DIR__.'/config.php';
88+
89+
$consumer = $context->createConsumer($eventQueue);
90+
91+
while (true) {
92+
if ($message = $consumer->receive(5000)) {
93+
$result = $asyncProcessor->process($message, $context);
94+
95+
switch ((string) $result) {
96+
case PsrProcessor::ACK:
97+
$consumer->acknowledge($message);
98+
break;
99+
case PsrProcessor::REJECT:
100+
$consumer->reject($message);
101+
break;
102+
case PsrProcessor::REQUEUE:
103+
$consumer->reject($message, true);
104+
break;
105+
default:
106+
throw new \LogicException('Result is not supported');
107+
}
108+
}
109+
}
110+
```
111+
112+
[back to index](../index.md)

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
- [Production settings](bundle/production_settings.md)
3535
- [Debuging](bundle/debuging.md)
3636
- [Functional testing](bundle/functional_testing.md)
37+
* Async event dispatcher (Symfony)
38+
- [Quick tour](async_event_dispatcher/quick_tour.md)
3739
* Magento
3840
- [Quick tour](magento/quick_tour.md)
3941
- [Cli commands](magento/cli_commands.md)

pkg/async-event-dispatcher/AsyncListener.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public function __construct(PsrContext $context, Registry $registry, $eventQueue
4040
$this->eventQueue = $eventQueue instanceof PsrQueue ? $eventQueue : $context->createQueue($eventQueue);
4141
}
4242

43+
public function __invoke(Event $event, $eventName)
44+
{
45+
$this->onEvent($event, $eventName);
46+
}
47+
4348
public function resetSyncMode()
4449
{
4550
$this->syncMode = [];
@@ -67,7 +72,7 @@ public function isSyncMode($eventName)
6772
* @param Event $event
6873
* @param string $eventName
6974
*/
70-
public function onEvent(Event $event = null, $eventName)
75+
public function onEvent(Event $event, $eventName)
7176
{
7277
if (false == isset($this->syncMode[$eventName])) {
7378
$transformerName = $this->registry->getTransformerNameForEvent($eventName);

0 commit comments

Comments
 (0)