File: LazyListenerAggregate.php

package info (click to toggle)
php-zend-eventmanager 3.14.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 904 kB
  • sloc: php: 3,542; xml: 696; makefile: 17
file content (102 lines) | stat: -rw-r--r-- 3,153 bytes parent folder | download | duplicates (2)
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
94
95
96
97
98
99
100
101
102
<?php

namespace Laminas\EventManager;

use Psr\Container\ContainerInterface;

use function get_debug_type;
use function is_array;
use function sprintf;

/**
 * Aggregate listener for attaching lazy listeners.
 *
 * Lazy listeners are listeners where creation is deferred until they are
 * triggered; this removes the most costly mechanism of pulling a listener
 * from a container unless the listener is actually invoked.
 *
 * Usage is:
 *
 * <code>
 * $aggregate = new LazyListenerAggregate(
 *     $lazyEventListenersOrDefinitions,
 *     $container
 * );
 * $aggregate->attach($events);
 * </code>
 */
class LazyListenerAggregate implements ListenerAggregateInterface
{
    use ListenerAggregateTrait;

    /**
     * Generated LazyEventListener instances.
     *
     * @var LazyEventListener[]
     */
    private $lazyListeners = [];

    /**
     * Constructor
     *
     * Accepts the composed $listeners, as well as the $container and $env in
     * order to create a listener aggregate that defers listener creation until
     * the listener is triggered.
     *
     * Listeners may be either LazyEventListener instances, or lazy event
     * listener definitions that can be provided to a LazyEventListener
     * constructor in order to create a new instance; in the latter case, the
     * $container and $env will be passed at instantiation as well.
     *
     * @param array $listeners LazyEventListener instances or array definitions
     *     to pass to the LazyEventListener constructor.
     * @throws Exception\InvalidArgumentException For invalid listener items.
     */
    public function __construct(
        array $listeners,
        /**
         * @var ContainerInterface Container from which to pull lazy listeners
         */
        private ContainerInterface $container,
        /**
         * @var array Additional environment/option variables to use when creating listener
         */
        private array $env = []
    ) {
        // This would raise an exception for invalid structs
        foreach ($listeners as $listener) {
            if (is_array($listener)) {
                $listener = new LazyEventListener($listener, $container, $env);
            }

            if (! $listener instanceof LazyEventListener) {
                throw new Exception\InvalidArgumentException(sprintf(
                    'All listeners must be LazyEventListener instances or definitions; received %s',
                    get_debug_type($listener),
                ));
            }

            $this->lazyListeners[] = $listener;
        }
    }

    /**
     * Attach the aggregate to the event manager.
     *
     * Loops through all composed lazy listeners, and attaches them to the
     * event manager.
     *
     * @param int $priority
     * @return void
     */
    public function attach(EventManagerInterface $events, $priority = 1)
    {
        foreach ($this->lazyListeners as $lazyListener) {
            $this->listeners[] = $events->attach(
                $lazyListener->getEvent(),
                $lazyListener,
                $lazyListener->getPriority($priority)
            );
        }
    }
}