slick/event

Simple PSR-14 event handling implementation.

v1.1.1 2025-06-17 17:38 UTC

This package is auto-updated.

Last update: 2025-06-17 17:38:37 UTC


README

Latest Version License: MIT CI Status Quality Score Downloads

Slick Event is a modern and lightweight PHP library that provides a clean, flexible, and PSR-14-compliant event dispatching system. Built for developers who value simplicity, testability, and performance, it allows you to decouple your application logic using event-driven architecture without the overhead of heavier frameworks.

Whether you're working with a microservice, a modular monolith, or a full-stack application, Slick Event integrates seamlessly and gives you the tools to manage domain events, listeners, and dispatchers with ease.

โœจ Features

  • โœ… PSR-14 compliant: Follows the PHP-FIG Event Dispatcher standard for interoperability.
  • ๐Ÿ” Synchronous event dispatching with support for multiple listeners.
  • โš™๏ธ Simple and intuitive API for registering and dispatching events.
  • ๐Ÿงช Spec-driven development using PHPSpec ensures reliability and clean design.
  • ๐Ÿงฉ Framework-agnostic: Use it in any PHP project, regardless of framework.

This package follows:

๐Ÿš€ Installation

Install via Composer:

composer require slick/event

๐Ÿงฉ Basic Usage

use Slick\Event\EventDispatcher;
use Slick\Event\Event;

$dispatcher = new EventDispatcher();

$dispatcher->listen(SomeEvent::class, function (SomeEvent $event) {
    // Handle event
});

// Dispatch event
$dispatcher->dispatch(new SomeEvent());

โœ… Usage

1. Define an Event and a Listener

namespace App\Domain\Event;

final class UserWasRegistered
{
    public function __construct(public string $email) {}
}
namespace App\Application\Listener;

use App\Domain\Event\UserWasRegistered;
use Slick\Event\Application\Attribute\AsEventListener;

#[AsEventListener(event: UserWasRegistered::class)]
final class SendWelcomeEmail
{
    public function __invoke(UserWasRegistered $event): void
    {
        // Send welcome email to $event->email
    }
}

2. Set Up the ListenerProvider and Dispatcher

use Slick\Event\Infrastructure\AttributeListenerResolver;
use Slick\Event\Infrastructure\AttributeListenerProvider;
use Slick\Event\EventDispatcher;
use App\YourContainer;

$container = new YourContainer(); // PSR-11 compliant

$resolver = new AttributeListenerResolver(
    __DIR__.'/src/Application/Listener',
    $container
);

$provider = new AttributeListenerProvider($resolver);
$dispatcher = new EventDispatcher($provider);

3. Dispatch the Event

$dispatcher->dispatch(new UserWasRegistered('user@example.com'));

All listeners discovered via #[AsEventListener] will be automatically executed.

โœ… Testing

We use PHPSpec for unit testing.

Run tests with:

vendor/bin/phpspec run

or

composer test

๐Ÿค Contributing

Please read our CONTRIBUTING.md guidelines.

๐Ÿ›ก Security

If you discover a security vulnerability, please email us at slick.framework@gmail.com instead of using the issue tracker.

๐Ÿ™ Credits

๐Ÿ“„ License

This package is open-source software licensed under the MIT License.