EsiFragmentRenderer.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Esi;
  15. /**
  16. * Implements the ESI rendering strategy.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class EsiFragmentRenderer extends RoutableFragmentRenderer
  21. {
  22. private $esi;
  23. private $inlineStrategy;
  24. /**
  25. * Constructor.
  26. *
  27. * The "fallback" strategy when ESI is not available should always be an
  28. * instance of InlineFragmentRenderer.
  29. *
  30. * @param Esi $esi An Esi instance
  31. * @param InlineFragmentRenderer $inlineStrategy The inline strategy to use when ESI is not supported
  32. */
  33. public function __construct(Esi $esi, InlineFragmentRenderer $inlineStrategy)
  34. {
  35. $this->esi = $esi;
  36. $this->inlineStrategy = $inlineStrategy;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. *
  41. * Note that if the current Request has no ESI 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 an esi:include tag
  48. *
  49. * @see Symfony\Component\HttpKernel\HttpCache\ESI
  50. */
  51. public function render($uri, Request $request, array $options = array())
  52. {
  53. if (!$this->esi->hasSurrogateEsiCapability($request)) {
  54. return $this->inlineStrategy->render($uri, $request, $options);
  55. }
  56. if ($uri instanceof ControllerReference) {
  57. $uri = $this->generateFragmentUri($uri, $request);
  58. }
  59. $alt = isset($options['alt']) ? $options['alt'] : null;
  60. if ($alt instanceof ControllerReference) {
  61. $alt = $this->generateFragmentUri($alt, $request);
  62. }
  63. $tag = $this->esi->renderIncludeTag($uri, $alt, isset($options['ignore_errors']) ? $options['ignore_errors'] : false, isset($options['comment']) ? $options['comment'] : '');
  64. return new Response($tag);
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getName()
  70. {
  71. return 'esi';
  72. }
  73. }