Skip to content

Commit 6be47c3

Browse files
committed
add some new util class
1 parent d34dc96 commit 6be47c3

File tree

3 files changed

+311
-0
lines changed

3 files changed

+311
-0
lines changed

src/Helper/PhpHelper.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
use ReflectionException;
1616
use ReflectionMethod;
1717
use RuntimeException;
18+
use Throwable;
1819
use Toolkit\Stdlib\Obj\ObjectHelper;
20+
use Toolkit\Stdlib\Util\PhpError;
21+
use Toolkit\Stdlib\Util\PhpException;
1922
use function array_sum;
23+
use function error_get_last;
2024
use function explode;
2125
use function fopen;
2226
use function ftok;
@@ -248,6 +252,19 @@ public static function runtime(int $startTime, $startMem, array $info = [], bool
248252
return $info;
249253
}
250254

255+
/**
256+
* Returns the last occurred PHP error or an empty string if no error occurred.
257+
*
258+
* @return string
259+
*/
260+
public static function getLastError(): string
261+
{
262+
$message = error_get_last()['message'] ?? '';
263+
// $message = ini_get('html_errors') ? Html::htmlToText($message) : $message;
264+
265+
return preg_replace('#^\w+\(.*?\): #', '', $message);
266+
}
267+
251268
/**
252269
* Usage:
253270
*
@@ -322,6 +339,38 @@ public static function exportVar(...$vars): string
322339
return preg_replace('/=>\s+\n\s+array \(/', '=> array (', $string);
323340
}
324341

342+
/**
343+
* @return array
344+
*/
345+
public static function lastError2array(): array
346+
{
347+
return PhpError::lastError2array();
348+
}
349+
350+
/**
351+
* @param Throwable $e
352+
* @param bool $getTrace
353+
* @param string|null $catcher
354+
*
355+
* @return string
356+
*/
357+
public static function exception2string(Throwable $e, bool $getTrace = true, string $catcher = null): string
358+
{
359+
return PhpException::toString($e, $getTrace, $catcher);
360+
}
361+
362+
/**
363+
* @param Throwable $e
364+
* @param bool $getTrace
365+
* @param string|null $catcher
366+
*
367+
* @return string
368+
*/
369+
public static function exception2html(Throwable $e, bool $getTrace = true, string $catcher = null): string
370+
{
371+
return PhpException::toHtml($e, $getTrace, $catcher);
372+
}
373+
325374
/**
326375
* @param string $pathname
327376
* @param int|string $projectId This must be a one character

src/Util/PhpError.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/sys-utils.
4+
*
5+
* @author https://github.com/inhere
6+
* @link https://github.com/php-toolkit/sys-utils
7+
* @license MIT
8+
*/
9+
10+
namespace Toolkit\Stdlib\Util;
11+
12+
use function error_get_last;
13+
use const E_COMPILE_ERROR;
14+
use const E_COMPILE_WARNING;
15+
use const E_CORE_ERROR;
16+
use const E_CORE_WARNING;
17+
use const E_DEPRECATED;
18+
use const E_ERROR;
19+
use const E_NOTICE;
20+
use const E_PARSE;
21+
use const E_RECOVERABLE_ERROR;
22+
use const E_STRICT;
23+
use const E_USER_DEPRECATED;
24+
use const E_USER_ERROR;
25+
use const E_USER_NOTICE;
26+
use const E_USER_WARNING;
27+
use const E_WARNING;
28+
29+
/**
30+
* Class PhpError
31+
*
32+
* @package Toolkit\Stdlib\Util
33+
*/
34+
class PhpError
35+
{
36+
/**
37+
* @var array
38+
*/
39+
public static $fatalErrors = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR];
40+
41+
/**
42+
* @return array
43+
*/
44+
public static function lastError2array(): array
45+
{
46+
return self::toArray(error_get_last());
47+
}
48+
49+
/**
50+
* $lastError = error_get_last();
51+
*
52+
* @param array $lastError
53+
* @param null|string $catcher
54+
*
55+
* @return array
56+
*/
57+
public static function toArray(array $lastError, string $catcher = null): array
58+
{
59+
$digest = 'Fatal Error (' . self::codeToString($lastError['type']) . '): ' . $lastError['message'];
60+
$data = [
61+
'code' => $lastError['type'],
62+
'message' => $lastError['message'],
63+
'file' => $lastError['file'],
64+
'line' => $lastError['line'],
65+
'catcher' => __METHOD__,
66+
];
67+
68+
if ($catcher) {
69+
$data['catcher'] = $catcher;
70+
}
71+
72+
return [$digest, $data];
73+
}
74+
75+
/**
76+
* @param int $code
77+
*
78+
* @return string
79+
*/
80+
public static function codeToString(int $code): string
81+
{
82+
switch ($code) {
83+
case E_ERROR:
84+
return 'E_ERROR';
85+
case E_WARNING:
86+
return 'E_WARNING';
87+
case E_PARSE:
88+
return 'E_PARSE';
89+
case E_NOTICE:
90+
return 'E_NOTICE';
91+
case E_CORE_ERROR:
92+
return 'E_CORE_ERROR';
93+
case E_CORE_WARNING:
94+
return 'E_CORE_WARNING';
95+
case E_COMPILE_ERROR:
96+
return 'E_COMPILE_ERROR';
97+
case E_COMPILE_WARNING:
98+
return 'E_COMPILE_WARNING';
99+
case E_USER_ERROR:
100+
return 'E_USER_ERROR';
101+
case E_USER_WARNING:
102+
return 'E_USER_WARNING';
103+
case E_USER_NOTICE:
104+
return 'E_USER_NOTICE';
105+
case E_STRICT:
106+
return 'E_STRICT';
107+
case E_RECOVERABLE_ERROR:
108+
return 'E_RECOVERABLE_ERROR';
109+
case E_DEPRECATED:
110+
return 'E_DEPRECATED';
111+
case E_USER_DEPRECATED:
112+
return 'E_USER_DEPRECATED';
113+
}
114+
115+
return 'Unknown PHP error';
116+
}
117+
}

src/Util/PhpException.php

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of toolkit/sys-utils.
4+
*
5+
* @author https://github.com/inhere
6+
* @link https://github.com/php-toolkit/sys-utils
7+
* @license MIT
8+
*/
9+
10+
namespace Toolkit\Stdlib\Util;
11+
12+
use Throwable;
13+
use function get_class;
14+
use function json_encode;
15+
use function strip_tags;
16+
17+
/**
18+
* Class PhpException
19+
*
20+
* @package Toolkit\Stdlib\Util
21+
*/
22+
class PhpException
23+
{
24+
/**
25+
* @param Throwable $e
26+
* @param bool $getTrace
27+
* @param string|null $catcher
28+
*
29+
* @return string
30+
* @see PhpException::toHtml()
31+
*/
32+
public static function toText(Throwable $e, bool $getTrace = true, string $catcher = null): string
33+
{
34+
return self::toHtml($e, $getTrace, $catcher, true);
35+
}
36+
37+
/**
38+
* @param Throwable $e
39+
* @param bool $getTrace
40+
* @param string|null $catcher
41+
*
42+
* @return string
43+
* @see PhpException::toHtml()
44+
*/
45+
public static function toString(Throwable $e, bool $getTrace = true, string $catcher = null): string
46+
{
47+
return self::toHtml($e, $getTrace, $catcher, true);
48+
}
49+
50+
/**
51+
* Converts an exception into a simple string.
52+
*
53+
* @param Throwable $e the exception being converted
54+
* @param bool $clearHtml
55+
* @param bool $getTrace
56+
* @param null|string $catcher
57+
*
58+
* @return string the string representation of the exception.
59+
*/
60+
public static function toHtml(Throwable $e, bool $getTrace = true, string $catcher = null, bool $clearHtml = false): string
61+
{
62+
if (!$getTrace) {
63+
$message = "Error: {$e->getMessage()}";
64+
} else {
65+
$message = sprintf(
66+
"<h3>%s(%d): %s</h3>\n<pre><strong>File: %s(Line %d)</strong>%s \n\n%s</pre>",
67+
get_class($e),
68+
$e->getCode(),
69+
$e->getMessage(),
70+
$e->getFile(),
71+
$e->getLine(),
72+
$catcher ? "\nCatch By: $catcher" : '',
73+
$e->getTraceAsString()
74+
);
75+
}
76+
77+
return $clearHtml ? strip_tags($message) : "<div class=\"exception-box\">$message</div>";
78+
}
79+
80+
/**
81+
* Converts an exception into a simple array.
82+
*
83+
* @param Throwable $e the exception being converted
84+
* @param bool $getTrace
85+
* @param null|string $catcher
86+
*
87+
* @return array
88+
*/
89+
public static function toArray(Throwable $e, bool $getTrace = true, string $catcher = null): array
90+
{
91+
$data = [
92+
'class' => get_class($e),
93+
'message' => $e->getMessage(),
94+
'code' => $e->getCode(),
95+
'file' => $e->getFile() . ':' . $e->getLine(),
96+
];
97+
98+
if ($catcher) {
99+
$data['catcher'] = $catcher;
100+
}
101+
102+
if ($getTrace) {
103+
$data['trace'] = $e->getTrace();
104+
}
105+
106+
return $data;
107+
}
108+
109+
/**
110+
* Converts an exception into a json string.
111+
*
112+
* @param Throwable $e the exception being converted
113+
* @param bool $getTrace
114+
* @param null|string $catcher
115+
*
116+
* @return string the string representation of the exception.
117+
*/
118+
public static function toJson(Throwable $e, bool $getTrace = true, string $catcher = null): string
119+
{
120+
$errMsg = $e->getMessage();
121+
if (!$getTrace) {
122+
return json_encode(['msg' => "Error: $errMsg"]);
123+
}
124+
125+
$map = [
126+
'code' => $e->getCode() ?: 500,
127+
'msg' => sprintf(
128+
'%s(%d): %s, File: %s(Line %d)',
129+
get_class($e),
130+
$e->getCode(),
131+
$errMsg,
132+
$e->getFile(),
133+
$e->getLine()
134+
),
135+
'data' => $e->getTrace()
136+
];
137+
138+
if ($catcher) {
139+
$map['catcher'] = $catcher;
140+
}
141+
142+
$map['trace'] = $e->getTrace();
143+
return json_encode($map);
144+
}
145+
}

0 commit comments

Comments
 (0)