Skip to content

Commit

Permalink
feat: 优化错误信息
Browse files Browse the repository at this point in the history
  • Loading branch information
rehiy committed Jul 27, 2023
1 parent 26d39d0 commit 0316968
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
7 changes: 3 additions & 4 deletions library/app/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ public static function boot($url)
require APP_MODULES . 'route.php';
}
// 回退路由
$router->map('GET', '*', function ($args) {
$model = new ErrorModel($args);
$model->warning('not found', $args);
return $model;
$router->map('GET', '*', function () {
header('HTTP/1.1 404 Not Found');
AppHelper::message('请求的页面不存在', 'info');
});
// 匹配路由
$request = $router->match($url ?: '/');
Expand Down
32 changes: 29 additions & 3 deletions library/app/app_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,50 @@ class AppHelper
{
/**
* 读写缓存数据
* @param string $name
* @param string $file
* @param mixed $data
* @return mixed
*/
public static function storage($file, $data = true)
{
//读取数据存储
if ($data === true) {
return is_file($file) ? include($file) : array();
return is_file($file) ? include($file) : [];
}
//读取有效数据
if (is_numeric($data)) {
$time = is_file($file) ? filemtime($file) : 0;
return $time > $data ? self::storage($name) : false;
}
//写入数据存储
$sdir = dirname($file);
$data = var_export($data, true);
is_dir(dirname($file)) || mkdir(dirname($file), 0755, true);
is_dir($sdir) || mkdir($sdir, 0755, true);
return file_put_contents($file, "<?php\nreturn {$data};\n");
}

/**
* 输出提示信息
* @param string|array $text
* @param string $type
* @return void
*/
public static function message($text, $type = 'info')
{
echo '<html>';
echo '<meta charset="utf-8" />';
echo '<title>提示消息</title>';
echo '<meta content="width=device-width,initial-scale=1.0" name="viewport" />';
echo '<link href="assets/vendor/bootstrap/bootstrap.min.css" rel="stylesheet" />';
echo '<body>';
echo '<div class="container p-4">';
$text = is_array($text) ? $text : [$text];
array_walk($text, function ($text) use ($type) {
echo "<div class=\"alert alert-{$type}\">{$text}</div>";
});
echo '</div>';
echo '</body>';
echo '</html>';
exit;
}
}
5 changes: 2 additions & 3 deletions library/app/app_router.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AppRouter
'h' => '[0-9A-Fa-f]++',
'*' => '.+?',
'**' => '.++',
'' => '[^/\.]++'
'' => '[^/\.]++',
];

/**
Expand Down Expand Up @@ -195,7 +195,7 @@ public function match($requestUrl = null, $requestMethod = null)
foreach ($this->routes as $handler) {
list($methods, $route, $target, $name) = $handler;

$method_match = (stripos($methods, $requestMethod) !== false);
$method_match = stripos($methods, $requestMethod) !== false;

// Method did not match, continue to next route.
if (!$method_match) {
Expand All @@ -218,7 +218,6 @@ public function match($requestUrl = null, $requestMethod = null)
if (strncmp($requestUrl, $route, $position) !== 0 && ($lastRequestUrlChar === '/' || $route[$position - 1] !== '/')) {
continue;
}

$regex = $this->compileRoute($route);
$match = preg_match($regex, $requestUrl, $params) === 1;
}
Expand Down
12 changes: 12 additions & 0 deletions library/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,15 @@
return true;
}
});

// 注册错误处理函数

set_error_handler(function ($no, $str, $file, $line) {
AppHelper::message("{$str} in {$file} on line {$line}", 'danger');
});

// 注册异常处理函数

set_exception_handler(function ($e) {
AppHelper::message($e->getMessage(), 'warning');
});

0 comments on commit 0316968

Please sign in to comment.