| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Spatie\Ignition\ErrorPage;
|
|
|
4 |
|
|
|
5 |
use Spatie\FlareClient\Report;
|
|
|
6 |
use Spatie\FlareClient\Truncation\ReportTrimmer;
|
|
|
7 |
use Spatie\Ignition\Config\IgnitionConfig;
|
|
|
8 |
use Spatie\Ignition\Contracts\Solution;
|
|
|
9 |
use Spatie\Ignition\Solutions\SolutionTransformer;
|
|
|
10 |
use Throwable;
|
|
|
11 |
|
|
|
12 |
class ErrorPageViewModel
|
|
|
13 |
{
|
|
|
14 |
/**
|
|
|
15 |
* @param \Throwable|null $throwable
|
|
|
16 |
* @param \Spatie\Ignition\Config\IgnitionConfig $ignitionConfig
|
|
|
17 |
* @param \Spatie\FlareClient\Report $report
|
|
|
18 |
* @param array<int, Solution> $solutions
|
|
|
19 |
* @param string|null $solutionTransformerClass
|
|
|
20 |
*/
|
|
|
21 |
public function __construct(
|
|
|
22 |
protected ?Throwable $throwable,
|
|
|
23 |
protected IgnitionConfig $ignitionConfig,
|
|
|
24 |
protected Report $report,
|
|
|
25 |
protected array $solutions,
|
|
|
26 |
protected ?string $solutionTransformerClass = null,
|
|
|
27 |
protected string $customHtmlHead = '',
|
|
|
28 |
protected string $customHtmlBody = ''
|
|
|
29 |
) {
|
|
|
30 |
$this->solutionTransformerClass ??= SolutionTransformer::class;
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
public function throwableString(): string
|
|
|
34 |
{
|
|
|
35 |
if (! $this->throwable) {
|
|
|
36 |
return '';
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
$throwableString = sprintf(
|
|
|
40 |
"%s: %s in file %s on line %d\n\n%s\n",
|
|
|
41 |
get_class($this->throwable),
|
|
|
42 |
$this->throwable->getMessage(),
|
|
|
43 |
$this->throwable->getFile(),
|
|
|
44 |
$this->throwable->getLine(),
|
|
|
45 |
$this->report->getThrowable()?->getTraceAsString()
|
|
|
46 |
);
|
|
|
47 |
|
|
|
48 |
return htmlspecialchars($throwableString);
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public function title(): string
|
|
|
52 |
{
|
|
|
53 |
return htmlspecialchars($this->report->getMessage());
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* @return array<string, mixed>
|
|
|
58 |
*/
|
|
|
59 |
public function config(): array
|
|
|
60 |
{
|
|
|
61 |
return $this->ignitionConfig->toArray();
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
public function theme(): string
|
|
|
65 |
{
|
|
|
66 |
return $this->config()['theme'] ?? 'auto';
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* @return array<int, mixed>
|
|
|
71 |
*/
|
|
|
72 |
public function solutions(): array
|
|
|
73 |
{
|
|
|
74 |
return array_map(function (Solution $solution) {
|
|
|
75 |
/** @var class-string $transformerClass */
|
|
|
76 |
$transformerClass = $this->solutionTransformerClass;
|
|
|
77 |
|
|
|
78 |
/** @var SolutionTransformer $transformer */
|
|
|
79 |
$transformer = new $transformerClass($solution);
|
|
|
80 |
|
|
|
81 |
return ($transformer)->toArray();
|
|
|
82 |
}, $this->solutions);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* @return array<string, mixed>
|
|
|
87 |
*/
|
|
|
88 |
public function report(): array
|
|
|
89 |
{
|
|
|
90 |
return $this->report->toArray();
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
public function jsonEncode(mixed $data): string
|
|
|
94 |
{
|
|
|
95 |
$jsonOptions = JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
|
|
|
96 |
|
|
|
97 |
return (string)json_encode($data, $jsonOptions);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
public function getAssetContents(string $asset): string
|
|
|
101 |
{
|
|
|
102 |
$assetPath = __DIR__."/../../resources/compiled/{$asset}";
|
|
|
103 |
|
|
|
104 |
return (string)file_get_contents($assetPath);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
/**
|
|
|
108 |
* @return array<int|string, mixed>
|
|
|
109 |
*/
|
|
|
110 |
public function shareableReport(): array
|
|
|
111 |
{
|
|
|
112 |
return (new ReportTrimmer())->trim($this->report());
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
public function updateConfigEndpoint(): string
|
|
|
116 |
{
|
|
|
117 |
// TODO: Should be based on Ignition config
|
|
|
118 |
return '/_ignition/update-config';
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
public function customHtmlHead(): string
|
|
|
122 |
{
|
|
|
123 |
return $this->customHtmlHead;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
public function customHtmlBody(): string
|
|
|
127 |
{
|
|
|
128 |
return $this->customHtmlBody;
|
|
|
129 |
}
|
|
|
130 |
}
|