This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
sfEventDispatcher.php
159 lines (141 loc) · 3.59 KB
/
sfEventDispatcher.php
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
150
151
152
153
154
155
156
157
158
159
<?php
require_once dirname(__FILE__).'/sfEvent.php';
/*
* This file is part of the symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfEventDispatcher implements a dispatcher object.
*
* @see http://developer.apple.com/documentation/Cocoa/Conceptual/Notifications/index.html Apple's Cocoa framework
*
* @package symfony
* @subpackage event_dispatcher
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @version SVN: $Id: sfEventDispatcher.class.php 10631 2008-08-03 16:50:47Z fabien $
*/
class sfEventDispatcher
{
protected
$listeners = array();
/**
* Connects a listener to a given event name.
*
* @param string $name An event name
* @param mixed $listener A PHP callable
*/
public function connect($name, $listener)
{
if (!isset($this->listeners[$name]))
{
$this->listeners[$name] = array();
}
$this->listeners[$name][] = $listener;
}
/**
* Disconnects a listener for a given event name.
*
* @param string $name An event name
* @param mixed $listener A PHP callable
*
* @return mixed false if listener does not exist, null otherwise
*/
public function disconnect($name, $listener)
{
if (!isset($this->listeners[$name]))
{
return false;
}
foreach ($this->listeners[$name] as $i => $callable)
{
if ($listener === $callable)
{
unset($this->listeners[$name][$i]);
}
}
}
/**
* Notifies all listeners of a given event.
*
* @param sfEvent $event A sfEvent instance
*
* @return sfEvent The sfEvent instance
*/
public function notify(sfEvent $event)
{
foreach ($this->getListeners($event->getName()) as $listener)
{
call_user_func($listener, $event);
}
return $event;
}
/**
* Notifies all listeners of a given event until one returns a non null value.
*
* @param sfEvent $event A sfEvent instance
*
* @return sfEvent The sfEvent instance
*/
public function notifyUntil(sfEvent $event)
{
foreach ($this->getListeners($event->getName()) as $listener)
{
if (call_user_func($listener, $event))
{
$event->setProcessed(true);
break;
}
}
return $event;
}
/**
* Filters a value by calling all listeners of a given event.
*
* @param sfEvent $event A sfEvent instance
* @param mixed $value The value to be filtered
*
* @return sfEvent The sfEvent instance
*/
public function filter(sfEvent $event, $value)
{
foreach ($this->getListeners($event->getName()) as $listener)
{
$value = call_user_func_array($listener, array($event, $value));
}
$event->setReturnValue($value);
return $event;
}
/**
* Returns true if the given event name has some listeners.
*
* @param string $name The event name
*
* @return Boolean true if some listeners are connected, false otherwise
*/
public function hasListeners($name)
{
if (!isset($this->listeners[$name]))
{
$this->listeners[$name] = array();
}
return (boolean) count($this->listeners[$name]);
}
/**
* Returns all listeners associated with a given event name.
*
* @param string $name The event name
*
* @return array An array of listeners
*/
public function getListeners($name)
{
if (!isset($this->listeners[$name]))
{
return array();
}
return $this->listeners[$name];
}
}