Skip to content

PSR-14 Event Dispatcher for Neos Flow - Symfony EventDispatcher with autodiscover

Notifications You must be signed in to change notification settings

wbsply/event-dispatcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

PSR-14 Event Dispatcher for Neos Flow

Symfony EventDispatcher integrated with auto-discover!

Installation

composer require websupply/event-dispatcher

PSR-14 integration

This package integrates the PSR-14 functionality. But instead of reinventing the wheel, the package implements symfony/event-dispatcher.

How to use it

With the configuration in Configuration/Objects.yaml you can inject/instantiate the Psr\EventDispatcher\EventDispatcherInterface interface

use Psr\EventDispatcher\EventDispatcherInterface;

public function __construct(
    protected readonly EventDispathcerInterface $eventDispathcer
) {}

public function method(string $argument): void
{
    // what ever business logic goes before the dispatching
    $this->eventDispatcher->dispatch(new ProductWasCreated($argument));
}

The Event

A event to dispatch is stripped down to be a plain PHP object and could look like this

namespace Project;

use ValueObject;

final class ProductWasCreated
{
    public function __construct(
        public readonly ValueObject\ProductId $id,
        public readonly ValueObject\ProductName $name
    ) {}
}

The Event Listener

Similar to the Event class, the EventListener is a plain PHP object, but with a important #[EventListener] annotation brought to you by the WebSupply\EventDispatcher\Annotations\EventListener class.

It must implement the __invoke(..) method, with the corresponding event class as argument

This example injects a PSR logger and adds a note about the newly created product

use WebSupply\EventDispatcher\Annotations\EventListener;
use ProductWasCreated;
use Psr\Log\LoggerInterface;

#[EventListener]
final class ProductWasCreatedListener
{

    public function __construct(protected readonly LoggerInterface $logger)
    {}
    public function __invoke(ProductWasCreated $event)
    {
        $this->logger->info('Product was created', ['id' => (string) $event->id]);
    }
}

CLI tool for overview

The command ./flow events:list gives you a list of resolved event and listeners.

If you end up having a ton of events and listeners, you can filter the list, to show listeners based on a single event, by adding a --event "<event-class>" arguments

./flow commands:list --command "ProductWasCreated"

Support and sponsoring

Work on this package is supported by the danish web company WebSupply ApS

About

PSR-14 Event Dispatcher for Neos Flow - Symfony EventDispatcher with autodiscover

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages