File: lazy-listener-aggregate.md

package info (click to toggle)
php-zend-eventmanager 3.14.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 896 kB
  • sloc: php: 3,542; xml: 696; makefile: 17
file content (93 lines) | stat: -rw-r--r-- 2,817 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# LazyListenerAggregate

`Laminas\EventManager\LazyListenerAggregate` exists to facilitate attaching a
number of listeners as lazy listeners.

## Usage

Similar to a [LazyListener](lazy-listener.md) or
[LazyEventListener](lazy-event-listener.md), the `LazyListenerAggregate` accepts
a definition (or, rather, set of definitions), a PSR-11 container instance, and
optionall an `$env` array to its constructor.

Unlike either, however, the definition provided is an array of definitions to
use to create `LazyEventListener` instances; you may also intersperse actual
`LazyEventListener` instances if desired.

As an example, let's assume we have two listeners,
`My\Application\RouteListener` and `My\Application\DispatchListener`; the first
will use its `onRoute()` method to listen to the `route` event at priority 100,
the second its `onDispatch()` method to listen to the `dispatch` event at
priority -100.

```php
use My\Application\DispatchListener;
use My\Application\RouteListener;
use Laminas\EventManager\LazyListenerAggregate;

$definitions = [
    [
        'listener' => RouteListener::class,
        'method'   => 'onRoute',
        'event'    => 'route',
        'priority' => 100,
    ],
    [
        'listener' => DispatchListener::class,
        'method'   => 'onDispatch',
        'event'    => 'dispatch',
        'priority' => -100,
    ],
];

$aggregate = new LazyListenerAggregate(
    $definitions,
    $container
);
$aggregate->attach($events);
```

Internally, the `LazyListenerAggregate` will create `LazyEventListener`
instances, and during its `attach()` phase use them to attach to the event
manager using the event and priority they compose.

Below is a functionally identical example, mixing in a concrete
`LazyEventListener` instance for one listener:

```php
use My\Application\DispatchListener;
use My\Application\RouteListener;
use Laminas\EventManager\LazyEventListener;
use Laminas\EventManager\LazyListenerAggregate;

$dispatchListener = new LazyEventListener([
    'listener' => DispatchListener::class,
    'method'   => 'onDispatch',
    'event'    => 'dispatch',
    'priority' => -100,
], $container);

$definitions = [
    [
        'listener' => RouteListener::class,
        'method'   => 'onRoute',
        'event'    => 'route',
        'priority' => 100,
    ],
    $dispatchListener,
];

$aggregate = new LazyListenerAggregate(
    $definitions,
    $container
);
$aggregate->attach($events);
```

## Recommendations

We recommend using `LazyListenerAggregate` when you have listeners you will be
pulling from a Dependency Injection Container, but which may not execute on
every request; this will help minimize the number of objects pulled from the
DIC. As pulling instances from a DIC is often an expensive operation, this can
be a healthy performance optimization.