Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
/**
4
 * Slim Framework (https://slimframework.com)
5
 *
6
 * @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
7
 */
8
 
9
declare(strict_types=1);
10
 
11
namespace Slim\Error\Renderers;
12
 
13
use Slim\Error\AbstractErrorRenderer;
14
use Throwable;
15
 
16
use function get_class;
17
use function sprintf;
18
use function str_replace;
19
 
20
/**
21
 * Default Slim application XML Error Renderer
22
 */
23
class XmlErrorRenderer extends AbstractErrorRenderer
24
{
25
    public function __invoke(Throwable $exception, bool $displayErrorDetails): string
26
    {
27
        $xml = '<' . '?xml version="1.0" encoding="UTF-8" standalone="yes"?' . ">\n";
28
        $xml .= "<error>\n  <message>" . $this->createCdataSection($this->getErrorTitle($exception)) . "</message>\n";
29
 
30
        if ($displayErrorDetails) {
31
            do {
32
                $xml .= "  <exception>\n";
33
                $xml .= '    <type>' . get_class($exception) . "</type>\n";
34
                $xml .= '    <code>' . $exception->getCode() . "</code>\n";
35
                $xml .= '    <message>' . $this->createCdataSection($exception->getMessage()) . "</message>\n";
36
                $xml .= '    <file>' . $exception->getFile() . "</file>\n";
37
                $xml .= '    <line>' . $exception->getLine() . "</line>\n";
38
                $xml .= "  </exception>\n";
39
            } while ($exception = $exception->getPrevious());
40
        }
41
 
42
        $xml .= '</error>';
43
 
44
        return $xml;
45
    }
46
 
47
    /**
48
     * Returns a CDATA section with the given content.
49
     */
50
    private function createCdataSection(string $content): string
51
    {
52
        return sprintf('<![CDATA[%s]]>', str_replace(']]>', ']]]]><![CDATA[>', $content));
53
    }
54
}