1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
require __DIR__ . '/../vendor/autoload.php'; |
4
|
|
|
|
5
|
|
|
use Puzzle\Configuration\Memory; |
6
|
|
|
use Puzzle\AMQP\Clients\Pecl; |
7
|
|
|
use Puzzle\AMQP\Workers\WorkerContext; |
8
|
|
|
use Puzzle\AMQP\ReadableMessage; |
9
|
|
|
use Puzzle\AMQP\Workers\Worker; |
10
|
|
|
use Psr\Log\LoggerAwareTrait; |
11
|
|
|
use Psr\Log\NullLogger; |
12
|
|
|
use Puzzle\AMQP\Workers\ProcessorInterfaceAdapter; |
13
|
|
|
use Puzzle\AMQP\Consumers\Insomniac; |
14
|
|
|
|
15
|
|
|
$configuration = new Memory(array( |
16
|
|
|
'amqp/broker/host' => 'rabbitmq', |
17
|
|
|
'amqp/broker/login' => 'guest', |
18
|
|
|
'amqp/broker/password' => 'guest', |
19
|
|
|
'amqp/broker/vhost' => '/', |
20
|
|
|
'amqp/global/disallowSilentDropping' => true, |
21
|
|
|
'app/id' => 'puzzle-amqp-test', |
22
|
|
|
)); |
23
|
|
|
|
24
|
|
|
$client = new Pecl($configuration); |
|
|
|
|
25
|
|
|
$consumer = new Insomniac(); |
26
|
|
|
|
27
|
|
|
class TestWorker implements Worker |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
use LoggerAwareTrait; |
30
|
|
|
|
31
|
|
|
private |
32
|
|
|
$nbMessages; |
|
|
|
|
33
|
|
|
|
34
|
|
|
public function __construct() |
35
|
|
|
{ |
36
|
|
|
$this->logger = new NullLogger(); |
|
|
|
|
37
|
|
|
$this->nbMessages = 0; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function process(ReadableMessage $message) |
41
|
|
|
{ |
42
|
|
|
$decoded = [ |
43
|
|
|
"content-type" => $message->getContentType(), |
44
|
|
|
"body" => $message->getBodyInOriginalFormat(), |
45
|
|
|
"headers" => $message->getHeaders() |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
var_dump($decoded); |
|
|
|
|
49
|
|
|
|
50
|
|
|
$this->nbMessages++; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function nbMessagesProcessed() |
54
|
|
|
{ |
55
|
|
|
return $this->nbMessages; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$workerContext = new WorkerContext(function() { |
60
|
|
|
return new TestWorker(); |
61
|
|
|
}, |
62
|
|
|
$consumer, |
63
|
|
|
'test_1' |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$processor = new ProcessorInterfaceAdapter($workerContext); |
67
|
|
|
|
68
|
|
|
$workerContext |
69
|
|
|
->getConsumer() |
70
|
|
|
->consume($processor, $client, $workerContext); |
71
|
|
|
|
72
|
|
|
$nbMsg = $workerContext->getWorker()->nbMessagesProcessed(); |
73
|
|
|
if($nbMsg > 0) |
74
|
|
|
{ |
75
|
|
|
echo "\033[32m[SUCCESS] WORKER HAS PROCESSED $nbMsg MESSAGE(S) \033[0m" . PHP_EOL; |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
echo "\033[31m[FAILD] WORKER HAS PROCESSED NONE MESSAGE \033[0m" . PHP_EOL; |
80
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.