/league-lazy-event

:zzz: Provides a LazyListener for use with League\Event which allows for lazy fetching of actual listeners.

Primary LanguagePHPMIT LicenseMIT

league-lazy-event

Build Status Code Climate Test Coverage Latest Stable Version Total Downloads

This repository provides a LazyListener for use with league/event, which allows for lazy fetching of an actual listener from the composed container.

Installation

Run

$ composer require refinery29/league-lazy-event

Usage

Register your actual listener as a service with the container:

use League\Container;

$container = new Container();

$container->share(ExpensiveListener::class, function () {
    /*
     * here, some heavy lifting occurs that creates the actual listener,
     * which should implement the ListenerInterface
     */
    return $listener;
});

Then register a LazyListener, composing the alias and the container:

use League\Event\Emitter;
use Refinery29\Event\LazyListener;

$emitter->addListener(ContentChangedEvent::class, LazyListener::fromAlias(
    ExpensiveListener::class,
    $container
));

Trigger your events as needed!

$emitter->emit(ContentChangedEvent::class, new ContentChangedEvent(
    $url,
    new DateTimeImmutable()
);

👍 Listeners are only ever fetched from the container when the event is handled.

Contributing

Please have a look at CONTRIBUTING.md.

Code of Conduct

Please have a look at CONDUCT.md.

License

This package is licensed using the MIT License.