TeamSpeak 3 PHP Framework
1.1.12
|
00001 <?php 00002 00003 /** 00004 * @file 00005 * TeamSpeak 3 PHP Framework 00006 * 00007 * $Id: Signal.php 2/18/2012 12:42:46 scp@orilla $ 00008 * 00009 * This program is free software: you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation, either version 3 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 * @package TeamSpeak3 00023 * @version 1.1.12 00024 * @author Sven 'ScP' Paulsen 00025 * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved. 00026 */ 00027 00028 /** 00029 * @class TeamSpeak3_Helper_Signal 00030 * @brief Helper class for signal slots. 00031 */ 00032 class TeamSpeak3_Helper_Signal 00033 { 00034 /** 00035 * Stores the TeamSpeak3_Helper_Signal object. 00036 * 00037 * @var TeamSpeak3_Helper_Signal 00038 */ 00039 protected static $instance = null; 00040 00041 /** 00042 * Stores subscribed signals and their slots. 00043 * 00044 * @var array 00045 */ 00046 protected $sigslots = array(); 00047 00048 /** 00049 * Emits a signal with a given set of parameters. 00050 * 00051 * @param string $signal 00052 * @param mixed $params 00053 * @return mixed 00054 */ 00055 public function emit($signal, $params = null) 00056 { 00057 if(!$this->hasHandlers($signal)) 00058 { 00059 return; 00060 } 00061 00062 if(!is_array($params)) 00063 { 00064 $params = func_get_args(); 00065 $params = array_slice($params, 1); 00066 } 00067 00068 foreach($this->sigslots[$signal] as $slot) 00069 { 00070 $return = $slot->call($params); 00071 } 00072 00073 return $return; 00074 } 00075 00076 /** 00077 * Subscribes to a signal and returns the signal handler. 00078 * 00079 * @param string $signal 00080 * @param mixed $callback 00081 * @return TeamSpeak3_Helper_Signal_Handler 00082 */ 00083 public function subscribe($signal, $callback) 00084 { 00085 if(empty($this->sigslots[$signal])) 00086 { 00087 $this->sigslots[$signal] = array(); 00088 } 00089 00090 $index = md5(serialize($callback)); 00091 00092 if(!array_key_exists($index, $this->sigslots[$signal])) 00093 { 00094 $this->sigslots[$signal][$index] = new TeamSpeak3_Helper_Signal_Handler($signal, $callback); 00095 } 00096 00097 return $this->sigslots[$signal][$index]; 00098 } 00099 00100 /** 00101 * Unsubscribes from a signal. 00102 * 00103 * @param string $signal 00104 * @param mixed $callback 00105 * @return void 00106 */ 00107 public function unsubscribe($signal, $callback = null) 00108 { 00109 if(!$this->hasHandlers($signal)) 00110 { 00111 return; 00112 } 00113 00114 if($callback !== null) 00115 { 00116 $index = md5(serialize($callback)); 00117 00118 if(!array_key_exists($index, $this->sigslots[$signal])) 00119 { 00120 return; 00121 } 00122 00123 unset($this->sigslots[$signal][$index]); 00124 } 00125 else 00126 { 00127 unset($this->sigslots[$signal]); 00128 } 00129 } 00130 00131 /** 00132 * Returns all registered signals. 00133 * 00134 * @return array 00135 */ 00136 public function getSignals() 00137 { 00138 return array_keys($this->sigslots); 00139 } 00140 00141 /** 00142 * Returns TRUE there are slots subscribed for a specified signal. 00143 * 00144 * @param string $signal 00145 * @return boolean 00146 */ 00147 public function hasHandlers($signal) 00148 { 00149 return empty($this->sigslots[$signal]) ? FALSE : TRUE; 00150 } 00151 00152 /** 00153 * Returns all slots for a specified signal. 00154 * 00155 * @param string $signal 00156 * @return array 00157 */ 00158 public function getHandlers($signal) 00159 { 00160 if(!$this->hasHandlers($signal)) 00161 { 00162 return $this->sigslots[$signal]; 00163 } 00164 00165 return array(); 00166 } 00167 00168 /** 00169 * Clears all slots for a specified signal. 00170 * 00171 * @param string $signal 00172 * @return void 00173 */ 00174 public function clearHandlers($signal) 00175 { 00176 if(!$this->hasHandlers($signal)) 00177 { 00178 unset($this->sigslots[$signal]); 00179 } 00180 } 00181 00182 /** 00183 * Returns a singleton instance of TeamSpeak3_Helper_Signal. 00184 * 00185 * @return TeamSpeak3_Helper_Signal 00186 */ 00187 public static function getInstance() 00188 { 00189 if(self::$instance === null) 00190 { 00191 self::$instance = new self(); 00192 } 00193 00194 return self::$instance; 00195 } 00196 }