~ubuntu-branches/debian/experimental/php-sabre-event/experimental

« back to all changes in this revision

Viewing changes to lib/Sabre/Event/EventEmitterTrait.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2013-11-12 15:54:32 UTC
  • Revision ID: package-import@ubuntu.com-20131112155432-qe7mz2pjzi8kxkyx
Tags: upstream-1.0.0
Import upstream version 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
namespace Sabre\Event;
 
4
 
 
5
/**
 
6
 * Event Emitter Trait
 
7
 *
 
8
 * This traits contains all the basic functions to implement an
 
9
 * EventEmitterInterface.
 
10
 *
 
11
 * Using the trait + interface allows you to add EventEmitter capabilities
 
12
 * without having to change your base-class.
 
13
 *
 
14
 * @copyright Copyright (C) 2013 fruux GmbH. All rights reserved.
 
15
 * @author Evert Pot (http://evertpot.com/)
 
16
 * @license https://raw.github.com/fruux/sabre-event/master/LICENSE
 
17
 */
 
18
trait EventEmitterTrait {
 
19
 
 
20
    /**
 
21
     * The list of listeners
 
22
     *
 
23
     * @var array
 
24
     */
 
25
    protected $listeners = [];
 
26
 
 
27
    /**
 
28
     * Subscribe to an event.
 
29
     *
 
30
     * @param string $eventName
 
31
     * @param callable $callBack
 
32
     * @param int $priority
 
33
     * @return void
 
34
     */
 
35
    public function on($eventName, callable $callBack, $priority = 100) {
 
36
 
 
37
        $listeners =& $this->listeners($eventName);
 
38
        $listeners[] = [$priority, $callBack];
 
39
        usort($listeners, function($a, $b) {
 
40
 
 
41
            return $a[0]-$b[0];
 
42
 
 
43
        });
 
44
 
 
45
    }
 
46
 
 
47
    /**
 
48
     * Subscribe to an event exactly once.
 
49
     *
 
50
     * @param string $eventName
 
51
     * @param callable $callBack
 
52
     * @param int $priority
 
53
     * @return void
 
54
     */
 
55
    public function once($eventName, callable $callBack, $priority = 100) {
 
56
 
 
57
        $wrapper = null;
 
58
        $wrapper = function() use ($eventName, $callBack, &$wrapper) {
 
59
 
 
60
            $this->removeListener($eventName, $wrapper);
 
61
            $result = call_user_func_array($callBack, func_get_args());
 
62
 
 
63
        };
 
64
 
 
65
        $this->on($eventName, $wrapper);
 
66
 
 
67
    }
 
68
 
 
69
    /**
 
70
     * Emits an event.
 
71
     *
 
72
     * This method will return true if 0 or more listeners were succesfully
 
73
     * handled. false is returned if one of the events broke the event chain.
 
74
     *
 
75
     * @param string $eventName
 
76
     * @param array $arguments
 
77
     * @return bool
 
78
     */
 
79
    public function emit($eventName, array $arguments = []) {
 
80
 
 
81
        foreach($this->listeners($eventName) as $listener) {
 
82
 
 
83
            $result = call_user_func_array($listener[1], $arguments);
 
84
            if ($result === false) {
 
85
                return false;
 
86
            }
 
87
        }
 
88
 
 
89
        return true;
 
90
 
 
91
    }
 
92
 
 
93
 
 
94
    /**
 
95
     * Returns the list of listeners for an event.
 
96
     *
 
97
     * The list is returned as an array. Every item is another array with 2
 
98
     * elements: priority and the callback.
 
99
     *
 
100
     * The array is returned by reference, and can therefore be used to
 
101
     * manipulate the list of events.
 
102
     *
 
103
     * @param string $eventName
 
104
     * @return array
 
105
     */
 
106
    public function &listeners($eventName) {
 
107
 
 
108
        if (!isset($this->listeners[$eventName])) {
 
109
            $this->listeners[$eventName] = [];
 
110
        }
 
111
 
 
112
        return $this->listeners[$eventName];
 
113
 
 
114
    }
 
115
 
 
116
    /**
 
117
     * Removes a specific listener from an event.
 
118
     *
 
119
     * @param string $eventName
 
120
     * @param callable $listener
 
121
     * @return void
 
122
     */
 
123
    public function removeListener($eventName, callable $listener) {
 
124
 
 
125
        $listeners =& $this->listeners($eventName);
 
126
        foreach($listeners as $index => $check) {
 
127
            if ($check[1]===$listener) {
 
128
                unset($listeners[$index]);
 
129
                break;
 
130
            }
 
131
        }
 
132
 
 
133
    }
 
134
 
 
135
    /**
 
136
     * Removes all listeners from the specified event.
 
137
     *
 
138
     * @param string $eventName
 
139
     * @return void
 
140
     */
 
141
    public function removeAllListeners($eventName) {
 
142
 
 
143
        $listeners =& $this->listeners($eventName);
 
144
        $listeners = [];
 
145
 
 
146
    }
 
147
 
 
148
}