Skip to content

Commit 6ff6fda

Browse files
committed
up: move some util class to stdlib
1 parent 4b3cdd8 commit 6ff6fda

File tree

2 files changed

+4
-217
lines changed

2 files changed

+4
-217
lines changed

src/Util/PhpError.php

Lines changed: 2 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -9,98 +9,12 @@
99

1010
namespace Toolkit\Sys\Util;
1111

12-
use const E_COMPILE_ERROR;
13-
use const E_COMPILE_WARNING;
14-
use const E_CORE_ERROR;
15-
use const E_CORE_WARNING;
16-
use const E_DEPRECATED;
17-
use const E_ERROR;
18-
use const E_NOTICE;
19-
use const E_PARSE;
20-
use const E_RECOVERABLE_ERROR;
21-
use const E_STRICT;
22-
use const E_USER_DEPRECATED;
23-
use const E_USER_ERROR;
24-
use const E_USER_NOTICE;
25-
use const E_USER_WARNING;
26-
use const E_WARNING;
27-
2812
/**
2913
* Class PhpError
3014
*
3115
* @package Toolkit\Sys\Util
16+
* @deprecated
3217
*/
33-
class PhpError
18+
class PhpError extends \Toolkit\Stdlib\Util\PhpError
3419
{
35-
/** @var array */
36-
public static $fatalErrors = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR];
37-
38-
/**
39-
* $lastError = error_get_last();
40-
*
41-
* @param array $lastError
42-
* @param null|string $catcher
43-
*
44-
* @return array
45-
*/
46-
public static function toArray(array $lastError, $catcher = null): array
47-
{
48-
$digest = 'Fatal Error (' . self::codeToString($lastError['type']) . '): ' . $lastError['message'];
49-
$data = [
50-
'code' => $lastError['type'],
51-
'message' => $lastError['message'],
52-
'file' => $lastError['file'],
53-
'line' => $lastError['line'],
54-
'catcher' => __METHOD__,
55-
];
56-
57-
if ($catcher) {
58-
$data['catcher'] = $catcher;
59-
}
60-
61-
return [$digest, $data];
62-
}
63-
64-
/**
65-
* @param int $code
66-
*
67-
* @return string
68-
*/
69-
public static function codeToString(int $code): string
70-
{
71-
switch ($code) {
72-
case E_ERROR:
73-
return 'E_ERROR';
74-
case E_WARNING:
75-
return 'E_WARNING';
76-
case E_PARSE:
77-
return 'E_PARSE';
78-
case E_NOTICE:
79-
return 'E_NOTICE';
80-
case E_CORE_ERROR:
81-
return 'E_CORE_ERROR';
82-
case E_CORE_WARNING:
83-
return 'E_CORE_WARNING';
84-
case E_COMPILE_ERROR:
85-
return 'E_COMPILE_ERROR';
86-
case E_COMPILE_WARNING:
87-
return 'E_COMPILE_WARNING';
88-
case E_USER_ERROR:
89-
return 'E_USER_ERROR';
90-
case E_USER_WARNING:
91-
return 'E_USER_WARNING';
92-
case E_USER_NOTICE:
93-
return 'E_USER_NOTICE';
94-
case E_STRICT:
95-
return 'E_STRICT';
96-
case E_RECOVERABLE_ERROR:
97-
return 'E_RECOVERABLE_ERROR';
98-
case E_DEPRECATED:
99-
return 'E_DEPRECATED';
100-
case E_USER_DEPRECATED:
101-
return 'E_USER_DEPRECATED';
102-
}
103-
104-
return 'Unknown PHP error';
105-
}
10620
}

src/Util/PhpException.php

Lines changed: 2 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -9,139 +9,12 @@
99

1010
namespace Toolkit\Sys\Util;
1111

12-
use Throwable;
13-
use function get_class;
14-
use function json_encode;
15-
use function strip_tags;
16-
1712
/**
1813
* Class PhpException
1914
*
2015
* @package Toolkit\Sys\Util
16+
* @deprecated
2117
*/
22-
class PhpException
18+
class PhpException extends \Toolkit\Stdlib\Util\PhpException
2319
{
24-
/**
25-
* @param Throwable $e
26-
* @param bool $getTrace
27-
* @param null $catcher
28-
*
29-
* @return string
30-
* @see PhpException::toHtml()
31-
*/
32-
public static function toText(Throwable $e, bool $getTrace = true, $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 null $catcher
41-
*
42-
* @return string
43-
* @see PhpException::toHtml()
44-
*/
45-
public static function toString(Throwable $e, bool $getTrace = true, $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-
if (!$getTrace) {
121-
return json_encode(['msg' => "Error: {$e->getMessage()}"]);
122-
}
123-
124-
$map = [
125-
'code' => $e->getCode() ?: 500,
126-
'msg' => sprintf(
127-
'%s(%d): %s, File: %s(Line %d)',
128-
get_class($e),
129-
$e->getCode(),
130-
$e->getMessage(),
131-
$e->getFile(),
132-
$e->getLine()
133-
),
134-
'data' => $e->getTrace()
135-
];
136-
137-
if ($catcher) {
138-
$map['catcher'] = $catcher;
139-
}
140-
141-
if ($getTrace) {
142-
$map['trace'] = $e->getTrace();
143-
}
144-
145-
return json_encode($map);
146-
}
14720
}

0 commit comments

Comments
 (0)