Abstract.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @file
  4. * TeamSpeak 3 PHP Framework
  5. *
  6. * $Id: Abstract.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_Adapter_Abstract
  28. * @brief Provides low-level methods for concrete adapters to communicate with a TeamSpeak 3 Server.
  29. */
  30. abstract class TeamSpeak3_Adapter_Abstract
  31. {
  32. /**
  33. * Stores user-provided options.
  34. *
  35. * @var array
  36. */
  37. protected $options = null;
  38. /**
  39. * Stores an TeamSpeak3_Transport_Abstract object.
  40. *
  41. * @var TeamSpeak3_Transport_Abstract
  42. */
  43. protected $transport = null;
  44. /**
  45. * The TeamSpeak3_Adapter_Abstract constructor.
  46. *
  47. * @param array $options
  48. * @return TeamSpeak3_Adapter_Abstract
  49. */
  50. public function __construct(array $options)
  51. {
  52. $this->options = $options;
  53. if($this->transport === null)
  54. {
  55. $this->syn();
  56. }
  57. }
  58. /**
  59. * The TeamSpeak3_Adapter_Abstract destructor.
  60. *
  61. * @return void
  62. */
  63. abstract public function __destruct();
  64. /**
  65. * Connects the TeamSpeak3_Transport_Abstract object and performs initial actions on the remote
  66. * server.
  67. *
  68. * @throws TeamSpeak3_Adapter_Exception
  69. * @return void
  70. */
  71. abstract protected function syn();
  72. /**
  73. * Commit pending data.
  74. *
  75. * @return array
  76. */
  77. public function __sleep()
  78. {
  79. return array("options");
  80. }
  81. /**
  82. * Reconnects to the remote server.
  83. *
  84. * @return void
  85. */
  86. public function __wakeup()
  87. {
  88. $this->syn();
  89. }
  90. /**
  91. * Returns the profiler timer used for this connection adapter.
  92. *
  93. * @return TeamSpeak3_Helper_Profiler_Timer
  94. */
  95. public function getProfiler()
  96. {
  97. return TeamSpeak3_Helper_Profiler::get(spl_object_hash($this));
  98. }
  99. /**
  100. * Returns the transport object used for this connection adapter.
  101. *
  102. * @return TeamSpeak3_Transport_Abstract
  103. */
  104. public function getTransport()
  105. {
  106. return $this->transport;
  107. }
  108. /**
  109. * Loads the transport object object used for the connection adapter and passes a given set
  110. * of options.
  111. *
  112. * @param array $options
  113. * @param string $transport
  114. * @throws TeamSpeak3_Adapter_Exception
  115. * @return void
  116. */
  117. protected function initTransport($options, $transport = "TeamSpeak3_Transport_TCP")
  118. {
  119. if(!is_array($options))
  120. {
  121. throw new TeamSpeak3_Adapter_Exception("transport parameters must provided in an array");
  122. }
  123. $this->transport = new $transport($options);
  124. }
  125. /**
  126. * Returns the hostname or IPv4 address the underlying TeamSpeak3_Transport_Abstract object
  127. * is connected to.
  128. *
  129. * @return string
  130. */
  131. public function getTransportHost()
  132. {
  133. return $this->getTransport()->getConfig("host", "0.0.0.0");
  134. }
  135. /**
  136. * Returns the port number of the server the underlying TeamSpeak3_Transport_Abstract object
  137. * is connected to.
  138. *
  139. * @return string
  140. */
  141. public function getTransportPort()
  142. {
  143. return $this->getTransport()->getConfig("port", "0");
  144. }
  145. }