PHPackages                             nilportugues/messagebus - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. nilportugues/messagebus

ActiveLibrary[Caching](/categories/caching)

nilportugues/messagebus
=======================

MessageBus library. Contains CommandBus, QueryBus and EventBus and multiple middleware implementations.

1.2.1(9y ago)18610.4k6[2 issues](https://github.com/PHPMessageBus/messagebus/issues)1MITPHPPHP &gt;=7

Since Apr 17Pushed 9y ago13 watchersCompare

[ Source](https://github.com/PHPMessageBus/messagebus)[ Packagist](https://packagist.org/packages/nilportugues/messagebus)[ Docs](http://nilportugues.com)[ RSS](/packages/nilportugues-messagebus/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (7)Dependencies (9)Versions (7)Used By (1)

Message Bus
===========

[](#message-bus)

[![Build Status](https://camo.githubusercontent.com/4e06e684b9487682821995786fa7c8d3c869595cb0ee11c9deda1169367f46a0/68747470733a2f2f7472617669732d63692e6f72672f5048504d6573736167654275732f6d6573736167656275732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/PHPMessageBus/messagebus) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/c4d2ccb0b9bea7ed17af4c63a280ac687860d31e13ad293e9eb44e446fb2d921/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f5048504d6573736167654275732f6d6573736167656275732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/PHPMessageBus/messagebus/?branch=master) [![SensioLabsInsight](https://camo.githubusercontent.com/0485370011a827d65420dab8723e89dc83b648498ba74bc180bc94a4a373143b/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f30346531613132662d653931362d346165362d383735662d3565363934666237316236612f6d696e692e706e673f)](https://insight.sensiolabs.com/projects/04e1a12f-e916-4ae6-875f-5e694fb71b6a) [![Latest Stable Version](https://camo.githubusercontent.com/5af19a7a682d0d083dac5714322546db5ea658a7b70ba01fffc5bcafba390206/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6d6573736167656275732f762f737461626c653f)](https://packagist.org/packages/nilportugues/messagebus) [![Total Downloads](https://camo.githubusercontent.com/b9a07c45b1a9562a135bb4024dfdcfe561342902219f4f038d9ba9e980319ed5/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6d6573736167656275732f646f776e6c6f6164733f)](https://packagist.org/packages/nilportugues/messagebus) [![License](https://camo.githubusercontent.com/c2a10811ff107d251919051033e773cb75f85f75228c31d08e7fde04549687cd/68747470733a2f2f706f7365722e707567782e6f72672f6e696c706f727475677565732f6d6573736167656275732f6c6963656e73653f)](https://packagist.org/packages/nilportugues/messagebus)[![Donate](https://camo.githubusercontent.com/7b6de155df30b37b25eb5fec52f9213680c3dbf067dfb7d7e2850ac4096c7d05/68747470733a2f2f7777772e70617970616c6f626a656374732e636f6d2f656e5f55532f692f62746e2f62746e5f646f6e6174655f534d2e676966)](https://www.paypal.me/nilportugues)

Implementation for the CommandBus, the QueryBus and the EventBus in PHP 7 and using PSR-11.

---

- [Installation](#installation)
- [Introduction](#introduction)
- [What is a Message Bus?](#what-is-a-message-bus)
- [What are its benefits?](#what-are-its-benefits)
- [1. CommandBus](#1---commandbus)
    - [1.1 - Usage](#11---usage)
        - [1.1.1 - Create a Command](#111---create-a-command)
        - [1.1.2 - Create a CommandHandler](#112---create-a-commandhandler)
        - [1.1.3 - Register the CommandHandler](#113---register-the-commandhandler)
        - [1.1.4 - Setting up the CommandBus](#114---setting-up-the-commandbus)
        - [1.1.5 - Registering the remaining CommandBus classes](#115---registering-the-remaining-commandbus-classes)
        - [1.1.6 - Running the CommandBus](#116---running-the-commandbus)
    - [ 1.2 - Predefined Middlewares](#12---predefined-middlewares)
    - [ 1.3 - Custom Middlewares](#13---custom-middlewares)
- [2. QueryBus](#2---querybus)
    - [2.1 - Usage](#21-usage)
        - [2.1.1 - Create a Query](#211---create-a-query)
        - [2.1.2 - Create a QueryHandler](#212---create-a-queryhandler)
        - [2.1.3 - Create a QueryResponse](#213---create-a-queryresponse)
        - [2.1.4 - Register the QueryHandler](#214---register-the-queryhandler)
        - [2.1.5 - Setting up the QueryBusMiddleware](#215---setting-up-the-querybusmiddleware)
        - [2.1.6 - Registering the remaining QueryBus classes](#216---registering-the-remaining-querybus-classes)
        - [2.1.7 - Running the QueryBus](#216---running-the-querybus)
    - [ 2.2 - Predefined Middlewares](#22---predefined-middlewares)
    - [ 2.3 - Custom Middlewares](#23---custom-middlewares)
- [3. EventBus](#3---eventbus)
    - [3.1 - Usage](#31---usage)
        - [3.1.1 - Create an Event](#311---create-an-event)
        - [3.1.2 - Create an EventHandler](#312---create-an-eventhandler)
        - [3.1.3 - (Optional) Set the EventHandler's Priority](#313---optional-set-the-eventhandlers-priority)
        - [3.1.4 - Register the EventHandler](#314---register-the-eventhandler)
        - [3.1.5 - Setting up the EventBusMiddleware](#315---setting-up-the-eventbusmiddleware)
        - [3.1.6 - Registering the remaining EventBus classes](#316---registering-the-remaining-eventbus-classes)
        - [3.1.7 - Running the EventBus](#317---running-the-eventbus)
        - [3.1.8 - (Optional) Running the EventBus as a Queue](#318---optional-running-the-eventbus-as-a-queue)
    - [3.2 - Predefined Middlewares](#32---predefined-middlewares)
    - [3.3 - Custom Middlewares](#33---custom-middlewares)
- [4 - Serializers](#4---serializers)
    - [4.1 - NilPortugues\\MessageBus\\Serializer\\NativeSerializer](#41---nilportuguesmessagebusserializernativeserializer)
    - [4.2 - NilPortugues\\MessageBus\\Serializer\\JsonSerializer](#42---nilportuguesmessagebusserializerjsonserializer)
    - [4.3 - NilPortugues\\MessageBus\\Serializer\\JsonObjectSerializer](#43---nilportuguesmessagebusserializerjsonobjectserializer)
- [Contribute](#contribute)
- [Support](#support)
- [Authors](#authors)
- [License](#license)

Installation
------------

[](#installation)

Use [Composer](https://getcomposer.org) to install the package:

```
$ composer require nilportugues/messagebus

```

Introduction
------------

[](#introduction)

The idea of a message bus is that you create message objects that represent what you want your application to do. Then, you toss it into the bus and the bus makes sure that the message object gets to where it needs to go.

Easy right? Keep reading!

### What is a Message Bus?

[](#what-is-a-message-bus)

A Message Bus is a pipe of Messages. This implementation takes care of 3 types of messages, Commands, Queries and Events. While all look similar at first, their intent is different.

- **Command**: its intent is about expressing an order to the system and modifies its current state. User expects no response.
- **Event**: its intent is to express something that has already happened in the system, and record it. User expects no response.
- **Query**: its intent is about expressing a question to the system. User expects a response.

From this classification, one can spot that Command and Events can work together very well.

### What are its benefits?

[](#what-are-its-benefits)

Given the nature of the message, implementing an interface, you may write behaviours that wrap the message to log, process or modify the response using the **Decorator pattern**. These are called **Middleware**.

**For instance:**

- Implementing task-based user-interfaces you can map concepts to Commands, Queries and Events easily.
- It allows you to easily write a logging system to know what's going, whether its a Command, a Query or an Event. It's possible.

**To wrap-up, its benefits are:**

- Encourages separation of concerns.
- Encourages single responsibility design.

---

1. CommandBus
-------------

[](#1-commandbus)

### 1.1 - Usage

[](#11---usage)

#### 1.1.1 - Create a Command

[](#111---create-a-command)

```
