PHPackages                             icanboogie/event - 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. icanboogie/event

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

icanboogie/event
================

Provides an event API

v4.1.0(4y ago)09.0k↓84.6%7BSD-3-ClausePHPPHP &gt;=7.2CI failing

Since Nov 7Pushed 1y ago1 watchersCompare

[ Source](https://github.com/ICanBoogie/Event)[ Packagist](https://packagist.org/packages/icanboogie/event)[ Docs](https://icanboogie.org/)[ RSS](/packages/icanboogie-event/feed)WikiDiscussions 6.0 Synced 3w ago

READMEChangelog (5)Dependencies (3)Versions (14)Used By (7)

Event
=====

[](#event)

[![Release](https://camo.githubusercontent.com/286314c8d98c66202a5099a536eb26661cb39bcaeeff8c2b4ed0c54fd0924784/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6963616e626f6f6769652f6576656e742e737667)](https://packagist.org/packages/icanboogie/event)[![Code Coverage](https://camo.githubusercontent.com/3823473b2526d308a3ec5bbb02959c2bf248919a01dea743960331c0077dad34/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f4943616e426f6f6769652f4576656e742f62616467652e7376673f6272616e63683d362e30)](https://coveralls.io/r/ICanBoogie/Event?branch=6.0)[![Downloads](https://camo.githubusercontent.com/e535289c1c1ac28ed0f3c3e6046f224b95635a269b2dd0ba36dad70d9a3cc192/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6963616e626f6f6769652f6576656e742e737667)](https://packagist.org/packages/icanboogie/event)

The **icanboogie/event** allows you to provide hooks which other developers can attach to, to be notified when certain events occur inside the application and take action.

Inside [ICanBoogie](https://icanboogie.org/), events are often used to alter initial parameters, take action before/after an operation is processed or when it fails, take action before/after a request is dispatched or to rescue an exception.

#### Installation

[](#installation)

```
composer require icanboogie/event
```

### Feature highlights

[](#feature-highlights)

- Easily implementable.
- Events are typed.
- Events usually have a target object, but simpler event types can also be emitted.
- Event hooks are attached to classes rather than objects, and they are inherited.
- Event hooks can be attached to a *finish chain* that is executed after the event hooks chain.
- Execution of the event chain can be stopped.

A twist on the Observer pattern
-------------------------------

[](#a-twist-on-the-observer-pattern)

The pattern used by the API is similar to the [Observer pattern](http://en.wikipedia.org/wiki/Observer_pattern), although instead of attaching event hooks to objects they are attached to their class. When an event is fired upon a target object, the hierarchy of its class is used to filter event hooks.

Consider the following class hierarchy:

```
ICanBoogie\Operation
└─ ICanBoogie\Module\Operation\SaveOperation
    └─ Icybee\Modules\Node\Operation\SaveOperation
        └─ Icybee\Modules\Content\Operation\SaveOperation
            └─ Icybee\Modules\News\Operation\SaveOperation

```

When a `ProcessEvent` is emitted with a `…\News\Operation\SaveOperation` instance, all event hooks attached to the classes for this event are called, starting from the event hooks attached to the instance class (`…\News\Operation\SaveOperation`) all the way up to those attached to its root class.

Thus, event hooks attached to the `…\Node\Operation\SaveOperation` class are called when a `ProcessEvent` event is fired with `…\News\Operation\SaveOperation` instance. One could consider that event hooks are *inherited*.

Getting started
---------------

[](#getting-started)

To be emitted, events need an event collection, which holds event hooks. Because a new event collection is created for you when required, you don't need to set one up yourself. Still, you might want to do so if you have a bunch of event hooks that you need to attach while creating the event collection. To do so, you need to define a *provider* that returns your event collection when required.

The following example demonstrates how to set up a provider that instantiates an event collection with event hooks provided by an app configuration:

```
