Issues (30)

examples/04-stomp-consumer.php (1 issue)

1
<?php
2
/**
3
 * See http://www.rabbitmq.com/stomp.html for more infos.
4
 * Run with rabbit stomp plugin.
5
 *
6
 * You must enable the plugin
7
 *   `rabbitmq-plugins enable rabbitmq_stomp`
8
 * You need to create rabbit user (stompUser, stompPass)
9
 *   Guest user have security restriction. See https://github.com/stomp-php/stomp-php/issues/105
10
 *
11
 * This example is the consumer part.
12
 * It print the message body
13
 * if the message body is "wrong" the message will be nack
14
 * else ack all message
15
 */
16
require_once __DIR__.'/../vendor/autoload.php';
17
18
use Swarrot\Broker\Message;
19
use Swarrot\Broker\MessageProvider\StatefulStompMessageProvider;
20
use Swarrot\Consumer;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Consumer. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
21
use Swarrot\Processor\Callback\CallbackProcessor;
22
23
$client = new \Stomp\Client('tcp://localhost:61613');
24
$client->setLogin('stompUser', 'stompPass');
25
$client->setVhostname('/');
26
$client->connect();
27
28
$stompMessageProvider = new StatefulStompMessageProvider($client, '/queue/stomp_queue');
29
30
$processor = new CallbackProcessor(function (Message $message, array $options) use ($stompMessageProvider) {
31
    $body = $message->getBody();
32
    if ('wrong' == $body) {
33
        echo "$body NACK\r\n";
34
        $stompMessageProvider->nack($message);
35
36
        return;
37
    }
38
    echo "$body\r\n";
39
    $stompMessageProvider->ack($message);
40
});
41
42
$consumer = new Consumer($stompMessageProvider, $processor);
43
44
$consumer->consume([]);
45