Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | 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\HttpKernel\Fragment;
13
 
14
use Symfony\Component\HttpFoundation\RequestStack;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\HttpFoundation\StreamedResponse;
17
use Symfony\Component\HttpKernel\Controller\ControllerReference;
18
use Symfony\Component\HttpKernel\Exception\HttpException;
19
 
20
/**
21
 * Renders a URI that represents a resource fragment.
22
 *
23
 * This class handles the rendering of resource fragments that are included into
24
 * a main resource. The handling of the rendering is managed by specialized renderers.
25
 *
26
 * @author Fabien Potencier <fabien@symfony.com>
27
 *
28
 * @see FragmentRendererInterface
29
 */
30
class FragmentHandler
31
{
32
    private bool $debug;
33
    private array $renderers = [];
34
    private RequestStack $requestStack;
35
 
36
    /**
37
     * @param FragmentRendererInterface[] $renderers An array of FragmentRendererInterface instances
38
     * @param bool                        $debug     Whether the debug mode is enabled or not
39
     */
40
    public function __construct(RequestStack $requestStack, array $renderers = [], bool $debug = false)
41
    {
42
        $this->requestStack = $requestStack;
43
        foreach ($renderers as $renderer) {
44
            $this->addRenderer($renderer);
45
        }
46
        $this->debug = $debug;
47
    }
48
 
49
    /**
50
     * Adds a renderer.
51
     */
52
    public function addRenderer(FragmentRendererInterface $renderer)
53
    {
54
        $this->renderers[$renderer->getName()] = $renderer;
55
    }
56
 
57
    /**
58
     * Renders a URI and returns the Response content.
59
     *
60
     * Available options:
61
     *
62
     *  * ignore_errors: true to return an empty string in case of an error
63
     *
64
     * @throws \InvalidArgumentException when the renderer does not exist
65
     * @throws \LogicException           when no main request is being handled
66
     */
67
    public function render(string|ControllerReference $uri, string $renderer = 'inline', array $options = []): ?string
68
    {
69
        if (!isset($options['ignore_errors'])) {
70
            $options['ignore_errors'] = !$this->debug;
71
        }
72
 
73
        if (!isset($this->renderers[$renderer])) {
74
            throw new \InvalidArgumentException(sprintf('The "%s" renderer does not exist.', $renderer));
75
        }
76
 
77
        if (!$request = $this->requestStack->getCurrentRequest()) {
78
            throw new \LogicException('Rendering a fragment can only be done when handling a Request.');
79
        }
80
 
81
        return $this->deliver($this->renderers[$renderer]->render($uri, $request, $options));
82
    }
83
 
84
    /**
85
     * Delivers the Response as a string.
86
     *
87
     * When the Response is a StreamedResponse, the content is streamed immediately
88
     * instead of being returned.
89
     *
90
     * @return string|null The Response content or null when the Response is streamed
91
     *
92
     * @throws \RuntimeException when the Response is not successful
93
     */
94
    protected function deliver(Response $response): ?string
95
    {
96
        if (!$response->isSuccessful()) {
97
            $responseStatusCode = $response->getStatusCode();
98
            throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %d).', $this->requestStack->getCurrentRequest()->getUri(), $responseStatusCode), 0, new HttpException($responseStatusCode));
99
        }
100
 
101
        if (!$response instanceof StreamedResponse) {
102
            return $response->getContent();
103
        }
104
 
105
        $response->sendContent();
106
 
107
        return null;
108
    }
109
}