<?php

namespace xmvc;

// @codingStandardsIgnoreStart
/**
 * 格式化 trace
 *
 * @param [array] $backtrace
 * @return void
 */
function backtraceFormat(array $backtrace){
	$traceList = [];
	//trace过滤列表
	$traceFilter = [
		['', 'onShutdownDebug'], //空文件来自php自己触发
		['', 'onShutdownProduct'],
		['', 'onShutdownCli'],
		ROOT . '/index.php',  'execute', //[文件名,方法名]
		DIR_MVC . '/MVC.php',
		DIR_MVC . '/Function.php',
		DIR_MVC . '/ErrorClass.php',
		DIR_MVC . '/DB.php',
		DIR_MVC . '/Sqler.php',
		DIR_MVC . '/PdoHelper.php',
	];
	$caller = getTraceCaller($backtrace, $traceFilter);
	if (is_null($caller)) {
		return;
	}
	foreach ($backtrace as $trace) {
		if (!isset($trace['class'])) continue;
		$trace['line'] = (isset($trace['line'])) ?
					 '<a href="'.HOST. '/xmvc/core/cmd-showPhpCode~filename-'
					. Encoder::encode($trace['file']).'~line-'. $trace['line'].'#goto" target="_blank">'
					. $trace['line'] . '</a>'
					: '';
		if(MVC::$cfg['error']['allBacktrace']){
			$traceList[] = [
				'文件' => relDir($trace['file']) ?: '',
				'行' => $trace['line'] ?: '',
				'类' => $trace['class'] ?: '',
				'&nbsp' => $trace['type'] ?: '',
				'方法' => $trace['function'] ?: '',
				'对象' => $trace['object'],
				'跟踪' => $trace['args'] ? htmlspecialchars(substr(json_encode($trace['args'],JSON_UNESCAPED_UNICODE),0,1024), ENT_QUOTES): '',
			];
		}else{
			$traceList[] = [
				'文件' => isset($trace['file']) ? relDir($trace['file']) : '',
				'行' => $trace['line'] ?: '',
				'类' => $trace['class'] ?: '',
				'&nbsp' => $trace['type'] ?: '',
				'方法' => $trace['function'] ?: '',
			];
		}
	}
	$styles = [
		'文件' => 'white-space:nowrap;',
		'行' => 'white-space:nowrap;',
		'类' => 'white-space:nowrap;',
		'&nbsp' => 'min-width:16px',
		'方法' => 'white-space:nowrap;',
		'对象' => 'white-space:nowrap;',
		'跟踪' => 'white-space:nowrap;overflow: hidden;text-overflow: ellipsis;',
	];
	$traceTable = dumpTableResult($traceList,$styles);
	return [$caller['file'], $caller['line'], $traceTable];
}