vendor/symfony/http-kernel/Fragment/AbstractSurrogateFragmentRenderer.php line 60

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\HttpKernel\Fragment;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Controller\ControllerReference;
  14. use Symfony\Component\HttpKernel\HttpCache\SurrogateInterface;
  15. use Symfony\Component\HttpKernel\UriSigner;
  16. /**
  17.  * Implements Surrogate rendering strategy.
  18.  *
  19.  * @author Fabien Potencier <fabien@symfony.com>
  20.  */
  21. abstract class AbstractSurrogateFragmentRenderer extends RoutableFragmentRenderer
  22. {
  23.     private $surrogate;
  24.     private $inlineStrategy;
  25.     private $signer;
  26.     /**
  27.      * The "fallback" strategy when surrogate is not available should always be an
  28.      * instance of InlineFragmentRenderer.
  29.      *
  30.      * @param FragmentRendererInterface $inlineStrategy The inline strategy to use when the surrogate is not supported
  31.      */
  32.     public function __construct(SurrogateInterface $surrogate nullFragmentRendererInterface $inlineStrategyUriSigner $signer null)
  33.     {
  34.         $this->surrogate $surrogate;
  35.         $this->inlineStrategy $inlineStrategy;
  36.         $this->signer $signer;
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      *
  41.      * Note that if the current Request has no surrogate capability, this method
  42.      * falls back to use the inline rendering strategy.
  43.      *
  44.      * Additional available options:
  45.      *
  46.      *  * alt: an alternative URI to render in case of an error
  47.      *  * comment: a comment to add when returning the surrogate tag
  48.      *
  49.      * Note, that not all surrogate strategies support all options. For now
  50.      * 'alt' and 'comment' are only supported by ESI.
  51.      *
  52.      * @see Symfony\Component\HttpKernel\HttpCache\SurrogateInterface
  53.      */
  54.     public function render($uriRequest $request, array $options = [])
  55.     {
  56.         if (!$this->surrogate || !$this->surrogate->hasSurrogateCapability($request)) {
  57.             if ($uri instanceof ControllerReference && $this->containsNonScalars($uri->attributes)) {
  58.                 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.');
  59.             }
  60.             return $this->inlineStrategy->render($uri$request$options);
  61.         }
  62.         if ($uri instanceof ControllerReference) {
  63.             $uri $this->generateSignedFragmentUri($uri$request);
  64.         }
  65.         $alt $options['alt'] ?? null;
  66.         if ($alt instanceof ControllerReference) {
  67.             $alt $this->generateSignedFragmentUri($alt$request);
  68.         }
  69.         $tag $this->surrogate->renderIncludeTag($uri$alt$options['ignore_errors'] ?? false$options['comment'] ?? '');
  70.         return new Response($tag);
  71.     }
  72.     private function generateSignedFragmentUri(ControllerReference $uriRequest $request): string
  73.     {
  74.         if (null === $this->signer) {
  75.             throw new \LogicException('You must use a URI when using the ESI rendering strategy or set a URL signer.');
  76.         }
  77.         // we need to sign the absolute URI, but want to return the path only.
  78.         $fragmentUri $this->signer->sign($this->generateFragmentUri($uri$requesttrue));
  79.         return substr($fragmentUri, \strlen($request->getSchemeAndHttpHost()));
  80.     }
  81.     private function containsNonScalars(array $values): bool
  82.     {
  83.         foreach ($values as $value) {
  84.             if (\is_array($value)) {
  85.                 return $this->containsNonScalars($value);
  86.             } elseif (!is_scalar($value) && null !== $value) {
  87.                 return true;
  88.             }
  89.         }
  90.         return false;
  91.     }
  92. }