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

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

tobento/service-event
=====================

A PSR-14 event dispatcher with autowiring support.

2.0(9mo ago)015710MITPHPPHP &gt;=8.4

Since Dec 21Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/tobento-ch/service-event)[ Packagist](https://packagist.org/packages/tobento/service-event)[ Docs](https://www.tobento.ch)[ RSS](/packages/tobento-service-event/feed)WikiDiscussions 2.x Synced today

READMEChangelog (3)Dependencies (6)Versions (5)Used By (10)

Event Service
=============

[](#event-service)

A PSR-14 event dispatcher with autowiring support.

Table of Contents
-----------------

[](#table-of-contents)

- [Getting started](#getting-started)
    - [Requirements](#requirements)
    - [Highlights](#highlights)
    - [Simple Example](#simple-example)
- [Documentation](#documentation)
    - [Listeners](#listeners)
        - [Create Listeners](#create-listeners)
        - [Defining And Add Listener](#defining-and-add-listener)
        - [Retrieve Listeners](#retrieve-listeners)
    - [Dispatcher](#dispatcher)
        - [Create Dispatcher](#create-dispatcher)
        - [Dispachting Events](#dispatching-events)
    - [Events](#events)
        - [Create Events](#create-events)
        - [Add Listeners](#add-listeners)
        - [Dispatch Events](#dispatch-events)
        - [Supporting Events](#supporting-events)
- [Credits](#credits)

---

Getting started
===============

[](#getting-started)

Add the latest version of the event service running this command.

```
composer require tobento/service-event

```

Requirements
------------

[](#requirements)

- PHP 8.4 or greater

Highlights
----------

[](#highlights)

- Framework-agnostic, will work with any project
- Decoupled design
- Autowiring support

Simple Example
--------------

[](#simple-example)

Here is a simple example of how to use the Event service.

```
use Tobento\Service\Event\Dispatcher;
use Tobento\Service\Event\Listeners;

class FooEvent {}

$listeners = new Listeners();

$listeners->add(function(FooEvent $event) {
    // do something
});

$dispatcher = new Dispatcher($listeners);

$event = $dispatcher->dispatch(new FooEvent());
```

**Using Events**

```
use Tobento\Service\Event\Events;

class FooEvent {}

$events = new Events();

$events->listen(function(FooEvent $event) {
    // do something
});

$event = $events->dispatch(new FooEvent());
```

Documentation
=============

[](#documentation)

Listeners
---------

[](#listeners)

The listeners class uses reflection to scan listeners for its events.

### Create Listeners

[](#create-listeners)

```
use Tobento\Service\Event\Listeners;
use Tobento\Service\Event\ListenersInterface;
use Tobento\Service\Event\CallableFactoryInterface;
use Tobento\Service\Event\ListenerEventsResolverInterface;
use Psr\EventDispatcher\ListenerProviderInterface;

$listeners = new Listeners(
    callableFactory: null, // null|CallableFactoryInterface
    listenerEventsResolver: null, // null|ListenerEventsResolverInterface
);

var_dump($listeners instanceof ListenersInterface);
// bool(true)

var_dump($listeners instanceof ListenerProviderInterface);
// bool(true)
```

**Using Autowiring**

You might want to use autowiring for creating your listeners.

```
use Tobento\Service\Event\Listeners;
use Tobento\Service\Event\AutowiringCallableFactory;
use Tobento\Service\Container\Container;

class FooListener
{
    public function foo(FooEvent $event): void
    {
        // do something
    }
}

// any PSR-11 container
$container = new Container();

$listeners = new Listeners(
    callableFactory: new AutowiringCallableFactory($container),
);

$listeners->add(FooListener::class);
```

### Defining And Add Listener

[](#defining-and-add-listener)

As the listeners class uses reflection to scan listeners for its events named **$event**, there is no need to define its event(s) when adding a listener. But you might do so if you have multiple events in your listener and want only to listen for the specific events.

**Class using invoke**

```
class FooListener
{
    public function __invoke(FooEvent $event): void
    {
        // do something
    }
}

class FooBuildInListener
{
    public function __construct(protected int $number) {}

    public function __invoke(FooEvent $event): void
    {
        // do something
    }
}

$listeners->add(new FooListener());

// using autowiring:
$listeners->add(FooListener::class);

// using autowiring with build-in parameters:
$listeners->add([FooBuildInListener::class, ['number' => 5]]);
```

**Class defining multiple events to listen**

```
class FooBarListener
{
    public function foo(FooEvent $event): void
    {
        // do something
    }

    public function bar(BarEvent $event): void
    {
        // do something
    }

    public function another(AnotherEvent $event): void
    {
        // do something
    }

    public function fooAndBar(FooEvent|BarEvent $event): void
    {
        // do something
    }
}

// only listen to foo and bar event:
$listeners->add(new FooBarListener())
          ->event(FooEvent::class, BarEvent::class);

// using autowiring:
$listeners->add(FooBarListener::class)
          ->event(FooEvent::class, BarEvent::class);
```

**Using closure**

```
$listeners->add(function(FooEvent $event) {
    // do something
});
```

**Prioritize**

You might prioritize listeners by the following way:

```
$listeners->add(function(FooEvent $event) {
    // do something
})->priority(1500);

// gets called first as higher priority.
$listeners->add(function(FooEvent $event) {
    // do something
})->priority(2000);
```

**Add a custom listener**

You might add a custom listener by implementing the following interface:

```
use Tobento\Service\Event\ListenerInterface;
use Tobento\Service\Event\CallableFactoryInterface;

interface ListenerInterface
{
    /**
     * Returns the listener.
     *
     * @return mixed
     */
    public function getListener(): mixed;

    /**
     * Returns the listener events.
     *
     * @return array
     */
    public function getListenerEvents(): array;

    /**
     * Returns the listeners for the specified event.
     *
     * @param object $event
     * @param CallableFactoryInterface $callableFactory
     * @return iterable
     *   An iterable (array, iterator, or generator) of callables. Each
     *   callable MUST be type-compatible with $event.
     */
    public function getForEvent(object $event, CallableFactoryInterface $callableFactory): iterable;

    /**
     * Returns the priority.
     *
     * @return int
     */
    public function getPriority(): int;
}

$listeners->addListener(new AnyCustomListener());
```

### Retrieve Listeners

[](#retrieve-listeners)

```
use Tobento\Service\Event\ListenerInterface;

foreach($listeners->all() as $listener) {
    var_dump($listener instanceof ListenerInterface);
    // bool(true)
}
```

Dispatcher
----------

[](#dispatcher)

### Create Dispatcher

[](#create-dispatcher)

```
use Tobento\Service\Event\Dispatcher;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\EventDispatcher\ListenerProviderInterface;
use Psr\Container\ContainerInterface;
use Tobento\Service\Event\Listeners;

$listeners = new Listeners();

$dispatcher = new Dispatcher(
    listenerProvider: $listeners, // ListenerProviderInterface
    container: null, // null|ContainerInterface
);

var_dump($dispatcher instanceof EventDispatcherInterface);
// bool(true)
```

**Using Autowire**

You might set a container for autowiring events methods. This will break PSR-14 definition although.

```
use Tobento\Service\Event\Dispatcher;
use Tobento\Service\Event\Listeners;
use Tobento\Service\Container\Container;

class FooEvent {}
class Bar {}

$listeners = new Listeners();

// adding more parameters after the $event.
$listeners->add(function(FooEvent $event, Bar $bar) {
    // do something
});

// Any PSR-11 container
$container = new Container();

$dispatcher = new Dispatcher(
    listenerProvider: $listeners,
    container: $container,
);

$dispatcher->dispatch(new FooEvent());
```

### Dispatching Events

[](#dispatching-events)

```
$dispatcher->dispatch(new AnyEvent());
```

Events
------

[](#events)

### Create Events

[](#create-events)

```
use Tobento\Service\Event\Events;
use Tobento\Service\Event\EventsInterface;
use Tobento\Service\Event\ListenersInterface;
use Tobento\Service\Event\DispatcherFactoryInterface;
use Psr\EventDispatcher\EventDispatcherInterface;

$events = new Events(
    listeners: null, // null|ListenersInterface
    dispatcherFactory: null, // null|DispatcherFactoryInterface
);

var_dump($events instanceof EventsInterface);
// bool(true)

var_dump($events instanceof EventDispatcherInterface);
// bool(true)
```

**Using the events factory**

```
use Tobento\Service\Event\EventsFactory;
use Tobento\Service\Event\EventsFactoryInterface;
use Tobento\Service\Event\ListenersInterface;
use Tobento\Service\Event\DispatcherFactoryInterface;
use Tobento\Service\Event\EventsInterface;

$eventsFactory = new EventsFactory();

var_dump($eventsFactory instanceof EventsFactoryInterface);
// bool(true)

$events = $eventsFactory->createEvents(
    listeners: null, // null|ListenersInterface
    dispatcherFactory: null, // null|DispatcherFactoryInterface
);

var_dump($events instanceof EventsInterface);
// bool(true)
```

**Using the autowiring events factory**

```
use Tobento\Service\Event\AutowiringEventsFactory;
use Tobento\Service\Event\EventsFactoryInterface;
use Tobento\Service\Event\EventsInterface;
use Tobento\Service\Container\Container;

// Any PSR-11 container
$container = new Container();

$eventsFactory = new AutowiringEventsFactory(
    container: $container,
    withAutowiringDispatching: true,
);

var_dump($eventsFactory instanceof EventsFactoryInterface);
// bool(true)

$events = $eventsFactory->createEvents();

var_dump($events instanceof EventsInterface);
// bool(true)
```

### Add Listeners

[](#add-listeners)

**Using listen method**

```
$events->listen(FooListener::class);

$events->listen(AnyListener::class)
       ->event(FooEvent::class)
       ->priority(2000);
```

**Using listeners method**

```
use Tobento\Service\Event\ListenersInterface;

$listeners = $events->listeners();

var_dump($listeners instanceof ListenersInterface);
// bool(true)

$listeners->add(AnyListener::class);
```

For more detail see [Defining And Add Listener](#defining-and-add-listener)

### Dispatch Events

[](#dispatch-events)

```
$events->dispatch(new AnyEvent());
```

### Supporting Events

[](#supporting-events)

You might use the HasEvents trait with the EventsAware interface for any classes supporting events.

```
use Tobento\Service\Event\EventsAware;
use Tobento\Service\Event\HasEvents;
use Tobento\Service\Event\EventsInterface;
use Tobento\Service\Event\Events;

class AnyService implements EventsAware
{
    use HasEvents;

    public function __construct(EventsInterface $events)
    {
        $this->events = $events;
    }
}

$service = new AnyService(new Events());

var_dump($service->events() instanceof EventsInterface);
// bool(true)
```

Credits
=======

[](#credits)

- [Tobias Strub](https://www.tobento.ch)
- [All Contributors](../../contributors)

###  Health Score

42

—

FairBetter than 88% of packages

Maintenance57

Moderate activity, may be stable

Popularity12

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity71

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

Every ~343 days

Total

5

Last Release

282d ago

Major Versions

1.x-dev → 2.02025-09-24

PHP version history (2 changes)1.0.0PHP &gt;=8.0

2.0PHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/055d6a1b5c2384bb179c75ab0b55914231d898fdc4dffeb30770f81200e52206?d=identicon)[TOBENTOch](/maintainers/TOBENTOch)

---

Top Contributors

[![tobento-ch](https://avatars.githubusercontent.com/u/16684832?v=4)](https://github.com/tobento-ch "tobento-ch (6 commits)")

---

Tags

Autowiringpackagepsr-14event dispatchertobento

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tobento-service-event/health.svg)

```
[![Health](https://phpackages.com/badges/tobento-service-event/health.svg)](https://phpackages.com/packages/tobento-service-event)
```

###  Alternatives

[symfony/symfony

The Symfony PHP framework

31.4k87.2M2.2k](/packages/symfony-symfony)[deptrac/deptrac

Deptrac is a static code analysis tool that helps to enforce rules for dependencies between software layers.

3.0k8.8M118](/packages/deptrac-deptrac)[mcp/sdk

Model Context Protocol SDK for Client and Server applications in PHP

1.5k1.5M88](/packages/mcp-sdk)[drupal/core-recommended

Locked core dependencies; require this project INSTEAD OF drupal/core.

6942.5M421](/packages/drupal-core-recommended)[ecotone/ecotone

Enterprise architecture layer for Laravel and Symfony — CQRS, Event Sourcing, Durable Workflows (Sagas, Orchestrators), Projections, and Outbox messaging via PHP attributes.

564576.7k52](/packages/ecotone-ecotone)[testo/testo

A lightweight PHP testing framework.

1959.3k55](/packages/testo-testo)

PHPackages © 2026

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