refinery29/league-lazy-event

Provides a LazyListener for use with league/event which allows for lazy fetching of actual listeners.

0.3.0 2017-03-06 17:09 UTC

This package is not auto-updated.

Last update: 2024-04-13 16:23:43 UTC


README

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.