PHPackages                             fergusinlondon/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. fergusinlondon/events

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

fergusinlondon/events
=====================

A very simple Events Handling system.. so simple it's essentially a toy.

1.0.0(9y ago)013GPLPHP

Since May 18Pushed 9y agoCompare

[ Source](https://github.com/FergusInLondon/Events)[ Packagist](https://packagist.org/packages/fergusinlondon/events)[ RSS](/packages/fergusinlondon-events/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (4)Versions (2)Used By (0)

Events: A simple events registry and handling system
====================================================

[](#events-a-simple-events-registry-and-handling-system)

[![Build Status](https://camo.githubusercontent.com/5e44d382da2e27ea2cc72b003326057110f7fe621066e951e64533cd3c06f47b/68747470733a2f2f7472617669732d63692e6f72672f466572677573496e4c6f6e646f6e2f4576656e74732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/FergusInLondon/Events) [![Test Coverage](https://camo.githubusercontent.com/0f0401aece06416f4cd23448bc57c9787655f0f0cc3b1648eaf3a82128eca52e/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f466572677573496e4c6f6e646f6e2f4576656e74732f6261646765732f636f7665726167652e737667)](https://codeclimate.com/github/FergusInLondon/Events/coverage) [![Code Climate](https://camo.githubusercontent.com/ec6a4e3f51f94d7d9b454e3a36f8b4c0d5f9cde1938ff5d1b58cd9372e3941d7/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f466572677573496e4c6f6e646f6e2f4576656e74732f6261646765732f6770612e737667)](https://codeclimate.com/github/FergusInLondon/Events) [![Issue Count](https://camo.githubusercontent.com/8b25121b13e8c32ce6e28758e2029fd45c82583bfe78ca6915adcf3e143a4a1b/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f466572677573496e4c6f6e646f6e2f4576656e74732f6261646765732f69737375655f636f756e742e737667)](https://codeclimate.com/github/FergusInLondon/Events)

This is an incredibly simple events registry and handling system, which is composed of a total of two objects: a registry object, and a subclassable base handler.

The primary intention behind this project was for a three part blog post about writing small modular components, packaging them as composer modules and subsequently using a continuous integration system to ensure their status.

Despite this original intention, if you need an incredibly lightweight system for registering and handling events, then this will most likely suffice!

On the other hand, if you're powering something that is larger and has an excess of events, then you may wish to consider an option which *compiles* your events in to a map - allowing better performance.

### Blog Post(s)

[](#blog-posts)

The blog posts are not yet written, but will be made available on [Fergus.London](https://fergus.london).

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

[](#installation)

Install via Composer, as `fergusinlondon/events`.

> $ composer require fergusinlondon/events=dev-master
>
> Warning: This development build of composer is over 60 days old. It is recommended to update it by running "/usr/local/bin/composer self-update" to get the latest version.
> ./composer.json has been updated
>
> Loading composer repositories with package information
>
> Updating dependencies (including require-dev)
>
> - Installing fergusinlondon/events (dev-master b245be2)
>     Cloning b245be2bae5e546cbd33a279c17ff0652aca33f8

Example:
--------

[](#example)

The best form of documentation is usually an example.

### Basic

[](#basic)

A basic example could be a User registration/creation event, whereby when a user signs up to your application an event is fired that allows other parts of the application to respond in some way.

```
use FergusInLondon\Events\Registry;
use FergusInLondon\Events\Handler;

$registry = new Registry();

$registry->registerHandler("user.creation", new Handler(function($user){
	$this->container->get('email')->sendWelcome( $user->email_address );
}));

$registry->registerHandler("", new Handler(function($user){
    $this->container->get('logger')->info(
    	sprintf("New user registration: %s (%d)", $user->name, $user->id)
    );
}));
```

Meanwhile, in our associated controller we would simply trigger the event when required..

```
class UserController {
	public function create() {
		// ...
		$user->save();
		$registry->trigger("user.creation", $user);

	}
}
```

### Full API

[](#full-api)

Alternatively, the example below demonstrates the entirety of the API and feature-set. There are public methods open on these objects which *should not be used*: and are there for `registryhandler` communications. These methods will be marked as such in the source code.

```
use FergusInLondon\Events\Registry;
use FergusInLondon\Events\Handler;

$registry = new Registry();

// As Handler objects are - by default - initialised with a Callable parameter. This is overridable via subclassing.
$registry->registerHandler("event.demo", new Handler(function(){
    echo "See, this is a very simple event handler.\n";
}));
$registry->registerHandler("event.never", new Handler(function(){
    echo "This will never run, as we'll clear all handlers first.\n";
}));

// Handlers can be instantiated inline, and access parameters passed in via Registry::trigger()
$userDeleteHandler = new Handler(function($name, $id){
    printf("User created: %s (%d)\n", $name, $id);
});

// Handlers also gain the context of the Handler object. Especially useful if you need to subclass and/or access utility methods.
$userCreateHandler = new Handler(function($name, $id){
	printf("User created: %s (%d)\n", $name, $id);
	printf(
	    "Handlers have access to the current Handler object too. (i.e %s)",
	    $this->registryIdentifier
	);
});

// Registering instantiated handlers.
$registry->registerHandler("user.create", $userCreateHandler);
$registry->registerHandler("user.delete", $userDeleteHandler);

// The clearHandlers method is capable of
$registry->trigger("event.demo");
$registry->clearHandlers("event.demo");
$registry->trigger("event.demo");

// It's possible to trigger events whilst passing data to the handler
$user = ["name" => "J. Smith", "id" => 101];
$registry->trigger("user.create", $user);
$registry->trigger("user.delete", $user);

// If clearHandlers is called without any parameters, all handlers are cleared
$registry->clearHandlers();
$registry->trigger("event.never");
```

Testing
-------

[](#testing)

Testing is done via PHPSpec, and tests that the public interface works as expected - but makes no guarantees as to the internal workings. As such, if you wish to use this project please stick to the public API as seen in the above example(s).

A [full build history](https://travis-ci.org/FergusInLondon/Events/builds) for this project is available on Travis CI.

License
-------

[](#license)

All code contained in this repository is licensed under **[The MIT License](https://opensource.org/licenses/MIT)**.

> Copyright © 2016 Fergus Morrow
>
> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

26

—

LowBetter than 46% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3597d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/68a285516869a33ba5c1d0970db092d4da93f4e657fb28c6ed5bb88755502216?d=identicon)[FergusInLondon](/maintainers/FergusInLondon)

---

Top Contributors

[![FergusInLondon](https://avatars.githubusercontent.com/u/16191346?v=4)](https://github.com/FergusInLondon "FergusInLondon (2 commits)")

###  Code Quality

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/fergusinlondon-events/health.svg)

```
[![Health](https://phpackages.com/badges/fergusinlondon-events/health.svg)](https://phpackages.com/packages/fergusinlondon-events)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
