| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
class sfEventDispatcher |
|---|
| 22 |
{ |
|---|
| 23 |
protected |
|---|
| 24 |
$listeners = array(); |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
* Connects a listener to a given event name. |
|---|
| 28 |
* |
|---|
| 29 |
* @param string $name An event name |
|---|
| 30 |
* @param mixed $listener A PHP callable |
|---|
| 31 |
*/ |
|---|
| 32 |
public function connect($name, $listener) |
|---|
| 33 |
{ |
|---|
| 34 |
if (!isset($this->listeners[$name])) |
|---|
| 35 |
{ |
|---|
| 36 |
$this->listeners[$name] = array(); |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
$this->listeners[$name][] = $listener; |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
* Disconnects a listener for a given event name. |
|---|
| 44 |
* |
|---|
| 45 |
* @param string $name An event name |
|---|
| 46 |
* @param mixed $listener A PHP callable |
|---|
| 47 |
* |
|---|
| 48 |
* @return mixed false if listener does not exist, null otherwise |
|---|
| 49 |
*/ |
|---|
| 50 |
public function disconnect($name, $listener) |
|---|
| 51 |
{ |
|---|
| 52 |
if (!isset($this->listeners[$name])) |
|---|
| 53 |
{ |
|---|
| 54 |
return false; |
|---|
| 55 |
} |
|---|
| 56 |
|
|---|
| 57 |
foreach ($this->listeners[$name] as $i => $callable) |
|---|
| 58 |
{ |
|---|
| 59 |
if ($listener === $callable) |
|---|
| 60 |
{ |
|---|
| 61 |
unset($this->listeners[$name][$i]); |
|---|
| 62 |
} |
|---|
| 63 |
} |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
* Notifies all listeners of a given event. |
|---|
| 68 |
* |
|---|
| 69 |
* @param sfEvent $event A sfEvent instance |
|---|
| 70 |
* |
|---|
| 71 |
* @return sfEvent The sfEvent instance |
|---|
| 72 |
*/ |
|---|
| 73 |
public function notify(sfEvent $event) |
|---|
| 74 |
{ |
|---|
| 75 |
foreach ($this->getListeners($event->getName()) as $listener) |
|---|
| 76 |
{ |
|---|
| 77 |
call_user_func($listener, $event); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
return $event; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
* Notifies all listeners of a given event until one returns a non null value. |
|---|
| 85 |
* |
|---|
| 86 |
* @param sfEvent $event A sfEvent instance |
|---|
| 87 |
* |
|---|
| 88 |
* @return sfEvent The sfEvent instance |
|---|
| 89 |
*/ |
|---|
| 90 |
public function notifyUntil(sfEvent $event) |
|---|
| 91 |
{ |
|---|
| 92 |
foreach ($this->getListeners($event->getName()) as $listener) |
|---|
| 93 |
{ |
|---|
| 94 |
if (call_user_func($listener, $event)) |
|---|
| 95 |
{ |
|---|
| 96 |
$event->setProcessed(true); |
|---|
| 97 |
break; |
|---|
| 98 |
} |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
return $event; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
* Filters a value by calling all listeners of a given event. |
|---|
| 106 |
* |
|---|
| 107 |
* @param sfEvent $event A sfEvent instance |
|---|
| 108 |
* @param mixed $value The value to be filtered |
|---|
| 109 |
* |
|---|
| 110 |
* @return sfEvent The sfEvent instance |
|---|
| 111 |
*/ |
|---|
| 112 |
public function filter(sfEvent $event, $value) |
|---|
| 113 |
{ |
|---|
| 114 |
foreach ($this->getListeners($event->getName()) as $listener) |
|---|
| 115 |
{ |
|---|
| 116 |
$value = call_user_func_array($listener, array($event, $value)); |
|---|
| 117 |
} |
|---|
| 118 |
|
|---|
| 119 |
$event->setReturnValue($value); |
|---|
| 120 |
|
|---|
| 121 |
return $event; |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
* Returns true if the given event name has some listeners. |
|---|
| 126 |
* |
|---|
| 127 |
* @param string $name The event name |
|---|
| 128 |
* |
|---|
| 129 |
* @return Boolean true if some listeners are connected, false otherwise |
|---|
| 130 |
*/ |
|---|
| 131 |
public function hasListeners($name) |
|---|
| 132 |
{ |
|---|
| 133 |
if (!isset($this->listeners[$name])) |
|---|
| 134 |
{ |
|---|
| 135 |
$this->listeners[$name] = array(); |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
return (boolean) count($this->listeners[$name]); |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
* Returns all listeners associated with a given event name. |
|---|
| 143 |
* |
|---|
| 144 |
* @param string $name The event name |
|---|
| 145 |
* |
|---|
| 146 |
* @return array An array of listeners |
|---|
| 147 |
*/ |
|---|
| 148 |
public function getListeners($name) |
|---|
| 149 |
{ |
|---|
| 150 |
if (!isset($this->listeners[$name])) |
|---|
| 151 |
{ |
|---|
| 152 |
return array(); |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
return $this->listeners[$name]; |
|---|
| 156 |
} |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|