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\HttpCache;
13
 
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
 
17
/**
18
 * Esi implements the ESI capabilities to Request and Response instances.
19
 *
20
 * For more information, read the following W3C notes:
21
 *
22
 *  * ESI Language Specification 1.0 (http://www.w3.org/TR/esi-lang)
23
 *
24
 *  * Edge Architecture Specification (http://www.w3.org/TR/edge-arch)
25
 *
26
 * @author Fabien Potencier <fabien@symfony.com>
27
 */
28
class Esi extends AbstractSurrogate
29
{
30
    public function getName(): string
31
    {
32
        return 'esi';
33
    }
34
 
35
    public function addSurrogateControl(Response $response)
36
    {
37
        if (str_contains($response->getContent(), '<esi:include')) {
38
            $response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
39
        }
40
    }
41
 
42
    public function renderIncludeTag(string $uri, string $alt = null, bool $ignoreErrors = true, string $comment = ''): string
43
    {
44
        $html = sprintf('<esi:include src="%s"%s%s />',
45
            $uri,
46
            $ignoreErrors ? ' onerror="continue"' : '',
47
            $alt ? sprintf(' alt="%s"', $alt) : ''
48
        );
49
 
50
        if (!empty($comment)) {
51
            return sprintf("<esi:comment text=\"%s\" />\n%s", $comment, $html);
52
        }
53
 
54
        return $html;
55
    }
56
 
57
    public function process(Request $request, Response $response): Response
58
    {
59
        $type = $response->headers->get('Content-Type');
60
        if (empty($type)) {
61
            $type = 'text/html';
62
        }
63
 
64
        $parts = explode(';', $type);
65
        if (!\in_array($parts[0], $this->contentTypes)) {
66
            return $response;
67
        }
68
 
69
        // we don't use a proper XML parser here as we can have ESI tags in a plain text response
70
        $content = $response->getContent();
71
        $content = preg_replace('#<esi\:remove>.*?</esi\:remove>#s', '', $content);
72
        $content = preg_replace('#<esi\:comment[^>]+>#s', '', $content);
73
 
74
        $chunks = preg_split('#<esi\:include\s+(.*?)\s*(?:/|</esi\:include)>#', $content, -1, \PREG_SPLIT_DELIM_CAPTURE);
75
        $chunks[0] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[0]);
76
 
77
        $i = 1;
78
        while (isset($chunks[$i])) {
79
            $options = [];
80
            preg_match_all('/(src|onerror|alt)="([^"]*?)"/', $chunks[$i], $matches, \PREG_SET_ORDER);
81
            foreach ($matches as $set) {
82
                $options[$set[1]] = $set[2];
83
            }
84
 
85
            if (!isset($options['src'])) {
86
                throw new \RuntimeException('Unable to process an ESI tag without a "src" attribute.');
87
            }
88
 
89
            $chunks[$i] = sprintf('<?php echo $this->surrogate->handle($this, %s, %s, %s) ?>'."\n",
90
                var_export($options['src'], true),
91
                var_export($options['alt'] ?? '', true),
92
                isset($options['onerror']) && 'continue' === $options['onerror'] ? 'true' : 'false'
93
            );
94
            ++$i;
95
            $chunks[$i] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[$i]);
96
            ++$i;
97
        }
98
        $content = implode('', $chunks);
99
 
100
        $response->setContent($content);
101
        $response->headers->set('X-Body-Eval', 'ESI');
102
 
103
        // remove ESI/1.0 from the Surrogate-Control header
104
        $this->removeFromControl($response);
105
 
106
        return $response;
107
    }
108
}