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