Drupal investigation

AbstractSurrogateFragmentRenderer.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. * Constructor.
  28. *
  29. * The "fallback" strategy when surrogate is not available should always be an
  30. * instance of InlineFragmentRenderer.
  31. *
  32. * @param SurrogateInterface $surrogate An Surrogate instance
  33. * @param FragmentRendererInterface $inlineStrategy The inline strategy to use when the surrogate is not supported
  34. * @param UriSigner $signer
  35. */
  36. public function __construct(SurrogateInterface $surrogate = null, FragmentRendererInterface $inlineStrategy, UriSigner $signer = null)
  37. {
  38. $this->surrogate = $surrogate;
  39. $this->inlineStrategy = $inlineStrategy;
  40. $this->signer = $signer;
  41. }
  42. /**
  43. * {@inheritdoc}
  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. *
  53. * Note, that not all surrogate strategies support all options. For now
  54. * 'alt' and 'comment' are only supported by ESI.
  55. *
  56. * @see Symfony\Component\HttpKernel\HttpCache\SurrogateInterface
  57. */
  58. public function render($uri, Request $request, array $options = array())
  59. {
  60. if (!$this->surrogate || !$this->surrogate->hasSurrogateCapability($request)) {
  61. return $this->inlineStrategy->render($uri, $request, $options);
  62. }
  63. if ($uri instanceof ControllerReference) {
  64. $uri = $this->generateSignedFragmentUri($uri, $request);
  65. }
  66. $alt = isset($options['alt']) ? $options['alt'] : null;
  67. if ($alt instanceof ControllerReference) {
  68. $alt = $this->generateSignedFragmentUri($alt, $request);
  69. }
  70. $tag = $this->surrogate->renderIncludeTag($uri, $alt, isset($options['ignore_errors']) ? $options['ignore_errors'] : false, isset($options['comment']) ? $options['comment'] : '');
  71. return new Response($tag);
  72. }
  73. private function generateSignedFragmentUri($uri, Request $request)
  74. {
  75. if (null === $this->signer) {
  76. throw new \LogicException('You must use a URI when using the ESI rendering strategy or set a URL signer.');
  77. }
  78. // we need to sign the absolute URI, but want to return the path only.
  79. $fragmentUri = $this->signer->sign($this->generateFragmentUri($uri, $request, true));
  80. return substr($fragmentUri, strlen($request->getSchemeAndHttpHost()));
  81. }
  82. }