DebugError.php 4.23 KB
<?php
/**
 * Created by PhpStorm.
 * User: Zip
 * Date: 14/12/7
 * Time: 下午9:52
 */

namespace Hood\Debug;


class DebugError
{
    /**
     * 错误code
     * @var integer
     */
    protected $errorLevel;

    public function exceptionHandler($exception)
    {
        $errorList = array(
            'error_message' => $exception->getMessage()
        );
        foreach ($exception->getTrace() as $key => $val) {
            $errorList['error_list'][] = array(
                'file' => empty($val['file']) ? '↑' : $val['file'],
                'function' => $val['function'],
                'line' => empty($val['line']) ? '' : $val['line'],
                'class' => empty($val['class']) ? '' : $val['class'],
                'type' => empty($val['type']) ? '' : $val['type'],
                'args' => empty($val['args']) ? array() : $val['args']
            );
        }
        if (defined('APPLICATION_DEBUG_FORMAT') == false || APPLICATION_DEBUG_FORMAT == 'html') {
            $this->debugToHtml($errorList, 'exception');
        } else {
            $this->debugToString($errorList, 'exception');
        }
    }


    public function errorHandler($errno, $errstr, $errfile, $errline)
    {
        $errorList = array(
            'error_message' => $errstr
        );
        foreach (debug_backtrace() as $key => $val) {
            $errorList['error_list'][] = array(
                'file' => empty($val['file']) ? __FILE__ : $val['file'],
                'function' => $val['function'],
                'line' => empty($val['line']) ? __LINE__ : $val['line'],
                'class' => empty($val['class']) ? __CLASS__ : $val['class'],
                'type' => empty($val['type']) ? '' : $val['type'],
                'args' => empty($val['args']) ? array() : $val['args']
            );
        }
        if (defined('APPLICATION_DEBUG_FORMAT') == false || APPLICATION_DEBUG_FORMAT == 'html') {
            $this->debugToHtml($errorList);
        } else {
            $this->debugToString($errorList);
        }
    }

    private function debugToString($errorList, $type = 'error')
    {

    }

    private function debugToHtml($errorList, $type = 'error')
    {
        $html = <<<EOD
<style type="text/css">
.warning{
    background:#eeeeec;
    border-color:#eeeeec;
}
strong {
  font-weight:bolder;
  font-style:normal;
  margin-right: 0.5rem;
  font-size: 1.125rem;
}
th ,td{
    padding:4px 10px;
}
</style>
EOD;
        $html .= '<div class="warning"><table style="width: 100%;"><tbody>';
        $html .= '<tr><th colspan="2" align="left" bgcolor="#f57900" style="height: 40px;padding-left: 10px;"><span style="background-color: #cc0000; color: #fce94f; font-size: x-large;">( ! )</span> ' . $errorList['error_message'] . '</th></tr>';
        $html .= '<tr><th align="left" bgcolor="#e9b96e" colspan="2">Call Stack</th></tr>';
        $html .= '<tr><td>Function</td><td>Location</td></td></tr>';
        if (count($errorList['error_list']) > 1) {
            unset($errorList['error_list'][0]);
        }
        foreach ($errorList['error_list'] as $key => $val) {
            $args = '(..)';
            if ($type == 'exception') {
                $args = '(' . implode(',', $val['args']) . ')';
            }
            $function = $val['class'] . $val['type'] . $val['function'] . $args;
            if (empty($val['type'])) {
                $function = '<a href="http://php.net/manual/en/function.' . str_replace('_', '-', $val['function']) . '.php" target="_blank">' . $val['function'] . $args . '</a>';
            }
            $line = '';
            if (!empty($val['line'])) {
                $line .= ' ( ' . $val['line'] . ' ) ';
            }
            $html .= '<tr><td style="color: #0000BB">' . $function . '</td><td>' . $val['file'] . $line . '</td></tr>';
        }
        $html .= '</tbody></table></div><br />';
        echo $html;
    }

    public function setUserDebug()
    {
        $this->errorLevel = ini_get('error_reporting');
        error_reporting(0);
        set_exception_handler(array($this, 'exceptionHandler'));
        set_error_handler(array($this, 'errorHandler'));
    }

    public function restoreUserDebug()
    {
        error_reporting($this->errorLevel);
        restore_error_handler();
        restore_exception_handler();
    }
}