TeamSpeak 3 PHP Framework  1.1.23
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 10/11/2013 11:35:21 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.23
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  * Generates a MD5 hash based on a given callback.
78  *
79  * @param mixed $callback
80  * @param string
81  * @return void
82  */
83  public function getCallbackHash($callback)
84  {
85  if(!is_callable($callback, TRUE, $callable_name))
86  {
87  throw new TeamSpeak3_Helper_Signal_Exception("invalid callback specified");
88  }
89 
90  return md5($callable_name);
91  }
92 
93  /**
94  * Subscribes to a signal and returns the signal handler.
95  *
96  * @param string $signal
97  * @param mixed $callback
98  * @return TeamSpeak3_Helper_Signal_Handler
99  */
100  public function subscribe($signal, $callback)
101  {
102  if(empty($this->sigslots[$signal]))
103  {
104  $this->sigslots[$signal] = array();
105  }
106 
107  $index = $this->getCallbackHash($callback);
108 
109  if(!array_key_exists($index, $this->sigslots[$signal]))
110  {
111  $this->sigslots[$signal][$index] = new TeamSpeak3_Helper_Signal_Handler($signal, $callback);
112  }
113 
114  return $this->sigslots[$signal][$index];
115  }
116 
117  /**
118  * Unsubscribes from a signal.
119  *
120  * @param string $signal
121  * @param mixed $callback
122  * @return void
123  */
124  public function unsubscribe($signal, $callback = null)
125  {
126  if(!$this->hasHandlers($signal))
127  {
128  return;
129  }
130 
131  if($callback !== null)
132  {
133  $index = $this->getCallbackHash($callback);
134 
135  if(!array_key_exists($index, $this->sigslots[$signal]))
136  {
137  return;
138  }
139 
140  unset($this->sigslots[$signal][$index]);
141  }
142  else
143  {
144  unset($this->sigslots[$signal]);
145  }
146  }
147 
148  /**
149  * Returns all registered signals.
150  *
151  * @return array
152  */
153  public function getSignals()
154  {
155  return array_keys($this->sigslots);
156  }
157 
158  /**
159  * Returns TRUE there are slots subscribed for a specified signal.
160  *
161  * @param string $signal
162  * @return boolean
163  */
164  public function hasHandlers($signal)
165  {
166  return empty($this->sigslots[$signal]) ? FALSE : TRUE;
167  }
168 
169  /**
170  * Returns all slots for a specified signal.
171  *
172  * @param string $signal
173  * @return array
174  */
175  public function getHandlers($signal)
176  {
177  if(!$this->hasHandlers($signal))
178  {
179  return $this->sigslots[$signal];
180  }
181 
182  return array();
183  }
184 
185  /**
186  * Clears all slots for a specified signal.
187  *
188  * @param string $signal
189  * @return void
190  */
191  public function clearHandlers($signal)
192  {
193  if(!$this->hasHandlers($signal))
194  {
195  unset($this->sigslots[$signal]);
196  }
197  }
198 
199  /**
200  * Returns a singleton instance of TeamSpeak3_Helper_Signal.
201  *
202  * @return TeamSpeak3_Helper_Signal
203  */
204  public static function getInstance()
205  {
206  if(self::$instance === null)
207  {
208  self::$instance = new self();
209  }
210 
211  return self::$instance;
212  }
213 }