File: LazyEventListener.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 (63 lines) | stat: -rw-r--r-- 1,674 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
<?php

namespace Laminas\EventManager;

use Psr\Container\ContainerInterface;

use function is_string;

/**
 * Lazy listener instance for use with LazyListenerAggregate.
 *
 * Used as an internal class for the LazyAggregate to allow lazy creation of
 * listeners via a dependency injection container.
 *
 * Lazy event listener definitions add the following members to what the
 * LazyListener accepts:
 *
 * - event: the event name to attach to.
 * - priority: the priority at which to attach the listener, if not the default.
 */
class LazyEventListener extends LazyListener
{
    /** @var string Event name to which to attach. */
    private $event;

    /** @var null|int Priority at which to attach. */
    private $priority;

    public function __construct(array $definition, ContainerInterface $container, array $env = [])
    {
        parent::__construct($definition, $container, $env);

        if (
            ! isset($definition['event'])
            || ! is_string($definition['event'])
            || empty($definition['event'])
        ) {
            throw new Exception\InvalidArgumentException(
                'Lazy listener definition is missing a valid "event" member; cannot create LazyListener'
            );
        }

        $this->event    = $definition['event'];
        $this->priority = isset($definition['priority']) ? (int) $definition['priority'] : null;
    }

    /**
     * @return string
     */
    public function getEvent()
    {
        return $this->event;
    }

    /**
     * @param int $default
     * @return int
     */
    public function getPriority($default = 1)
    {
        return $this->priority ?? $default;
    }
}