<commit_before># league-lazy-event

[![Build Status](https://travis-ci.org/refinery29/league-lazy-event.svg?branch=master)](https://travis-ci.org/refinery29/league-lazy-event)
[![Code Climate](https://codeclimate.com/github/refinery29/league-lazy-event/badges/gpa.svg)](https://codeclimate.com/github/refinery29/league-lazy-event)
[![Test Coverage](https://codeclimate.com/github/refinery29/league-lazy-event/badges/coverage.svg)](https://codeclimate.com/github/refinery29/league-lazy-event/coverage)

This repository provides a `LazyListener` for use with [`league/event`](http://github.com/thephpleague/event), which 
allows for lazy fetching of an actual listener from the composed container.<commit_msg>Fix: Update README.md<commit_after># league-lazy-event

[![Build Status](https://travis-ci.org/refinery29/league-lazy-event.svg?branch=master)](https://travis-ci.org/refinery29/league-lazy-event)
[![Code Climate](https://codeclimate.com/github/refinery29/league-lazy-event/badges/gpa.svg)](https://codeclimate.com/github/refinery29/league-lazy-event)
[![Test Coverage](https://codeclimate.com/github/refinery29/league-lazy-event/badges/coverage.svg)](https://codeclimate.com/github/refinery29/league-lazy-event/coverage)

This repository provides a `LazyListener` for use with [`league/event`](http://github.com/thephpleague/event), which 
allows for lazy fetching of an actual listener from the composed container.

## Installation

Reference the package location `composer.json` (not on Packagist yet):

```json
{
    "repositories": [
        {
            "type": "vcs",
            "url": "<EMAIL>:refinery29/league-lazy-event"
        }
    ]
}
```

Run

```
$ composer require refinery29/league-lazy-event
```

## Usage

Register your actual listener as a service with the container:

```php
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:

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

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

Trigger your events as needed!

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

:+1: Listeners are only ever fetched from the container when the event is handled.
{'fraction_non_alphanumeric': 0.14527938342967245, 'fraction_numerical': 0.011946050096339113, 'mean_word_length': 4.820627802690583, 'pattern_counts': {'":': 3, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 12, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4057206', 'n_tokens_mistral': 870, 'n_tokens_neox': 817, 'n_words': 162}