AbstractSurrogateFragmentRenderer.php 3.6 KB

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