-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathbootstrap.php
More file actions
100 lines (80 loc) · 2.15 KB
/
Copy pathbootstrap.php
File metadata and controls
100 lines (80 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
use Bernard\Consumer;
use Bernard\EventListener;
use Bernard\Message;
use Bernard\Producer;
use Bernard\QueueFactory\PersistentFactory;
use Bernard\Router\ReceiverMapRouter;
use Bernard\Serializer;
use Symfony\Component\EventDispatcher\EventDispatcher;
/*
* This file contains helper methods for the examples. See example/$driver.php
* for how to initiate the driver. Also the helper methods can be used as
* guidance if you are using Bernard outside a framework or you are developing
* a plugin to a framework.
*/
if (file_exists($autoloadFile = __DIR__.'/../vendor/autoload.php') || file_exists($autoloadFile = __DIR__.'/../../../autoload.php')) {
require $autoloadFile;
}
require __DIR__.'/EchoTimeService.php';
ini_set('display_errors', 1);
error_reporting(E_ALL);
function get_serializer()
{
return new Serializer();
}
function get_event_dispatcher()
{
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new EventListener\ErrorLogSubscriber());
$dispatcher->addSubscriber(new EventListener\FailureSubscriber(get_producer()));
return $dispatcher;
}
function get_queue_factory()
{
return new PersistentFactory(get_driver(), get_serializer());
}
function get_producer()
{
return new Producer(get_queue_factory(), new EventDispatcher());
}
function get_receivers()
{
return new ReceiverMapRouter([
'EchoTime' => new EchoTimeService(),
]);
}
function get_consumer()
{
return new Consumer(get_receivers(), get_event_dispatcher());
}
function produce()
{
$producer = get_producer();
while (true) {
$producer->produce(new Message\PlainMessage('EchoTime', [
'time' => time(),
]));
usleep(rand(100, 1000));
}
}
function consume()
{
$queues = get_queue_factory();
$consumer = get_consumer();
$consumer->consume($queues->create('echo-time'));
}
function main()
{
if (!isset($_SERVER['argv'][1])) {
die('You must provide an argument of either "consume" or "produce"');
}
if ($_SERVER['argv'][1] == 'produce') {
produce();
}
if ($_SERVER['argv'][1] == 'consume') {
consume();
}
}
// Run this diddy
main();