PHPackages                             buonaparte/bnp-lazy-listener - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. buonaparte/bnp-lazy-listener

ActiveLibrary

buonaparte/bnp-lazy-listener
============================

This library allows you to attach Lazy Listeners to a ZF2 EventManager

0.9.1(11y ago)2271BSD-3-ClausePHPPHP &gt;=5.3.3

Since Jul 23Pushed 11y ago1 watchersCompare

[ Source](https://github.com/buonaparte/BnpLazyListener)[ Packagist](https://packagist.org/packages/buonaparte/bnp-lazy-listener)[ RSS](/packages/buonaparte-bnp-lazy-listener/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (2)Dependencies (5)Versions (3)Used By (0)

BnpLazyListener
===============

[](#bnplazylistener)

[![Build Status](https://camo.githubusercontent.com/06cd7532eb9f6a6c9cf8b7cf8b4b1b6868c83df312bfcbc9d9e4e71ade14738f/68747470733a2f2f7472617669732d63692e6f72672f62756f6e6170617274652f426e704c617a794c697374656e65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/buonaparte/BnpLazyListener)[![Coverage Status](https://camo.githubusercontent.com/e67ad865bd9e1d45cf2c1ed5e1cfe91c63e78f8391a0110fbdcbc2cfe2d32d3b/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f62756f6e6170617274652f426e704c617a794c697374656e65722e737667)](https://coveralls.io/r/buonaparte/BnpLazyListener?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/0e923a6d99a0efac19757e185f76f68eddbf142f6bab3eefaa49e9240f477c59/68747470733a2f2f706f7365722e707567782e6f72672f62756f6e6170617274652f626e702d6c617a792d6c697374656e65722f762f737461626c652e737667)](https://packagist.org/packages/buonaparte/bnp-lazy-listener)[![Latest Unstable Version](https://camo.githubusercontent.com/867a62135c32238a4fbfa537ee2c1099fddef0eb9cddf7a8cd533cb814f50f8b/68747470733a2f2f706f7365722e707567782e6f72672f62756f6e6170617274652f626e702d6c617a792d6c697374656e65722f762f756e737461626c652e737667)](https://packagist.org/packages/buonaparte/bnp-lazy-listener)

This library allows you to attach Lazy Listeners to a ZF2 EventManager.

Installation
------------

[](#installation)

### Setup

[](#setup)

1. Add this project to your composer.json:

    ```
    "require": {
        "buonaparte/bnp-lazy-listener": "0.9.*"
    }
    ```
2. Now tell composer to download BnpServiceDefinition by running the command:

    ```
    $ php composer.phar update
    ```

**!!! Notice** this is not a Module, so you do not have to enable it in a ZF2 Application, moreover, you can use it with the `EventManager` decoupled component.

LazyListenerAggregate
---------------------

[](#lazylisteneraggregate)

There are many cases when instantiating a listener aggregate may be too expensive, especially when that listener will be never triggered, `LazyListenerAggregate` makes it possible to define a factory for your real listener, as well as specifications for which events the future listener is subscribed, `LazyListenerAggregate` will then instantiate your listener when the first of subscribed is being triggered.

```
class PlainPhpObjectWithDependencies
{
    // Your Dependencies

    public function __construct($aDependency, $anotherDependency)
    {
        // ...
    }

    public function setExtraDependency($dependency)
    {
        // ...
    }

    public function onFoo(Zend\EventManager\EventInterface $e)
    {
        // do some complex stuff ...
        $e->setParam('foo', 'bar');
    }

    public function onBar(Zend\EventManager\EventInterface $e)
    {
        // do some complex stuff ...
        $e->setParam('bar', 'baz');
    }

    public function onBaz(Zend\EventManager\EventInterface $e)
    {
        // do some complex stuff ...
        $e->setParam('baz', array_merge($e->getParam('baz', array()), array('element'));
    }
}
```

Now you can attach this PPO to the `EventManager`:

```
use Zend\EventManager\EventManager;
use Zend\EventManager\Event;
use BnpLazyListener\LazyListenerAggregate;

$events = new EventManager();

$events->attach(new LazyListenerAggregate(
    function () use ($aDependency, $anotherDependency, $dependency) {
        $listener = new PlainPhpObjectWithDependencies($aDependency, $anotherDependency);
        $listener->setExtraDependency($dependency);

        return $listener;
    },
    array(
        'foo' => 'onFoo',
        'bar' => array('onBar', 1000),
        'baz' => array(
            'onBaz',
            array('onBaz', -99)
        )
    )
));

$events->trigger(new Event('an_event'));

// PlainPhpObjectWithDependencies gets instantiated only now
$events->trigger(new Event('foo'));
```

We've used `\Closure` here as factory, `LazyListenerAggregate` however, accepts any valid `callable`. As you can already see this does not bring much flexibility, as we wrap statically all our listener dependencies, here is where the second utility listener comes in handy.

ServicesListenerAggregateCollection
-----------------------------------

[](#serviceslisteneraggregatecollection)

This is a `ServiceLocatorAware` service that is able to aggregate a collection of other `ListenerAggregate`s, from which, the most important one is that described above. All atom listeners are called delegates and can be represented as:

- `callable` - a callable to invoke to instantiate the delegate, optionally a `ServiceLocatorInterface`is passed as the only argument
- `string` (a valid FQ class name implementing `Zend\EventManager\ListenerAggregateInterface`)
- `string` (a valid FQ class name implementing `Zend\ServiceManager\FactoryInterface`)
- `Zend\ServiceManager\FactoryInterface` instance
- `string` - not matching a factory class name - will make `ServicesListenerAggregateCollection` to pull the delegate as a service from currently injected locator
- `array` - containing 2 elements, the first one - any of above, to use as a lazy factory for the `LazyListenerAggregate`and the second - an array defining the events being listened to, the second constructor argument for the `LazyListenerAggregate`

We can now define a bunch of listeners at a time using only a configuration array:

```
use Zend\ServiceManager\ServiceManager;
use Zend\EventManager\EventManager;
use BnpLazyListener\ServicesListenerAggregateCollection;

$delegates = array(
    'a_listener_aggregate_service',
    'MyApp\Factory\ListenerAggregateFactoryService',
    array(
        'PlainPhpObjectWithDependencies',
        array(
            'foo' => 'onFoo',
            'bar' => array('onBar', 1000),
            'baz' => array(
                'onBaz',
                array('onBaz', -99)
            )
        )
    )
);

$services = new ServiceManager();
// declare your a_listener_aggregate_service and PlainPhpObjectWithDependencies services in the container

$listener = new ServicesListenerAggregateCollection($delegates);
$listener->setServiceLocator($services);

$events = new EventManager();
$events->attach($listener);

$events->trigger(new Event('an_event'));

// PlainPhpObjectWithDependencies gets pulled from the locator only now
$events->trigger(new Event('bar'));
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

4311d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1dcc19ac57d82d04603ee017d1b82ea1d3203e000ef28d391dc4df79d5a5e092?d=identicon)[buonaparte](/maintainers/buonaparte)

---

Top Contributors

[![buonaparte](https://avatars.githubusercontent.com/u/886054?v=4)](https://github.com/buonaparte "buonaparte (8 commits)")

---

Tags

eventmanagerzf2lazy

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/buonaparte-bnp-lazy-listener/health.svg)

```
[![Health](https://phpackages.com/badges/buonaparte-bnp-lazy-listener/health.svg)](https://phpackages.com/packages/buonaparte-bnp-lazy-listener)
```

###  Alternatives

[zf-commons/zfc-base

A set of genetic (abstract) classes which are commonly used across multiple modules.

1441.1M25](/packages/zf-commons-zfc-base)[zfr/zfr-cors

Zend Framework module that let you deal with CORS requests

611.2M3](/packages/zfr-zfr-cors)[socalnick/scn-social-auth

Uses the HybridAuth PHP library to Enable authentication via Google, Facebook, Twitter, Yahoo!, etc for the ZfcUser ZF2 module.

21974.2k3](/packages/socalnick-scn-social-auth)[hounddog/doctrine-data-fixture-module

Zend Framework 2 Module that provides Doctrine Data-Fixture functionality

37335.4k9](/packages/hounddog-doctrine-data-fixture-module)[stroker/cache

Provides a full page cache solution for Laminas

6444.7k](/packages/stroker-cache)[snapshotpl/zf-snap-php-debug-bar

PHP Debug Bar module for Zend Framework 2

3026.1k](/packages/snapshotpl-zf-snap-php-debug-bar)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
