|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\ErrorHandler\Renderer; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
8
|
|
|
use Throwable; |
|
9
|
|
|
use Yiisoft\ErrorHandler\ErrorData; |
|
10
|
|
|
use Yiisoft\ErrorHandler\ThrowableRendererInterface; |
|
11
|
|
|
|
|
12
|
|
|
use function str_replace; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Formats throwable into XML string. |
|
16
|
|
|
*/ |
|
17
|
|
|
final class XmlRenderer implements ThrowableRendererInterface |
|
18
|
|
|
{ |
|
19
|
1 |
|
public function render(Throwable $t, ServerRequestInterface $request = null): ErrorData |
|
20
|
|
|
{ |
|
21
|
1 |
|
$content = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'; |
|
22
|
1 |
|
$content .= "\n<error>\n"; |
|
23
|
1 |
|
$content .= $this->tag('message', self::DEFAULT_ERROR_MESSAGE); |
|
24
|
1 |
|
$content .= '</error>'; |
|
25
|
1 |
|
return new ErrorData($content); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
1 |
|
public function renderVerbose(Throwable $t, ServerRequestInterface $request = null): ErrorData |
|
29
|
|
|
{ |
|
30
|
1 |
|
$content = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'; |
|
31
|
1 |
|
$content .= "\n<error>\n"; |
|
32
|
1 |
|
$content .= $this->tag('type', $t::class); |
|
33
|
1 |
|
$content .= $this->tag('message', $this->cdata($t->getMessage())); |
|
34
|
1 |
|
$content .= $this->tag('code', $this->cdata((string) $t->getCode())); |
|
35
|
1 |
|
$content .= $this->tag('file', $t->getFile()); |
|
36
|
1 |
|
$content .= $this->tag('line', (string) $t->getLine()); |
|
37
|
1 |
|
$content .= $this->tag('trace', $t->getTraceAsString()); |
|
38
|
1 |
|
$content .= '</error>'; |
|
39
|
1 |
|
return new ErrorData($content); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
2 |
|
private function tag(string $name, string $value): string |
|
43
|
|
|
{ |
|
44
|
2 |
|
return "<$name>" . $value . "</$name>\n"; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
private function cdata(string $value): string |
|
48
|
|
|
{ |
|
49
|
1 |
|
return '<![CDATA[' . str_replace(']]>', ']]]]><![CDATA[>', $value) . ']]>'; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|