Signal.php 4.4 KB

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