Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
namespace App\Service\Exception;
use App\Application as app;
use Whoops\Exception\Inspector;
abstract class Formatter
{
/** @var Inspector */
protected $inspector;
/** @var array */
protected $options = [];
/**
* Formatter constructor.
*
* @param Inspector $inspector
* @param array $options
*/
public function __construct(Inspector $inspector, array $options = [])
{
$this->inspector = $inspector;
$this->options = array_merge_recursive($this->options, $options);
}
abstract public function formatMessage(): string;
abstract public function formatTrace(): string;
protected function replacePath(string $path): string
{
try {
$projectPath = app::config()->env('PROJECT_PATH');
if ($projectPath) {
$path = preg_replace(
'~^' . app::app()->getBasePath() . '~',
$projectPath,
$path
);
}
} catch (\Throwable $e) {
// ignore this error because we are already in an error handler
}
return $path;
}
protected function generateArgs(array $args): string
{
$result = [];
foreach ($args as $arg) {
switch (gettype($arg)) {
case 'object':
$result[] = get_class($arg);
break;
case 'string':
if (!class_exists($arg)) {
$arg = strlen($arg) > 20 ? substr($arg, 0, 20) . '…' : $arg;
}
$result[] = sprintf('"%s"', $arg);
break;
case 'integer':
case 'double':
$result[] = (string)$arg;
break;
case 'boolean':
$result[] = $arg ? 'true' : 'false';
break;
default:
$result[] = gettype($arg);
break;
}
}
return implode(', ', $result);
}
}