Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\ErrorHandler\ErrorRenderer;
13
 
14
use Symfony\Component\ErrorHandler\Exception\FlattenException;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\RequestStack;
17
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
18
use Symfony\Component\Serializer\SerializerInterface;
19
 
20
/**
21
 * Formats an exception using Serializer for rendering.
22
 *
23
 * @author Nicolas Grekas <p@tchwork.com>
24
 */
25
class SerializerErrorRenderer implements ErrorRendererInterface
26
{
27
    private SerializerInterface $serializer;
28
    private string|\Closure $format;
29
    private ErrorRendererInterface $fallbackErrorRenderer;
30
    private bool|\Closure $debug;
31
 
32
    /**
33
     * @param string|callable(FlattenException) $format The format as a string or a callable that should return it
34
     *                                                  formats not supported by Request::getMimeTypes() should be given as mime types
35
     * @param bool|callable                     $debug  The debugging mode as a boolean or a callable that should return it
36
     */
37
    public function __construct(SerializerInterface $serializer, string|callable $format, ErrorRendererInterface $fallbackErrorRenderer = null, bool|callable $debug = false)
38
    {
39
        $this->serializer = $serializer;
40
        $this->format = \is_string($format) ? $format : $format(...);
41
        $this->fallbackErrorRenderer = $fallbackErrorRenderer ?? new HtmlErrorRenderer();
42
        $this->debug = \is_bool($debug) ? $debug : $debug(...);
43
    }
44
 
45
    public function render(\Throwable $exception): FlattenException
46
    {
1464 lars 47
        $headers = ['Vary' => 'Accept'];
148 lars 48
        $debug = \is_bool($this->debug) ? $this->debug : ($this->debug)($exception);
49
        if ($debug) {
50
            $headers['X-Debug-Exception'] = rawurlencode($exception->getMessage());
51
            $headers['X-Debug-Exception-File'] = rawurlencode($exception->getFile()).':'.$exception->getLine();
52
        }
53
 
54
        $flattenException = FlattenException::createFromThrowable($exception, null, $headers);
55
 
56
        try {
57
            $format = \is_string($this->format) ? $this->format : ($this->format)($flattenException);
1464 lars 58
            $headers['Content-Type'] = Request::getMimeTypes($format)[0] ?? $format;
148 lars 59
 
1464 lars 60
            $flattenException->setAsString($this->serializer->serialize($flattenException, $format, [
148 lars 61
                'exception' => $exception,
62
                'debug' => $debug,
1464 lars 63
            ]));
148 lars 64
        } catch (NotEncodableValueException) {
1464 lars 65
            $flattenException = $this->fallbackErrorRenderer->render($exception);
148 lars 66
        }
1464 lars 67
 
68
        return $flattenException->setHeaders($flattenException->getHeaders() + $headers);
148 lars 69
    }
70
 
71
    public static function getPreferredFormat(RequestStack $requestStack): \Closure
72
    {
73
        return static function () use ($requestStack) {
74
            if (!$request = $requestStack->getCurrentRequest()) {
75
                throw new NotEncodableValueException();
76
            }
77
 
78
            return $request->getPreferredFormat();
79
        };
80
    }
81
}