Idle.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Exception;
  36. /**
  37. * Class \Hoa\Exception\Idle.
  38. *
  39. * `\Hoa\Exception\Idle` is the mother exception class of libraries. The only
  40. * difference between `\Hoa\Exception\Idle` and its directly child
  41. * `\Hoa\Exception` is that the latter fires events after beeing constructed.
  42. *
  43. * @copyright Copyright © 2007-2017 Hoa community
  44. * @license New BSD License
  45. */
  46. class Idle extends \Exception
  47. {
  48. /**
  49. * Delay processing on arguments.
  50. *
  51. * @var array
  52. */
  53. protected $_tmpArguments = null;
  54. /**
  55. * Arguments to format message.
  56. *
  57. * @var array
  58. */
  59. protected $_arguments = null;
  60. /**
  61. * Backtrace.
  62. *
  63. * @var array
  64. */
  65. protected $_trace = null;
  66. /**
  67. * Previous.
  68. *
  69. * @var \Exception
  70. */
  71. protected $_previous = null;
  72. /**
  73. * Original message.
  74. *
  75. * @var string
  76. */
  77. protected $_rawMessage = null;
  78. /**
  79. * Create an exception.
  80. * An exception is built with a formatted message, a code (an ID) and an
  81. * array that contains the list of formatted strings for the message. If
  82. * chaining, we can add a previous exception.
  83. *
  84. * @param string $message Formatted message.
  85. * @param int $code Code (the ID).
  86. * @param array $arguments Arguments to format message.
  87. * @param \Exception $previous Previous exception in chaining.
  88. */
  89. public function __construct(
  90. $message,
  91. $code = 0,
  92. $arguments = [],
  93. \Exception $previous = null
  94. ) {
  95. $this->_tmpArguments = $arguments;
  96. parent::__construct($message, $code, $previous);
  97. $this->_rawMessage = $message;
  98. $this->message = @vsprintf($message, $this->getArguments());
  99. return;
  100. }
  101. /**
  102. * Get the backtrace.
  103. * Do not use \Exception::getTrace() any more.
  104. *
  105. * @return array
  106. */
  107. public function getBacktrace()
  108. {
  109. if (null === $this->_trace) {
  110. $this->_trace = $this->getTrace();
  111. }
  112. return $this->_trace;
  113. }
  114. /**
  115. * Get previous.
  116. * Do not use \Exception::getPrevious() any more.
  117. *
  118. * @return \Exception
  119. */
  120. public function getPreviousThrow()
  121. {
  122. if (null === $this->_previous) {
  123. $this->_previous = $this->getPrevious();
  124. }
  125. return $this->_previous;
  126. }
  127. /**
  128. * Get arguments for the message.
  129. *
  130. * @return array
  131. */
  132. public function getArguments()
  133. {
  134. if (null === $this->_arguments) {
  135. $arguments = $this->_tmpArguments;
  136. if (!is_array($arguments)) {
  137. $arguments = [$arguments];
  138. }
  139. foreach ($arguments as &$value) {
  140. if (null === $value) {
  141. $value = '(null)';
  142. }
  143. }
  144. $this->_arguments = $arguments;
  145. unset($this->_tmpArguments);
  146. }
  147. return $this->_arguments;
  148. }
  149. /**
  150. * Get the raw message.
  151. *
  152. * @return string
  153. */
  154. public function getRawMessage()
  155. {
  156. return $this->_rawMessage;
  157. }
  158. /**
  159. * Get the message already formatted.
  160. *
  161. * @return string
  162. */
  163. public function getFormattedMessage()
  164. {
  165. return $this->getMessage();
  166. }
  167. /**
  168. * Get the source of the exception (class, method, function, main etc.).
  169. *
  170. * @return string
  171. */
  172. public function getFrom()
  173. {
  174. $trace = $this->getBacktrace();
  175. $from = '{main}';
  176. if (!empty($trace)) {
  177. $t = $trace[0];
  178. $from = '';
  179. if (isset($t['class'])) {
  180. $from .= $t['class'] . '::';
  181. }
  182. if (isset($t['function'])) {
  183. $from .= $t['function'] . '()';
  184. }
  185. }
  186. return $from;
  187. }
  188. /**
  189. * Raise an exception as a string.
  190. *
  191. * @param bool $previous Whether raise previous exception if exists.
  192. * @return string
  193. */
  194. public function raise($previous = false)
  195. {
  196. $message = $this->getFormattedMessage();
  197. $trace = $this->getBacktrace();
  198. $file = '/dev/null';
  199. $line = -1;
  200. $pre = $this->getFrom();
  201. if (!empty($trace)) {
  202. $file = isset($trace['file']) ? $trace['file'] : null;
  203. $line = isset($trace['line']) ? $trace['line'] : null;
  204. }
  205. $pre .= ': ';
  206. try {
  207. $out =
  208. $pre . '(' . $this->getCode() . ') ' . $message . "\n" .
  209. 'in ' . $this->getFile() . ' at line ' .
  210. $this->getLine() . '.';
  211. } catch (\Exception $e) {
  212. $out =
  213. $pre . '(' . $this->getCode() . ') ' . $message . "\n" .
  214. 'in ' . $file . ' around line ' . $line . '.';
  215. }
  216. if (true === $previous &&
  217. null !== $previous = $this->getPreviousThrow()) {
  218. $out .=
  219. "\n\n" . ' ⬇' . "\n\n" .
  220. 'Nested exception (' . get_class($previous) . '):' . "\n" .
  221. ($previous instanceof self
  222. ? $previous->raise(true)
  223. : $previous->getMessage());
  224. }
  225. return $out;
  226. }
  227. /**
  228. * Catch uncaught exception (only \Hoa\Exception\Idle and children).
  229. *
  230. * @param \Throwable $exception The exception.
  231. * @return void
  232. * @throws \Throwable
  233. */
  234. public static function uncaught($exception)
  235. {
  236. if (!($exception instanceof self)) {
  237. throw $exception;
  238. }
  239. while (0 < ob_get_level()) {
  240. ob_end_flush();
  241. }
  242. echo
  243. 'Uncaught exception (' . get_class($exception) . '):' . "\n" .
  244. $exception->raise(true);
  245. return;
  246. }
  247. /**
  248. * String representation of object.
  249. *
  250. * @return string
  251. */
  252. public function __toString()
  253. {
  254. return $this->raise();
  255. }
  256. /**
  257. * Enable uncaught exception handler.
  258. * This is restricted to Hoa's exceptions only.
  259. *
  260. * @param bool $enable Enable.
  261. * @return mixed
  262. */
  263. public static function enableUncaughtHandler($enable = true)
  264. {
  265. if (false === $enable) {
  266. return restore_exception_handler();
  267. }
  268. return set_exception_handler(function ($exception) {
  269. return self::uncaught($exception);
  270. });
  271. }
  272. }