重构Error类以及异常模板

This commit is contained in:
麦当苗儿
2016-02-01 01:24:02 +08:00
parent 5ddad39f52
commit c19d23c248
2 changed files with 672 additions and 137 deletions

View File

@@ -6,129 +6,279 @@
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
// +----------------------------------------------------------------------
namespace think;
use think\Exception;
use think\exception\ErrorException;
use think\exception\NotFoundException;
class Error
{
/**
* 自定义异常处理
* @access public
* @param mixed $e 异常对象
* 注册异常处理
* @return null
*/
public static function appException($e)
public static function register()
{
$error = [
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString(),
'code' => $e->getCode(),
// Workaround PHP bug 42098
// https://bugs.php.net/bug.php?id=42098
// class_exists(think\exception\ErrorException::class);
set_error_handler([__CLASS__, 'appError']);
set_exception_handler([__CLASS__, 'appException']);
register_shutdown_function([__CLASS__, 'appShutdown']);
}
/**
* Exception Handler
* @param \Exception $exception
* @return Boolean true 禁止往下传播已处理过的异常
*/
public static function appException(\Exception $exception)
{
/* 非API模式下的部署模式跳转到指定的 Error Page */
if(!(APP_DEBUG || IS_API)){
$error_page = Config::get('error_page');
if (!empty($error_page)) {
header("Location: {$error_page}");
}
}
/* 收集异常数据 */
if(APP_DEBUG){
/* 调试模式,获取详细的错误信息 */
$data = [
'name' => get_class($exception),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'message' => $exception->getMessage(),
'trace' => $exception->getTrace(),
'code' => self::getCode($exception),
'source' => self::getSourceCode($exception),
'datas' => self::getExtendData($exception),
'tables' => [
'GET Data' => $_GET,
'POST Data' => $_POST,
'Files' => $_FILES,
'Cookies' => $_COOKIE,
'Session' => isset($_SESSION) ? $_SESSION : [],
'Server/Request Data' => $_SERVER,
'Environment Variables' => $_ENV,
'ThinkPHP Constants' => self::getTPConst(),
]
];
// 发送http状态信息
http_response_code(Config::get('exception_http_status'));
// 输出异常页面
self::halt($error);
} else {
/* 部署模式仅显示 Code 和 Message */
$data = [
'code' => $exception->getCode(),
'message' => Config::get('show_error_msg') ? $exception->getMessage() : Config::get('error_message')
];
}
/* 输出错误信息 */
self::output($exception, $data);
// 禁止往下传播已处理过的异常
return true;
}
/**
* 自定义错误处理
* @access public
* @param int $error_number 错误类型
* @param string $error_string 错误信息
* @param string $error_file 错误文件
* @param int $error_line 错误行数
* @return void
* Error Handler
* @param integer $errno 错误编号
* @param integer $errstr 详细错误信息
* @param string $errfile 出错的文件
* @param integer $errline 出错行号
* @return boolean true 禁止往下传播已处理过的异常
*/
public static function appError($error_number, $error_string, $error_file, $error_line)
public static function appError($errno, $errstr, $errfile, $errline)
{
$errorStr = "[{$error_number}] {$error_string} {$error_file}{$error_line} 行.";
switch ($error_number) {
case E_USER_ERROR:
self::halt($errorStr, $error_number);
break;
case E_STRICT:
case E_USER_WARNING:
case E_USER_NOTICE:
default:
Log::record($errorStr, 'notic');
break;
}
// 将错误信息托管至 think\exception\ErrorException
throw new ErrorException($errno, $errstr, $errfile, $errline);
// 禁止往下传播已处理过的异常
return true;
}
/**
* 应用关闭处理
* @return void
* Shutdown Handler
* @return boolean true-禁止往下传播已处理过的异常; false-未处理的异常继续传播
*/
public static function appShutdown()
{
// 记录日志
Log::save();
if ($e = error_get_last()) {
switch ($e['type']) {
case E_ERROR:
case E_PARSE:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
ob_end_clean();
self::halt($e);
break;
if($error = error_get_last()){
// 将错误信息托管至think\ErrorException
$exception = new ErrorException(
$error['type'],
$error['message'],
$error['file'],
$error['line']
);
/**
* Shutdown handler 中的异常将不被往下传播
* 所以,这里我们必须手动传播而不能像 Error handler 中那样 throw
*/
self::appException($exception);
// 禁止往下传播已处理过的异常
return true;
}
return false;
}
/**
* 输出异常信息
* @param \Exception $exception
* @param Array $vars 异常信息
* @return null
*/
public static function output(\Exception $exception, Array $vars)
{
if($exception instanceof Exception){
http_response_code($exception->getHttpStatus());
} else {
http_response_code(500);
}
// header('Content-Type: application/json');
// echo json_encode($vars);exit;
$type = Config::get('default_return_type');
if (IS_API && 'html' != $type) {
// 异常信息输出监听
APP_HOOK && Hook::listen('error_output', $data);
// 输出异常内容
Response::send($data, $type, Config::get('response_return'));
} else {
ob_end_clean();
extract($vars);
include Config::get('exception_tmpl');
}
}
/**
* 错误输出
*
* @param mixed $error 错误
* @param int $code
* 获取错误编码
* ErrorException则使用错误级别作为错误编码
* @param \Exception $exception
* @return integer 错误编码
*/
public static function halt($error, $code = 1)
private static function getCode(\Exception $exception)
{
$message = is_array($error) ? $error['message'] : $error;
$code = isset($error['code']) ? $error['code'] : $code;
$code = $exception->getCode();
if (APP_DEBUG) {
//调试模式下输出错误信息
if (!is_array($error)) {
$trace = debug_backtrace();
$e['message'] = $error;
$e['code'] = $code;
$e['file'] = $trace[0]['file'];
$e['line'] = $trace[0]['line'];
ob_start();
debug_print_backtrace();
$e['trace'] = ob_get_clean();
} else {
$e = $error;
if(!$code && $exception instanceof ErrorException){
$code = $exception->getSeverity();
}
} elseif (!IS_API) {
//否则定向到错误页面
$error_page = Config::get('error_page');
if (!empty($error_page)) {
header('Location: ' . $error_page);
} else {
$e['code'] = $code;
$e['message'] = Config::get('show_error_msg') ? $message : Config::get('error_message');
}
} else {
$e = ['message' => $message, 'code' => $code];
}
// 记录异常日志
Log::write('[' . $e['code'] . '] ' . $e['message'] . '[' . $e['file'] . ' : ' . $e['line'] . ']', 'error');
$type = Config::get('default_return_type');
if (!IS_API && 'html' == $type) {
include Config::get('exception_tmpl');
} else {
// 异常信息输出监听
APP_HOOK && Hook::listen('error_output', $e);
// 输出异常内容
Response::send($e, $type, Config::get('response_return'));
return $code;
}
exit;
/**
* 获取出错文件内容
* 获取错误的前9行和后9行
* @param \Exception $exception
* @return array 错误文件内容
*/
private static function getSourceCode(\Exception $exception)
{
// 读取前9行和后9行
$line = $exception->getLine();
$first = ($line - 9 > 0) ? $line - 9 : 1;
try {
$contents = file($exception->getFile());
$source = [
'first' => $first,
'source' => array_slice($contents, $first - 1, 19)
];
} catch (Exception $e){
$source = [];
}
return $source;
}
/**
* 获取异常扩展信息
* 用于非调试模式html返回类型显示
* @param \Exception $exception
* @return array 异常类定义的扩展数据
*/
private static function getExtendData(\Exception $exception)
{
$data = [];
if($exception instanceof Exception){
$data = $exception->getData();
}
return $data;
}
/**
* 获取ThinkPHP常量列表
* @return array 常量列表
*/
private static function getTPConst()
{
return [
'THINK_VERSION' => defined('THINK_VERSION') ? THINK_VERSION : 'undefined',
'THINK_PATH' => defined('THINK_PATH') ? THINK_PATH : 'undefined',
'LIB_PATH' => defined('LIB_PATH') ? LIB_PATH : 'undefined',
'EXTEND_PATH' => defined('EXTEND_PATH') ? EXTEND_PATH : 'undefined',
'MODE_PATH' => defined('MODE_PATH') ? MODE_PATH : 'undefined',
'CORE_PATH' => defined('CORE_PATH') ? CORE_PATH : 'undefined',
'ORG_PATH' => defined('ORG_PATH') ? ORG_PATH : 'undefined',
'TRAIT_PATH' => defined('TRAIT_PATH') ? TRAIT_PATH : 'undefined',
'APP_PATH' => defined('APP_PATH') ? APP_PATH : 'undefined',
'RUNTIME_PATH' => defined('RUNTIME_PATH') ? RUNTIME_PATH : 'undefined',
'DATA_PATH' => defined('DATA_PATH') ? DATA_PATH : 'undefined',
'LOG_PATH' => defined('LOG_PATH') ? LOG_PATH : 'undefined',
'CACHE_PATH' => defined('CACHE_PATH') ? CACHE_PATH : 'undefined',
'TEMP_PATH' => defined('TEMP_PATH') ? TEMP_PATH : 'undefined',
'VENDOR_PATH' => defined('VENDOR_PATH') ? VENDOR_PATH : 'undefined',
'MODULE_PATH' => defined('MODULE_PATH') ? MODULE_PATH : 'undefined',
'VIEW_PATH' => defined('VIEW_PATH') ? VIEW_PATH : 'undefined',
'APP_NAMESPACE' => defined('APP_NAMESPACE') ? APP_NAMESPACE : 'undefined',
'COMMON_MODULE' => defined('COMMON_MODULE') ? COMMON_MODULE : 'undefined',
'APP_MULTI_MODULE' => defined('APP_MULTI_MODULE') ? APP_MULTI_MODULE : 'undefined',
'MODULE_ALIAS' => defined('MODULE_ALIAS') ? MODULE_ALIAS : 'undefined',
'MODULE_NAME' => defined('MODULE_NAME') ? MODULE_NAME : 'undefined',
'CONTROLLER_NAME' => defined('CONTROLLER_NAME') ? CONTROLLER_NAME : 'undefined',
'ACTION_NAME' => defined('ACTION_NAME') ? ACTION_NAME : 'undefined',
'MODEL_LAYER' => defined('MODEL_LAYER') ? MODEL_LAYER : 'undefined',
'VIEW_LAYER' => defined('VIEW_LAYER') ? VIEW_LAYER : 'undefined',
'CONTROLLER_LAYER' => defined('CONTROLLER_LAYER') ? CONTROLLER_LAYER : 'undefined',
'APP_DEBUG' => defined('APP_DEBUG') ? APP_DEBUG : 'undefined',
'APP_HOOK' => defined('APP_HOOK') ? APP_HOOK : 'undefined',
'ENV_PREFIX' => defined('ENV_PREFIX') ? ENV_PREFIX : 'undefined',
'IS_API' => defined('IS_API') ? IS_API : 'undefined',
'APP_AUTO_BUILD' => defined('APP_AUTO_BUILD') ? APP_AUTO_BUILD : 'undefined',
'APP_AUTO_RUN' => defined('APP_AUTO_RUN') ? APP_AUTO_RUN : 'undefined',
'APP_MODE' => defined('APP_MODE') ? APP_MODE : 'undefined',
'REQUEST_METHOD' => defined('REQUEST_METHOD') ? REQUEST_METHOD : 'undefined',
'IS_CGI' => defined('IS_CGI') ? IS_CGI : 'undefined',
'IS_WIN' => defined('IS_WIN') ? IS_WIN : 'undefined',
'IS_CLI' => defined('IS_CLI') ? IS_CLI : 'undefined',
'IS_AJAX' => defined('IS_AJAX') ? IS_AJAX : 'undefined',
'IS_GET' => defined('IS_GET') ? IS_GET : 'undefined',
'IS_POST' => defined('IS_POST') ? IS_POST : 'undefined',
'IS_PUT' => defined('IS_PUT') ? IS_PUT : 'undefined',
'IS_DELETE' => defined('IS_DELETE') ? IS_DELETE : 'undefined',
'NOW_TIME' => defined('NOW_TIME') ? NOW_TIME : 'undefined',
'LANG_SET' => defined('LANG_SET') ? LANG_SET : 'undefined',
'EXT' => defined('EXT') ? EXT : 'undefined',
'DS' => defined('DS') ? DS : 'undefined',
'__INFO__' => defined('__INFO__') ? __INFO__ : 'undefined',
'__EXT__' => defined('__EXT__') ? __EXT__ : 'undefined',
'__INFO__' => defined('__INFO__') ? __INFO__ : 'undefined',
'__EXT__' => defined('__EXT__') ? __EXT__ : 'undefined',
];
}
}

View File

@@ -1,53 +1,438 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>系统发生错误</title>
<style type="text/css">
*{ padding: 0; margin: 0; }
html{ overflow-y: scroll; }
body{ background: #fff; font-family: "Microsoft Yahei","Helvetica Neue",Helvetica,Arial,sans-serif; color: #333; font-size: 16px; }
img{ border: 0; }
.error{ padding: 24px 48px; }
.face{ font-size: 6.25em; color:red;font-weight: 400; line-height: 1.0; margin-bottom: .15em; }
.error .content{ padding-top: 10px; }
.error .info{ margin-bottom: 12px; }
.error .info .title{ margin-bottom: 3px; }
h1{ font-size: 2.75em; line-height: 1.2; font-weight:200; }
h2{ padding-bottom: .3em; line-height: 1.225; border-bottom: 1px solid #eee; color: #000; font-weight: 300; font-size: 1.75em; }
.error .info .text{ line-height: 24px; }
.copyright{ font-weight: 200; padding: 12px 48px; color: #999; }
.copyright a{ color: #000; text-decoration: none; font-weight: 300; font-size: 1.1em; }
<meta name="robots" content="noindex,nofollow" />
<style>
/* Base */
body{
color: #333;
font: 14px Verdana, "Helvetica Neue", helvetica, Arial, 'Microsoft YaHei', sans-serif;
margin: 0px;
padding: 20px;
}
h1{
margin: 0;
font-size: 24px;
font-weight: 500;
line-height: 32px;
}
h2{
color: #4288ce;
font-weight: 400;
padding: 6px 0;
margin: 24px 0 0;
font-size: 18px;
border-bottom: 1px solid #eee;
}
h3{
margin: 12px;
font-size: 16px;
font-weight: bold;
}
abbr{
cursor: help;
text-decoration: underline;
text-decoration-style: dotted;
}
a{
color: #868686;
cursor: pointer;
}
a:hover{
text-decoration: underline;
}
.line-error{
background: #f8cbcb;
}
/* Exception Info */
.exception .code{
float: left;
text-align: center;
color: #fff;
margin-right: 12px;
padding: 16px;
border-radius: 4px;
background: #999;
}
.exception .source-code{
margin-top: 12px;
padding: 6px;
border: 1px solid #ddd;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
background: #f9f9f9;
overflow-x: auto;
}
.exception .source-code pre{
margin: 0;
font-family: Consolas,"Liberation Mono",Courier,"微软雅黑";
}
.exception .source-code pre ol{
margin: 0;
color: #4288ce;
display: inline-block;
min-width: 100%;
box-sizing: border-box;
padding-left: <?php echo parse_padding($source); ?>px;
}
.exception .source-code pre li{
border-left: 1px solid #ddd;
height: 18px;
line-height: 18px;
}
.exception .source-code pre code{
color: #333;
height: 100%;
padding-left: 6px;
display: inline-block;
border-left: 1px solid #fff;
}
.exception .trace{
padding: 6px;
border: 1px solid #ddd;
border-top: 0 none;
line-height: 16px;
}
.exception .trace ol{
margin: 12px;
}
.exception .trace ol li{
padding: 2px 4px;
}
.exception div:last-child{
border-bottom-left-radius: 4px;
border-bottom-right-radius: 4px;
}
/* Exception Variables */
.exception-var table{
width: 100%;
margin: 12px 0;
box-sizing: border-box;
}
.exception-var table caption{
text-align: left;
font-size: 16px;
font-weight: bold;
padding: 6px 0;
}
.exception-var table caption small{
font-weight: 300;
display: inline-block;
margin-left: 10px;
color: #ccc;
}
.exception-var table tbody{
font-size: 13px;
font-family: Consolas,"Liberation Mono",Courier,"微软雅黑";
}
.exception-var table td{
padding: 0 6px;
vertical-align: top;
}
.exception-var table td:first-child{
width: 12px;
font-weight: bold;
white-space: nowrap;
}
.exception-var table td pre{
margin: 0;
}
/* Copyright Info */
.copyright{
margin-top: 24px;
padding: 12px 0;
border-top: 1px solid #eee;
}
/* SPAN elements with the classes below are added by prettyprint. */
pre.prettyprint .pln { color: #000 } /* plain text */
pre.prettyprint .str { color: #080 } /* string content */
pre.prettyprint .kwd { color: #008 } /* a keyword */
pre.prettyprint .com { color: #800 } /* a comment */
pre.prettyprint .typ { color: #606 } /* a type name */
pre.prettyprint .lit { color: #066 } /* a literal value */
/* punctuation, lisp open bracket, lisp close bracket */
pre.prettyprint .pun, pre.prettyprint .opn, pre.prettyprint .clo { color: #660 }
pre.prettyprint .tag { color: #008 } /* a markup tag name */
pre.prettyprint .atn { color: #606 } /* a markup attribute name */
pre.prettyprint .atv { color: #080 } /* a markup attribute value */
pre.prettyprint .dec, pre.prettyprint .var { color: #606 } /* a declaration; a variable name */
pre.prettyprint .fun { color: red } /* a function name */
</style>
</head>
<body>
<div class="error">
<p class="face">:(</p>
<h1><?php if (!empty($e['code'])) { echo '<span style="color:#999">[ '.$e['code'].' ]</span> ';} echo strip_tags($e['message']);?></h1>
<div class="content">
<?php if (isset($e['file'])) {?>
<div class="exception">
<h1>
<span class="code"><?php echo $code; ?></span>
<div class="info">
<div class="title">
<h2></h2>
<div>
<?php echo sprintf('%s in %s', parse_class($name), parse_file($file, $line)); ?>
</div>
<div class="text">
<p>FILE: <?php echo $e['file'];?> &#12288;LINE: <?php echo $e['line'];?></p>
<div><?php echo htmlentities($message); ?></div>
</div>
</h1>
<div class="source-code">
<pre class="prettyprint lang-php"><ol start="<?php echo $source['first']; ?>"><?php foreach ($source['source'] as $key => $value) { ?><li class="line-<?php echo $key + $source['first']; ?>"><code><?php echo htmlentities($value); ?></code></li><?php } ?></ol></pre>
</div>
<div class="trace">
<h3>Call Stack</h3>
<ol>
<li><?php echo sprintf('in %s', parse_file($file, $line)); ?></li>
<?php foreach ($trace as $value) { ?>
<li>
<?php
// Show Function
if($value['function']){
echo sprintf(
'at %s%s%s(%s)',
isset($value['class']) ? parse_class($value['class']) : '',
isset($value['type']) ? $value['type'] : '',
$value['function'],
parse_args($value['args'])
);
}
// Show line
if (isset($value['file']) && isset($value['line'])) {
echo sprintf(' in %s', parse_file($value['file'], $value['line']));
}
?>
</li>
<?php } ?>
<?php if (isset($e['trace'])) {?>
<div class="info">
<div class="title">
<h2></h2>
</div>
<div class="text">
<p><?php echo nl2br($e['trace']);?></p>
</ol>
</div>
</div>
<?php if(!empty($datas)){ ?>
<div class="exception-var">
<h2>Exception Datas</h2>
<?php foreach ($datas as $label => $value) { ?>
<table>
<?php if(empty($value)){ ?>
<caption><?php echo $label; ?><small>empty</small></caption>
<?php } else { ?>
<caption><?php echo $label; ?></caption>
<tbody>
<?php foreach ($value as $key => $val) { ?>
<tr>
<td><?php echo htmlentities($key); ?></td>
<td>
<?php
if(is_array($val) || is_object($val)){
echo htmlentities(json_encode($val, JSON_PRETTY_PRINT));
} else if(is_bool($val)) {
echo $val ? 'true' : 'false';
} else {
echo htmlentities($val);
}
?>
</td>
</tr>
<?php } ?>
</tbody>
<?php } ?>
</table>
<?php } ?>
</div>
<?php } ?>
<div class="exception-var">
<h2>Environment Variables</h2>
<?php foreach ($tables as $label => $value) { ?>
<table>
<?php if(empty($value)){ ?>
<caption><?php echo $label; ?><small>empty</small></caption>
<?php } else { ?>
<caption><?php echo $label; ?></caption>
<tbody>
<?php foreach ($value as $key => $val) { ?>
<tr>
<td><?php echo htmlentities($key); ?></td>
<td>
<?php
if(is_array($val) || is_object($val)){
echo htmlentities(json_encode($val, JSON_PRETTY_PRINT));
} else if(is_bool($val)) {
echo $val ? 'true' : 'false';
} else {
echo htmlentities($val);
}
?>
</td>
</tr>
<?php } ?>
</tbody>
<?php } ?>
</table>
<?php } ?>
</div>
<div class="copyright">
<p><a title="官方网站" href="http://www.thinkphp.cn">ThinkPHP</a> <span>V<?php echo THINK_VERSION;?></span> { -API开发设计的高性能框架 }</p>
<a title="官方网站" href="http://www.thinkphp.cn">ThinkPHP</a>
<span>V<?php echo THINK_VERSION; ?></span>
<span>{ -API开发设计的高性能框架 }</span>
</div>
<script>
var LINE = <?php echo $line; ?>;
function $(selector, node){
var elements;
node = node || document;
if(document.querySelectorAll){
elements = node.querySelectorAll(selector);
} else {
switch(selector.substr(0, 1)){
case '#':
elements = [node.getElementById(selector.substr(1))];
break;
case '.':
if(document.getElementsByClassName){
elements = node.getElementsByClassName(selector.substr(1));
} else {
elements = get_elements_by_class(selector.substr(1), node);
}
break;
default:
elements = node.getElementsByTagName();
}
}
return elements;
function get_elements_by_class(search_class, node, tag) {
var elements = [], eles,
pattern = new RegExp('(^|\\s)' + search_class + '(\\s|$)');
node = node || document;
tag = tag || '*';
eles = node.getElementsByTagName(tag);
for(var i = 0; i < eles.length; i++) {
if(pattern.test(eles[i].className)) {
elements.push(eles[i])
}
}
return elements;
}
}
$.getScript = function(src, func){
var script = document.createElement('script');
script.async = 'async';
script.src = src;
script.onload = func || function(){};
$('head')[0].appendChild(script);
}
;(function(){
var files = $('.toggle');
var ol = $('ol', $('.prettyprint')[0]);
var li = $('li', ol[0]);
//
for(var i = 0; i < files.length; i++){
files[i].ondblclick = function(){
var title = this.title;
this.title = this.innerHTML;
this.innerHTML = title;
}
}
//
var err_line = $('.line-' + LINE, ol[0])[0];
err_line.className = err_line.className + ' line-error';
$.getScript('//cdn.bootcss.com/prettify/r298/prettify.min.js', function(){
prettyPrint();
// Firefox浏览器一个很诡异的问题
// ol的行号莫名其妙的错位
// li里面的html重新渲染就没有问题了
if(window.navigator.userAgent.indexOf('Firefox') >= 0){
ol[0].innerHTML = ol[0].innerHTML;
}
});
})();
</script>
</body>
</html>
<?php
function parse_padding($source)
{
$length = strlen(strval(count($source['source']) + $source['first']));
return 40 + ($length - 1) * 8;
}
function parse_class($name)
{
$names = explode('\\', $name);
return '<abbr title="'.$name.'">'.end($names).'</abbr>';
}
function parse_file($file, $line)
{
return '<a class="toggle" title="'."{$file} line {$line}".'">'.basename($file)." line {$line}".'</a>';
}
function parse_args($args)
{
$result = [];
foreach ($args as $key => $item) {
switch (true) {
case is_object($item):
$value = sprintf('<em>object</em>(%s)', parse_class(get_class($item)));
break;
case is_array($item):
if(count($item) > 3){
$value = sprintf('[%s, ...]', parse_args(array_slice($item, 0, 3)));
} else {
$value = sprintf('[%s]', parse_args($item));
}
break;
case is_string($item):
if(strlen($item) > 20){
$value = sprintf(
'\'<a class="toggle" title="%s">%s...</a>\'',
htmlentities($item),
htmlentities(substr($item, 0, 20))
);
} else {
$value = sprintf("'%s'", htmlentities($item));
}
break;
case is_int($item):
case is_float($item):
$value = $item;
break;
case is_null($item):
$value = '<em>null</em>';
break;
case is_bool($item):
$value = '<em>' . ($item ? 'true' : 'false') . '</em>';
break;
case is_resource($item):
$value = '<em>resource</em>';
break;
default:
$value = htmlentities(str_replace("\n", '', var_export(strval($item), true)));
break;
}
$result[] = is_int($key) ? $value : "'{$key}' => {$value}";
}
return implode(', ', $result);
}