PHPackages                             maxkaemmerer/events - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. maxkaemmerer/events

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

maxkaemmerer/events
===================

Provides Interfaces and Implementations for Event, EventSubscriber communication

v1.0.0(7y ago)0261MITPHPPHP &gt;=7.2

Since Jul 21Pushed 7y agoCompare

[ Source](https://github.com/maxkaemmerer/events)[ Packagist](https://packagist.org/packages/maxkaemmerer/events)[ RSS](/packages/maxkaemmerer-events/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (1)Dependencies (2)Versions (3)Used By (1)

maxkaemmerer/events
===================

[](#maxkaemmererevents)

[![Travis branch](https://camo.githubusercontent.com/8cfce179aaff2c292d4270711f8df6f746954778a593b09996b799c0e83bd554/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6d61786b61656d6d657265722f6576656e74732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/maxkaemmerer/events)[![Coveralls github](https://camo.githubusercontent.com/75d34c21f3db7b946ea3ad6c5f14cc37ab7ad95c43be8f0e3b2e6116fe46ed3a/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6d61786b61656d6d657265722f6576656e74732f6d61737465722e7376673f7374796c653d666c61742d737175617265266272616e63683d6d6173746572)](https://coveralls.io/github/maxkaemmerer/events?branch=master)[![PHP from Packagist](https://camo.githubusercontent.com/f3f6ea638f50933108cc3bfda53bc125b4b696f8119a15f935be437db740773d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6d61786b61656d6d657265722f6576656e74732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maxkaemmerer/events)[![Packagist](https://camo.githubusercontent.com/fa0cf617aa389072bbc11bdd5ad6441fbdced4b596eccdcc0ac2f80b4b9620f4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d61786b61656d6d657265722f6576656e74732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maxkaemmerer/events)[![Packagist](https://camo.githubusercontent.com/8786037b23dfd2cb643800d24419903b83bb99704f23508333a15905502bfe15/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6d61786b61656d6d657265722f6576656e74732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/maxkaemmerer/events)

Description:
------------

[](#description)

This library offers interfaces and implementations for a simple event, event-subscriber, event-courier structure.

This is of course not an original idea, but my preferred and fairly simple implementation.

The code is fully tested, I however do not take responsibility for use in production.

Use at your own risk.

Installation:
-------------

[](#installation)

`composer require maxkaemmerer/events`

Usage:
------

[](#usage)

Generally you don't want to subscribe each `EventSubscriber` by hand. You might want to use dependency injection via a container or service-manager.

(An example for the Symfony Framework would be using a `CompilerPass`)

You would also want to inject the `EventCourier` itself via dependency injection wherever you need it.

Feel free to create your own implementations of `EventCourier`, `Subscription` and `Payload` if you require something more advanced.

#### Subscribe an EventSubscriber:

[](#subscribe-an-eventsubscriber)

Subscribe an `EventSubscriber` to the `EventCourier`. The `EventSubscriber`'s `subscription()` method returns a `Subscription`, which contains the name of the `Event` that the `EventSubscriber` subscribes to and the priority at which it wants to be notified.

(The higher the priority, the earlier the `EventSubscriber` get notified of the `Event`, assuming there are other `EventSubscriber`s with lower priority.)

Best practice would be using the fully qualified class name of the event. `MyEvent::class`

The `EventSubscriber::on($event)` method is where your actual domain logic happens.

Feel free to inject services, a container, or whatever else you need, into your `EventSubscriber`s.

```
$courier = new SimpleEventCourier();

$courier->subscribe(new class implements EventSubscriber
{

    /**
     * @param Event $event
     */
    public function on(Event $event): void
    {
        echo 'Notify Shipping!'  . PHP_EOL;
    }

    /**
     * @return EventSubscription
     */
    public function subscription(): EventSubscription
    {
        return Subscription::fromEventNameAndPriority('PaymentReceived', 50);
    }
});

```

#### Dispatch an Event:

[](#dispatch-an-event)

Dispatching an `Event` causes the `EventCourier` to notify all `EventSubscriber`s, who's `Subscription::event()` method matches the `Event`'s name, specified by `Event:name()`, and calls their `EventSubscriber::on($event)` method in order of priority.

IMPORTANT: `EventSubscriber`'s and the `EventCourier` never return anything.

```
...

// The EventSubscriber was subscribed

echo 'Payment received.'  . PHP_EOL;
$courier->dispatch(new class implements Event
{
    public function payload(): EventPayload
    {
        // the payload should of course be built or set in the constructor
        return Payload::fromArray(['price' => '99.99€', 'method' => 'creditCard', 'timestamp' => '12345678']);
    }

    public function name(): string
    {
        // best practice would be using the fully qualified class name MyEvent::class
        return 'PaymentReceived';
    }
});

```

Result:

```
Payment received.
Notify Shipping!

```

#### Full Example:

[](#full-example)

```
