iztoksvetik/silex-amqp-provider

Silex AMQP provider

1.0.1 2015-10-20 15:12 UTC

This package is not auto-updated.

Last update: 2024-04-24 22:37:24 UTC


README

#AMQP Silex service provider

##About

This Silex service provider registers the M6Web/AmqpBundle as a service in Silex. It uses the php-amqp extension, and can communicate with any AMQP 0-9-1 compatible server, such as RabbitMQ, OpenAMQP and Qpid.

Publishing messages to AMQP server from a Silex application is easy

$app->post('/message', function(Request $request) use ($app){
    $producer = $app['amqp.producer']['producer_name'];
    $producer->publish('Some message');

    return new Response($msg_body);
});

And likewise consuming

$consumer = $app['amqp.consumer']['consumer_name'];
$message  = $consumer->getMessage()->getBody();

##Instalation

Require in your composer.json

{
    "require": {
        "iztoksvetik/silex-amqp-provider": "~1.0",
    }
}

Register the service

use Silex\Application;
use IztokSvetik\SilexAmqp\Provider;

$app = new Application();
$app->register(new AmqpServiceProvider());

Install the provider

$ composer update iztoksvetik/silex-amqp-provider

##Configuration

$app->register(new AmqpServiceProvider(), [
    'amqp.connections' => [
        'default' => [
            'host'      => 'localhost', // optional - default "localhost"
            'port'      => 5672,        // optional - default 5672
            'login'     => 'guest',     // optional - default "guest"
            'password'  => 'guest',     // optional - default "guest"
            'vhost'     => '/',         // optional - default "/"
            'lazy'      => false        // optional - default false
        ],
    ],
    'amqp.producers' => [
        'my_producer' => [
            'class'            => 'My\Producer\Class', // optional - overload default class
            'connection'       => 'default',           // required
            'queue_options'    => [
                'name'        => 'my-queue',           // optional
                'passive'     => false,                // optional - default false
                'durable'     => true,                 // optional - default true
                'auto_delete' => false,                // optional - default false
            ],
            'exchange_options' => [
                'name'               => 'my-exchange',      // required
                'type'               => 'direct',           // required - possible direct/fanout/topic/headers
                'passive'            => false,              // optional - default false
                'durable'            => true,               // optional - default true
                'auto_delete'        => false,              // optional - default false
                'arguments'          => ['key' => 'value'], // optional - default []
                'routing_keys'       => ['key1', 'key2'],   // optional - default []
                'publish_attributes' => ['key' => 'value'], // optional - default []
            ],
        ]
    ],
    'amqp.consumers' => [
        'my_consumer' => [
            'class'            => 'My\Consumer\Class', // optional - overload default class
            'connection'       => 'default',          // required
            'exchange_options' => [
                'name' => 'my-exchange'               // required
            ],
            'queue_options'    => [
                'name'         => 'my-queue',         // required
                'passive'      => false,              // optional - default false
                'durable'      => true,               // optional - default true
                'exclusive'    => false,              // optional - default false
                'auto_delete'  => false,              // optional - default false
                'arguments'    => ['key' => 'value'], // optional - default []
                'routing_keys' => ['key1', 'key2'],   // optional - default []
            ],
            'qos_options'      => [
                'prefetch_size'  => 0, // optional - default 0
                'prefetch_count' => 0  // optional - default 0
            ]
        ]
    ],
]);

In this example your service container will have services $app['amqp.consumer']['my_consumer'] and $app['amqp.producer']['my_producer']