菜谱项目

AbstractSurrogateFragmentRenderer.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 SurrogateInterface $surrogate An Surrogate instance
  31. * @param FragmentRendererInterface $inlineStrategy The inline strategy to use when the surrogate is not supported
  32. * @param UriSigner $signer
  33. */
  34. public function __construct(SurrogateInterface $surrogate = null, 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 = array())
  57. {
  58. if (!$this->surrogate || !$this->surrogate->hasSurrogateCapability($request)) {
  59. if ($uri instanceof ControllerReference && $this->containsNonScalars($uri->attributes)) {
  60. @trigger_error('Passing non-scalar values as part of URI attributes to the ESI and SSI rendering strategies is deprecated since version 3.1, and will be removed in 4.0. Use a different rendering strategy or pass scalar values.', E_USER_DEPRECATED);
  61. }
  62. return $this->inlineStrategy->render($uri, $request, $options);
  63. }
  64. if ($uri instanceof ControllerReference) {
  65. $uri = $this->generateSignedFragmentUri($uri, $request);
  66. }
  67. $alt = isset($options['alt']) ? $options['alt'] : null;
  68. if ($alt instanceof ControllerReference) {
  69. $alt = $this->generateSignedFragmentUri($alt, $request);
  70. }
  71. $tag = $this->surrogate->renderIncludeTag($uri, $alt, isset($options['ignore_errors']) ? $options['ignore_errors'] : false, isset($options['comment']) ? $options['comment'] : '');
  72. return new Response($tag);
  73. }
  74. private function generateSignedFragmentUri($uri, Request $request)
  75. {
  76. if (null === $this->signer) {
  77. throw new \LogicException('You must use a URI when using the ESI rendering strategy or set a URL signer.');
  78. }
  79. // we need to sign the absolute URI, but want to return the path only.
  80. $fragmentUri = $this->signer->sign($this->generateFragmentUri($uri, $request, true));
  81. return substr($fragmentUri, strlen($request->getSchemeAndHttpHost()));
  82. }
  83. private function containsNonScalars(array $values)
  84. {
  85. foreach ($values as $value) {
  86. if (is_array($value) && $this->containsNonScalars($value)) {
  87. return true;
  88. } elseif (!is_scalar($value) && null !== $value) {
  89. return true;
  90. }
  91. }
  92. return false;
  93. }
  94. }