summaryrefslogtreecommitdiff
path: root/core/event.php
blob: 0941acac42b8600bb99095d7f2f25f5a722316f7 (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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php

namespace Core;

/**
 * Event listener interface
 *
 * @package core
 * @author  Frederic Guillot
 */
interface Listener {

    /**
     * @return boolean
     */
    public function execute(array $data);
}

/**
 * Event dispatcher class
 *
 * @package core
 * @author  Frederic Guillot
 */
class Event
{
    /**
     * Contains all listeners
     *
     * @access private
     * @var array
     */
    private $listeners = array();

    /**
     * The last listener executed
     *
     * @access private
     * @var string
     */
    private $lastListener = '';

    /**
     * The last triggered event
     *
     * @access private
     * @var string
     */
    private $lastEvent = '';

    /**
     * Triggered events list
     *
     * @access private
     * @var array
     */
    private $events = array();

    /**
     * Attach a listener object to an event
     *
     * @access public
     * @param  string   $eventName   Event name
     * @param  Listener $listener    Object that implements the Listener interface
     */
    public function attach($eventName, Listener $listener)
    {
        if (! isset($this->listeners[$eventName])) {
            $this->listeners[$eventName] = array();
        }

        $this->listeners[$eventName][] = $listener;
    }

    /**
     * Trigger an event
     *
     * @access public
     * @param  string   $eventName   Event name
     * @param  array    $data        Event data
     */
    public function trigger($eventName, array $data)
    {
        $this->lastEvent = $eventName;
        $this->events[] = $eventName;

        if (isset($this->listeners[$eventName])) {
            foreach ($this->listeners[$eventName] as $listener) {
                if ($listener->execute($data)) {
                    $this->lastListener = get_class($listener);
                }
            }
        }
    }

    /**
     * Get the last listener executed
     *
     * @access public
     * @return string Event name
     */
    public function getLastListenerExecuted()
    {
        return $this->lastListener;
    }

    /**
     * Get the last fired event
     *
     * @access public
     * @return string Event name
     */
    public function getLastTriggeredEvent()
    {
        return $this->lastEvent;
    }

    /**
     * Get a list of triggered events
     *
     * @access public
     * @return array
     */
    public function getTriggeredEvents()
    {
        return $this->events;
    }

    /**
     * Check if a listener bind to an event
     *
     * @access public
     * @param  string $eventName    Event name
     * @param  mixed  $instance     Instance name or object itself
     * @return bool                 Yes or no
     */
    public function hasListener($eventName, $instance)
    {
        if (isset($this->listeners[$eventName])) {
            foreach ($this->listeners[$eventName] as $listener) {
                if ($listener instanceof $instance) {
                    return true;
                }
            }
        }

        return false;
    }
}