| 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\Request;
|
|
|
15 |
use Symfony\Component\HttpFoundation\Response;
|
|
|
16 |
use Symfony\Component\HttpKernel\Controller\ControllerReference;
|
|
|
17 |
use Symfony\Component\HttpKernel\HttpCache\SurrogateInterface;
|
|
|
18 |
use Symfony\Component\HttpKernel\UriSigner;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Implements Surrogate rendering strategy.
|
|
|
22 |
*
|
|
|
23 |
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
24 |
*/
|
|
|
25 |
abstract class AbstractSurrogateFragmentRenderer extends RoutableFragmentRenderer
|
|
|
26 |
{
|
|
|
27 |
private ?SurrogateInterface $surrogate;
|
|
|
28 |
private FragmentRendererInterface $inlineStrategy;
|
|
|
29 |
private ?UriSigner $signer;
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* The "fallback" strategy when surrogate is not available should always be an
|
|
|
33 |
* instance of InlineFragmentRenderer.
|
|
|
34 |
*
|
|
|
35 |
* @param FragmentRendererInterface $inlineStrategy The inline strategy to use when the surrogate is not supported
|
|
|
36 |
*/
|
|
|
37 |
public function __construct(SurrogateInterface $surrogate = null, FragmentRendererInterface $inlineStrategy, UriSigner $signer = null)
|
|
|
38 |
{
|
|
|
39 |
$this->surrogate = $surrogate;
|
|
|
40 |
$this->inlineStrategy = $inlineStrategy;
|
|
|
41 |
$this->signer = $signer;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Note that if the current Request has no surrogate capability, this method
|
|
|
46 |
* falls back to use the inline rendering strategy.
|
|
|
47 |
*
|
|
|
48 |
* Additional available options:
|
|
|
49 |
*
|
|
|
50 |
* * alt: an alternative URI to render in case of an error
|
|
|
51 |
* * comment: a comment to add when returning the surrogate tag
|
|
|
52 |
* * absolute_uri: whether to generate an absolute URI or not. Default is false
|
|
|
53 |
*
|
|
|
54 |
* Note, that not all surrogate strategies support all options. For now
|
|
|
55 |
* 'alt' and 'comment' are only supported by ESI.
|
|
|
56 |
*
|
|
|
57 |
* @see Symfony\Component\HttpKernel\HttpCache\SurrogateInterface
|
|
|
58 |
*/
|
|
|
59 |
public function render(string|ControllerReference $uri, Request $request, array $options = []): Response
|
|
|
60 |
{
|
|
|
61 |
if (!$this->surrogate || !$this->surrogate->hasSurrogateCapability($request)) {
|
|
|
62 |
if ($uri instanceof ControllerReference && $this->containsNonScalars($uri->attributes)) {
|
|
|
63 |
throw new \InvalidArgumentException('Passing non-scalar values as part of URI attributes to the ESI and SSI rendering strategies is not supported. Use a different rendering strategy or pass scalar values.');
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
return $this->inlineStrategy->render($uri, $request, $options);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
$absolute = $options['absolute_uri'] ?? false;
|
|
|
70 |
|
|
|
71 |
if ($uri instanceof ControllerReference) {
|
|
|
72 |
$uri = $this->generateSignedFragmentUri($uri, $request, $absolute);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
$alt = $options['alt'] ?? null;
|
|
|
76 |
if ($alt instanceof ControllerReference) {
|
|
|
77 |
$alt = $this->generateSignedFragmentUri($alt, $request, $absolute);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$tag = $this->surrogate->renderIncludeTag($uri, $alt, $options['ignore_errors'] ?? false, $options['comment'] ?? '');
|
|
|
81 |
|
|
|
82 |
return new Response($tag);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
private function generateSignedFragmentUri(ControllerReference $uri, Request $request, bool $absolute): string
|
|
|
86 |
{
|
|
|
87 |
return (new FragmentUriGenerator($this->fragmentPath, $this->signer))->generate($uri, $request, $absolute);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
private function containsNonScalars(array $values): bool
|
|
|
91 |
{
|
|
|
92 |
foreach ($values as $value) {
|
|
|
93 |
if (\is_scalar($value) || null === $value) {
|
|
|
94 |
continue;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
if (!\is_array($value) || $this->containsNonScalars($value)) {
|
|
|
98 |
return true;
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
return false;
|
|
|
103 |
}
|
|
|
104 |
}
|