Giter VIP home page Giter VIP logo

swarrot's Introduction

Swarrot

Build Status Scrutinizer Quality Score SensioLabsInsight Latest Stable Version Latest Unstable Version

Swarrot is PHP library to consume messages from a broker.

Installation

The recommended way to install Swarrot is through Composer. Require the swarrot/swarrot package into your composer.json file:

{
    "require": {
        "swarrot/swarrot": "@stable"
    }
}

Protip: you should browse the swarrot/swarrot page to choose a stable version to use, avoid the @stable meta constraint.

Usage

Basic usage

First, you need to create a message provider to retrieve message from your broker. For example, with A PeclPackageMessageProvider (retrieve message from an AMQP broker with the pecl amqp package:

use Swarrot\Broker\PeclPackageMessageProvider;

// Create connection
$connection = new \AMQPConnection();
$connection->connect();
$channel = new \AMQPChannel($connection);
// Get the queue to consume
$queue = new \AMQPQueue($channel);
$queue->setName('global');

$messageProvider = new PeclPackageMessageProvider($queue);

Once it's done you need to create a Processor to process messages retrieved from the broker. This processor must implement Swarrot\Processor\ProcessorInterface. For example:

use Swarrot\Processor\ProcessorInterface;
use Swarrot\Broker\Message;

class Processor implements ProcessorInterface {
    public function process(Message $message, array $options) {
        echo sprintf("Consume message #%d\n", $message->getId());
    }
}

You now have a Swarrot\Broker\MessageProviderInterface to retrieve messages and a Processor to process them. So, ask the Swarrot\Consumerto do it's job :

use Swarrot\Message;

$consumer = new Consumer($messageProvider, $processor);
$consumer->consume();

Using a stack

Heavily inspired by stackphp/builder you can use Swarrot\Processor\Stack\Builder to stack your processors. Using the built in processors or by creating your own, you can extend the behavior of your base processor. In this example, your processor is decorated by 2 others one. The ExceptionCatcherProcessor which decorate your own with a try/catch block and the MaxMessagesProcessor which stop your worker when some messages have been consumed.

use Swarrot\Processor\ProcessorInterface;
use Swarrot\Broker\Message;

class Processor implements ProcessorInterface {
    public function process(Message $message, array $options) {
        echo sprintf("Consume message #%d\n", $message->getId());
    }
}

$stack = (new \Swarrot\Processor\Stack\Builder())
    ->push('Swarrot\Processor\MaxMessages\MaxMessagesProcessor', new Logger())
    ->push('Swarrot\Processor\ExceptionCatcher\ExceptionCatcherProcessor')
    ->push('Swarrot\Processor\Ack\AckProcessor', $messageProvider)
;

$processor = $stack->resolve(new Processor());

Here is an illustration to show you what append when this order is used:

this

Processors

Official processors

Create your own processor

To create your own processor and be able to use it with the StackProcessor, you just need to implement ProcessorInterface and to take another ProcessorInterface as first argument in constructor.

Inspiration

License

Swarrot is released under the MIT License. See the bundled LICENSE file for details.

swarrot's People

Contributors

adrienbrault avatar astorije avatar cvasseur avatar jlepeltier avatar jmoati avatar l0rd59 avatar ludofleury avatar lyrixx avatar neeckeloo avatar niktux avatar odolbeau avatar pborreli avatar remylemeunier avatar stof avatar taluu avatar yohang avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.