Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ghostwriter/event-dispatcher
Provides an Event Dispatcher implementation for PHP
https://github.com/ghostwriter/event-dispatcher
event-dispatcher ghostwriter
Last synced: about 1 month ago
JSON representation
Provides an Event Dispatcher implementation for PHP
- Host: GitHub
- URL: https://github.com/ghostwriter/event-dispatcher
- Owner: ghostwriter
- License: bsd-3-clause
- Created: 2021-11-24T15:08:50.000Z (almost 3 years ago)
- Default Branch: 5.0.x
- Last Pushed: 2024-09-01T21:06:55.000Z (3 months ago)
- Last Synced: 2024-10-03T10:56:53.959Z (about 2 months ago)
- Topics: event-dispatcher, ghostwriter
- Language: PHP
- Homepage: https://github.com/ghostwriter/event-dispatcher
- Size: 1.26 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Event Dispatcher
[![Automation](https://github.com/ghostwriter/event-dispatcher/actions/workflows/automation.yml/badge.svg)](https://github.com/ghostwriter/event-dispatcher/actions/workflows/automation.yml)
[![Supported PHP Version](https://badgen.net/packagist/php/ghostwriter/event-dispatcher?color=8892bf)](https://www.php.net/supported-versions)
[![GitHub Sponsors](https://img.shields.io/github/sponsors/ghostwriter?label=Sponsor+@ghostwriter/event-dispatcher&logo=GitHub+Sponsors)](https://github.com/sponsors/ghostwriter)
[![Mutation Coverage](https://img.shields.io/endpoint?style=flat&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fghostwriter%2Fevent-dispatcher%2Fmain)](https://dashboard.stryker-mutator.io/reports/github.com/ghostwriter/event-dispatcher/main)
[![Code Coverage](https://codecov.io/gh/ghostwriter/event-dispatcher/branch/main/graph/badge.svg)](https://codecov.io/gh/ghostwriter/event-dispatcher)
[![Type Coverage](https://shepherd.dev/github/ghostwriter/event-dispatcher/coverage.svg)](https://shepherd.dev/github/ghostwriter/event-dispatcher)
[![Latest Version on Packagist](https://badgen.net/packagist/v/ghostwriter/event-dispatcher)](https://packagist.org/packages/ghostwriter/event-dispatcher)
[![Downloads](https://badgen.net/packagist/dt/ghostwriter/event-dispatcher?color=blue)](https://packagist.org/packages/ghostwriter/event-dispatcher)Provides an Event Dispatcher implementation for PHP.
## Installation
You can install the package via composer:
``` bash
composer require ghostwriter/event-dispatcher
```### Star ⭐️ this repo if you find it useful
You can also star (🌟) this repo to find it easier later.
### Usage
Registering and dispatching an Event Listener.
```php
use Ghostwriter\EventDispatcher\EventDispatcher;
use Ghostwriter\EventDispatcher\ListenerProvider;// Create an event class
final class ExampleEvent
{
}// Create an Event Listener
final class ExampleEventListener
{
public function __invoke(ExampleEvent $event): void
{
// Handle the event, e.g., print the event class name
// echo $event::class;
}
}// Create a ListenerProvider
$provider = ListenerProvider::new(); // or new ListenerProvider()// Bind the Listener to the Event
$provider->bind(ExampleEvent::class, ExampleEventListener::class);// Create an EventDispatcher
$dispatcher = EventDispatcher::new($provider); // or new EventDispatcher($provider)// Dispatch the Event.
$event = $dispatcher->dispatch(new ExampleEvent());// Assert the Event is the same as the dispatched Event
assert($event instanceof ExampleEvent);
```### Event Subscriber
Registering an Event Subscriber.
```php
use Ghostwriter\EventDispatcher\Interface\ListenerProviderInterface;
use Ghostwriter\EventDispatcher\Interface\SubscriberInterface;
use Override;final class EventSubscriber implements SubscriberInterface {
/**
* @throws Throwable
*/
#[Override]
public function __invoke(ListenerProviderInterface $provider): void
{
// InvokableListener '::__invoke'
$provider->bind(
TestEvent::class,
TestEventListener::class,
);
}
}// Create a ListenerProvider
$provider = ListenerProvider::new(); // or new ListenerProvider()// Subscribe the EventSubscriber
$provider->subscribe(EventSubscriber::class);// Create an EventDispatcher
$dispatcher = EventDispatcher::new($provider); // or new EventDispatcher($provider)// Dispatch the Event.
$event = $dispatcher->dispatch(new TestEvent());// Assert the Event is the same as the dispatched Event
assert($event instanceof TestEvent);
```### Changelog
Please see [CHANGELOG.md](./CHANGELOG.md) for more information what has changed recently.
### Security
If you discover any security related issues, please email `[email protected]` or create a [Security Advisory](https://github.com/ghostwriter/event-dispatcher/security/advisories/new) instead of using the issue tracker.
## License
The BSD-3-Clause. Please see [License File](./LICENSE) for more information.