ExceptionCollector.php 1.96 KB
<?php
/**
 * Created by PhpStorm.
 * User: Zip
 * Date: 15/4/26
 * Time: 下午12:26
 */

namespace Hood\Debug\Debugbar;


class ExceptionCollector implements InterfaceCollector
{

    private $collector = array();

    private $debugType = 'Exception';

    public function __construct($exception)
    {
        $errorList = $this->formatExceptionData($exception);
        foreach ($exception->getTrace() as $key => $val) {
            $_error = 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']
            );
            if (!empty($val['args'])) {
                $args = array();
                foreach ($val['args'] as $argsKey => $argsVal) {
                    if (empty($argsVal)) {
                        continue;
                    }
                    $args[] = $argsVal;
                }
                $_error['args'] = $args;
            }
            $errorList['exception_list'][] = $_error;
        }
        $this->collector = $errorList;
    }

    public function formatExceptionData($e)
    {
        $filePath = $e->getFile();
        $lines = array();
        if ($filePath && file_exists($filePath)) {
            $lines = file($filePath);
            $start = $e->getLine() - 4;
            $lines = array_slice($lines, $start < 0 ? 0 : $start, 7, true);
        }
        return array(
            'type' => get_class($e),
            'message' => $e->getMessage(),
            'code' => $e->getCode(),
            'file' => $filePath,
            'line' => $e->getLine(),
            'surrounding_lines' => $lines
        );
    }

    public function getDebugType()
    {
        return $this->debugType;
    }

    public function getCollector()
    {
        return $this->collector;
    }
}