<commit_before># php-event-pubsub

An event protocol and implementation over pub/sub

[![Author](http://img.shields.io/badge/author-@superbalist-blue.svg?style=flat-square)](https://twitter.com/superbalist)
[![Build Status](https://img.shields.io/travis/Superbalist/php-event-pubsub/master.svg?style=flat-square)](https://travis-ci.org/Superbalist/php-event-pubsub)
[![StyleCI](https://styleci.io/repos/80006408/shield?branch=master)](https://styleci.io/repos/80006408)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![Packagist Version](https://img.shields.io/packagist/v/superbalist/php-event-pubsub.svg?style=flat-square)](https://packagist.org/packages/superbalist/php-event-pubsub)
[![Total Downloads](https://img.shields.io/packagist/dt/superbalist/php-event-pubsub.svg?style=flat-square)](https://packagist.org/packages/superbalist/php-event-pubsub)

This library builds on top of the [php-pubsub](https://github.com/Superbalist/php-pubsub) package and adds support for
listening to and dispatching events over pub/sub channels.


## Installation

```bash
composer require superbalist/php-event-pubsub
```

## Integrations

Want to get started quickly? Check out some of these integrations:

* Laravel - https://github.com/Superbalist/laravel-event-pubsub

## Usage

### Simple Events

A `SimpleEvent` is an event which takes a name and optional attributes.

```php
// create a new event manager
$adapter = new \Superbalist\PubSub\Adapters\LocalPubSubAdapter();
$translator = new \Superbalist\EventPubSub\Translators\SimpleEventMessageTranslator();
$manager = new \Superbalist\EventPubSub\EventManager($adapter, $translator);

// dispatch an event
$event = new \Superbalist\EventPubSub\Events\SimpleEvent(
    'user.created',
    [
        'user' => [
            'id' => 1456,
            'first_name' => 'Joe',
            'last_name' => 'Soap',
            'email' => '<EMAIL>',
        ],
    ]
);
$manager->dispatch('events', $event);

// dispatch multiple events
$events = [
    new \Superbalist\EventPubSub\Events\SimpleEvent(
        'user.created',
        [
            'user' => [
                // ...
            ],
        ]
    ),
    new \Superbalist\EventPubSub\Events\SimpleEvent(
        'user.created',
        [
            'user' => [
                // ...
            ],
        ]
    ),
];
$manager->dispatch('events', $events);

// listen for an event
$manager->listen('events', 'user.created', function (\Superbalist\EventPubSub\EventInterface $event) {
    var_dump($event->getName());
    var_dump($event->getAttribute('user'));
});

// listen for all events on the channel
$manager->listen('events', '*', function (\Superbalist\EventPubSub\EventInterface $event) {
    var_dump($event->getName());
});
```

### Topic Events

A `TopicEvent` is an event which takes a topic, name, version and optional attributes.

```php
// create a new event manager
$adapter = new \Superbalist\PubSub\Adapters\LocalPubSubAdapter();
$translator = new \Superbalist\EventPubSub\Translators\TopicEventMessageTranslator();<commit_msg>fix usage typo in readme<commit_after># php-event-pubsub

An event protocol and implementation over pub/sub

[![Author](http://img.shields.io/badge/author-@superbalist-blue.svg?style=flat-square)](https://twitter.com/superbalist)
[![Build Status](https://img.shields.io/travis/Superbalist/php-event-pubsub/master.svg?style=flat-square)](https://travis-ci.org/Superbalist/php-event-pubsub)
[![StyleCI](https://styleci.io/repos/80006408/shield?branch=master)](https://styleci.io/repos/80006408)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
[![Packagist Version](https://img.shields.io/packagist/v/superbalist/php-event-pubsub.svg?style=flat-square)](https://packagist.org/packages/superbalist/php-event-pubsub)
[![Total Downloads](https://img.shields.io/packagist/dt/superbalist/php-event-pubsub.svg?style=flat-square)](https://packagist.org/packages/superbalist/php-event-pubsub)

This library builds on top of the [php-pubsub](https://github.com/Superbalist/php-pubsub) package and adds support for
listening to and dispatching events over pub/sub channels.


## Installation

```bash
composer require superbalist/php-event-pubsub
```

## Integrations

Want to get started quickly? Check out some of these integrations:

* Laravel - https://github.com/Superbalist/laravel-event-pubsub

## Usage

### Simple Events

A `SimpleEvent` is an event which takes a name and optional attributes.

```php
// create a new event manager
$adapter = new \Superbalist\PubSub\Adapters\LocalPubSubAdapter();
$translator = new \Superbalist\EventPubSub\Translators\SimpleEventMessageTranslator();
$manager = new \Superbalist\EventPubSub\EventManager($adapter, $translator);

// dispatch an event
$event = new \Superbalist\EventPubSub\Events\SimpleEvent(
    'user.created',
    [
        'user' => [
            'id' => 1456,
            'first_name' => 'Joe',
            'last_name' => 'Soap',
            'email' => '<EMAIL>',
        ],
    ]
);
$manager->dispatch('events', $event);

// dispatch multiple events
$events = [
    new \Superbalist\EventPubSub\Events\SimpleEvent(
        'user.created',
        [
            'user' => [
                // ...
            ],
        ]
    ),
    new \Superbalist\EventPubSub\Events\SimpleEvent(
        'user.created',
        [
            'user' => [
                // ...
            ],
        ]
    ),
];
$manager->dispatchBatch('events', $events);

// listen for an event
$manager->listen('events', 'user.created', function (\Superbalist\EventPubSub\EventInterface $event) {
    var_dump($event->getName());
    var_dump($event->getAttribute('user'));
});

// listen for all events on the channel
$manager->listen('events', '*', function (\Superbalist\EventPubSub\EventInterface $event) {
    var_dump($event->getName());
});
```

### Topic Events

A `TopicEvent` is an event which takes a topic, name, version and optional attributes.

```php
// create a new event manager
$adapter = new \Superbalist\PubSub\Adapters\LocalPubSubAdapter();
$translator = new \Superbalist\EventPubSub\Translators\TopicEventMessageTranslator();
{'fraction_non_alphanumeric': 0.15897435897435896, 'fraction_numerical': 0.00641025641025641, 'mean_word_length': 3.6470588235294117, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, 'https://': 24, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '389601', 'n_tokens_mistral': 2040, 'n_tokens_neox': 1957, 'n_words': 349}