Skip to content

refinery29/league-lazy-event

master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published