summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/stdlib/src/Events.php
blob: 34050868f4889a82f0d5659b1508116003319c9d (plain)
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
<?php

namespace ipl\Stdlib;

use Evenement\EventEmitterTrait;
use InvalidArgumentException;

trait Events
{
    use EventEmitterTrait {
        EventEmitterTrait::on as private evenementUnvalidatedOn;
    }

    /** @var array */
    protected $eventsEmittedOnce = [];

    /**
     * @param string $event
     * @param array $arguments
     */
    protected function emitOnce($event, array $arguments = [])
    {
        if (! isset($this->eventsEmittedOnce[$event])) {
            $this->eventsEmittedOnce[$event] = true;
            $this->emit($event, $arguments);
        }
    }

    /**
     * @param string $event
     * @param callable $listener
     * @return $this
     */
    public function on($event, callable $listener)
    {
        $this->assertValidEvent($event);
        $this->evenementUnvalidatedOn($event, $listener);

        return $this;
    }

    protected function assertValidEvent($event)
    {
        if (! $this->isValidEvent($event)) {
            throw new InvalidArgumentException("$event is not a valid event");
        }
    }

    /**
     * @param string $event
     * @return bool
     */
    public function isValidEvent($event)
    {
        return true;
    }
}