TeamSpeak 3 PHP Framework  1.1.16
Copyright © Planet TeamSpeak. All rights reserved.
 All Classes Namespaces Files Functions Variables Pages
Signal.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * @file
5  * TeamSpeak 3 PHP Framework
6  *
7  * $Id: Signal.php 8/31/2012 11:06:09 scp@orilla $
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @package TeamSpeak3
23  * @version 1.1.16
24  * @author Sven 'ScP' Paulsen
25  * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
26  */
27 
28 /**
29  * @class TeamSpeak3_Helper_Signal
30  * @brief Helper class for signal slots.
31  */
33 {
34  /**
35  * Stores the TeamSpeak3_Helper_Signal object.
36  *
37  * @var TeamSpeak3_Helper_Signal
38  */
39  protected static $instance = null;
40 
41  /**
42  * Stores subscribed signals and their slots.
43  *
44  * @var array
45  */
46  protected $sigslots = array();
47 
48  /**
49  * Emits a signal with a given set of parameters.
50  *
51  * @param string $signal
52  * @param mixed $params
53  * @return mixed
54  */
55  public function emit($signal, $params = null)
56  {
57  if(!$this->hasHandlers($signal))
58  {
59  return;
60  }
61 
62  if(!is_array($params))
63  {
64  $params = func_get_args();
65  $params = array_slice($params, 1);
66  }
67 
68  foreach($this->sigslots[$signal] as $slot)
69  {
70  $return = $slot->call($params);
71  }
72 
73  return $return;
74  }
75 
76  /**
77  * Subscribes to a signal and returns the signal handler.
78  *
79  * @param string $signal
80  * @param mixed $callback
81  * @return TeamSpeak3_Helper_Signal_Handler
82  */
83  public function subscribe($signal, $callback)
84  {
85  if(empty($this->sigslots[$signal]))
86  {
87  $this->sigslots[$signal] = array();
88  }
89 
90  $index = md5(serialize($callback));
91 
92  if(!array_key_exists($index, $this->sigslots[$signal]))
93  {
94  $this->sigslots[$signal][$index] = new TeamSpeak3_Helper_Signal_Handler($signal, $callback);
95  }
96 
97  return $this->sigslots[$signal][$index];
98  }
99 
100  /**
101  * Unsubscribes from a signal.
102  *
103  * @param string $signal
104  * @param mixed $callback
105  * @return void
106  */
107  public function unsubscribe($signal, $callback = null)
108  {
109  if(!$this->hasHandlers($signal))
110  {
111  return;
112  }
113 
114  if($callback !== null)
115  {
116  $index = md5(serialize($callback));
117 
118  if(!array_key_exists($index, $this->sigslots[$signal]))
119  {
120  return;
121  }
122 
123  unset($this->sigslots[$signal][$index]);
124  }
125  else
126  {
127  unset($this->sigslots[$signal]);
128  }
129  }
130 
131  /**
132  * Returns all registered signals.
133  *
134  * @return array
135  */
136  public function getSignals()
137  {
138  return array_keys($this->sigslots);
139  }
140 
141  /**
142  * Returns TRUE there are slots subscribed for a specified signal.
143  *
144  * @param string $signal
145  * @return boolean
146  */
147  public function hasHandlers($signal)
148  {
149  return empty($this->sigslots[$signal]) ? FALSE : TRUE;
150  }
151 
152  /**
153  * Returns all slots for a specified signal.
154  *
155  * @param string $signal
156  * @return array
157  */
158  public function getHandlers($signal)
159  {
160  if(!$this->hasHandlers($signal))
161  {
162  return $this->sigslots[$signal];
163  }
164 
165  return array();
166  }
167 
168  /**
169  * Clears all slots for a specified signal.
170  *
171  * @param string $signal
172  * @return void
173  */
174  public function clearHandlers($signal)
175  {
176  if(!$this->hasHandlers($signal))
177  {
178  unset($this->sigslots[$signal]);
179  }
180  }
181 
182  /**
183  * Returns a singleton instance of TeamSpeak3_Helper_Signal.
184  *
185  * @return TeamSpeak3_Helper_Signal
186  */
187  public static function getInstance()
188  {
189  if(self::$instance === null)
190  {
191  self::$instance = new self();
192  }
193 
194  return self::$instance;
195  }
196 }