Exception.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @file
  4. * TeamSpeak 3 PHP Framework
  5. *
  6. * $Id: Exception.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_Exception
  28. * @brief Enhanced exception class for TeamSpeak3 objects.
  29. */
  30. class TeamSpeak3_Exception extends Exception
  31. {
  32. /**
  33. * Stores custom error messages.
  34. *
  35. * @var array
  36. */
  37. protected static $messages = array();
  38. /**
  39. * The TeamSpeak3_Exception constructor.
  40. *
  41. * @param string $mesg
  42. * @param integer $code
  43. * @return TeamSpeak3_Exception
  44. */
  45. public function __construct($mesg, $code = 0x00)
  46. {
  47. parent::__construct($mesg, $code);
  48. if(array_key_exists((int) $code, self::$messages))
  49. {
  50. $this->message = $this->prepareCustomMessage(self::$messages[intval($code)]);
  51. }
  52. TeamSpeak3_Helper_Signal::getInstance()->emit("errorException", $this);
  53. }
  54. /**
  55. * Prepares a custom error message by replacing pre-defined signs with given values.
  56. *
  57. * @param TeamSpeak3_Helper_String $mesg
  58. * @return TeamSpeak3_Helper_String
  59. */
  60. protected function prepareCustomMessage(TeamSpeak3_Helper_String $mesg)
  61. {
  62. $args = array(
  63. "code" => $this->getCode(),
  64. "mesg" => $this->getMessage(),
  65. "line" => $this->getLine(),
  66. "file" => $this->getFile(),
  67. );
  68. return $mesg->arg($args)->toString();
  69. }
  70. /**
  71. * Registers a custom error message to $code.
  72. *
  73. * @param integer $code
  74. * @param string $mesg
  75. * @throws TeamSpeak3_Exception
  76. * @return void
  77. */
  78. public static function registerCustomMessage($code, $mesg)
  79. {
  80. if(array_key_exists((int) $code, self::$messages))
  81. {
  82. throw new self("custom message for code 0x" . strtoupper(dechex($code)) . " is already registered");
  83. }
  84. if(!is_string($mesg))
  85. {
  86. throw new self("custom message for code 0x" . strtoupper(dechex($code)) . " must be a string");
  87. }
  88. self::$messages[(int) $code] = new TeamSpeak3_Helper_String($mesg);
  89. }
  90. /**
  91. * Unregisters a custom error message from $code.
  92. *
  93. * @param integer $code
  94. * @throws TeamSpeak3_Exception
  95. * @return void
  96. */
  97. public static function unregisterCustomMessage($code)
  98. {
  99. if(!array_key_exists((int) $code, self::$messages))
  100. {
  101. throw new self("custom message for code 0x" . strtoupper(dechex($code)) . " is not registered");
  102. }
  103. unset(self::$messages[(int) $code]);
  104. }
  105. /**
  106. * Returns the class from which the exception was thrown.
  107. *
  108. * @return string
  109. */
  110. public function getSender()
  111. {
  112. $trace = $this->getTrace();
  113. return (isset($trace[0]["class"])) ? $trace[0]["class"] : "{main}";
  114. }
  115. }