123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- <?php
- namespace Hoa\Exception;
- class Idle extends \Exception
- {
-
- protected $_tmpArguments = null;
-
- protected $_arguments = null;
-
- protected $_trace = null;
-
- protected $_previous = null;
-
- protected $_rawMessage = null;
-
- public function __construct(
- $message,
- $code = 0,
- $arguments = [],
- \Exception $previous = null
- ) {
- $this->_tmpArguments = $arguments;
- parent::__construct($message, $code, $previous);
- $this->_rawMessage = $message;
- $this->message = @vsprintf($message, $this->getArguments());
- return;
- }
-
- public function getBacktrace()
- {
- if (null === $this->_trace) {
- $this->_trace = $this->getTrace();
- }
- return $this->_trace;
- }
-
- public function getPreviousThrow()
- {
- if (null === $this->_previous) {
- $this->_previous = $this->getPrevious();
- }
- return $this->_previous;
- }
-
- public function getArguments()
- {
- if (null === $this->_arguments) {
- $arguments = $this->_tmpArguments;
- if (!is_array($arguments)) {
- $arguments = [$arguments];
- }
- foreach ($arguments as &$value) {
- if (null === $value) {
- $value = '(null)';
- }
- }
- $this->_arguments = $arguments;
- unset($this->_tmpArguments);
- }
- return $this->_arguments;
- }
-
- public function getRawMessage()
- {
- return $this->_rawMessage;
- }
-
- public function getFormattedMessage()
- {
- return $this->getMessage();
- }
-
- public function getFrom()
- {
- $trace = $this->getBacktrace();
- $from = '{main}';
- if (!empty($trace)) {
- $t = $trace[0];
- $from = '';
- if (isset($t['class'])) {
- $from .= $t['class'] . '::';
- }
- if (isset($t['function'])) {
- $from .= $t['function'] . '()';
- }
- }
- return $from;
- }
-
- public function raise($previous = false)
- {
- $message = $this->getFormattedMessage();
- $trace = $this->getBacktrace();
- $file = '/dev/null';
- $line = -1;
- $pre = $this->getFrom();
- if (!empty($trace)) {
- $file = isset($trace['file']) ? $trace['file'] : null;
- $line = isset($trace['line']) ? $trace['line'] : null;
- }
- $pre .= ': ';
- try {
- $out =
- $pre . '(' . $this->getCode() . ') ' . $message . "\n" .
- 'in ' . $this->getFile() . ' at line ' .
- $this->getLine() . '.';
- } catch (\Exception $e) {
- $out =
- $pre . '(' . $this->getCode() . ') ' . $message . "\n" .
- 'in ' . $file . ' around line ' . $line . '.';
- }
- if (true === $previous &&
- null !== $previous = $this->getPreviousThrow()) {
- $out .=
- "\n\n" . ' ⬇' . "\n\n" .
- 'Nested exception (' . get_class($previous) . '):' . "\n" .
- ($previous instanceof self
- ? $previous->raise(true)
- : $previous->getMessage());
- }
- return $out;
- }
-
- public static function uncaught($exception)
- {
- if (!($exception instanceof self)) {
- throw $exception;
- }
- while (0 < ob_get_level()) {
- ob_end_flush();
- }
- echo
- 'Uncaught exception (' . get_class($exception) . '):' . "\n" .
- $exception->raise(true);
- return;
- }
-
- public function __toString()
- {
- return $this->raise();
- }
-
- public static function enableUncaughtHandler($enable = true)
- {
- if (false === $enable) {
- return restore_exception_handler();
- }
- return set_exception_handler(function ($exception) {
- return self::uncaught($exception);
- });
- }
- }
|