WebLinkExtension.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\Bridge\Twig\Extension;
  11. use Fig\Link\GenericLinkProvider;
  12. use Fig\Link\Link;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Twig\Extension\AbstractExtension;
  15. use Twig\TwigFunction;
  16. /**
  17. * Twig extension for the Symfony WebLink component.
  18. *
  19. * @author Kévin Dunglas <dunglas@gmail.com>
  20. */
  21. class WebLinkExtension extends AbstractExtension
  22. {
  23. private $requestStack;
  24. public function __construct(RequestStack $requestStack)
  25. {
  26. $this->requestStack = $requestStack;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function getFunctions()
  32. {
  33. return array(
  34. new TwigFunction('link', array($this, 'link')),
  35. new TwigFunction('preload', array($this, 'preload')),
  36. new TwigFunction('dns_prefetch', array($this, 'dnsPrefetch')),
  37. new TwigFunction('preconnect', array($this, 'preconnect')),
  38. new TwigFunction('prefetch', array($this, 'prefetch')),
  39. new TwigFunction('prerender', array($this, 'prerender')),
  40. );
  41. }
  42. /**
  43. * Adds a "Link" HTTP header.
  44. *
  45. * @param string $uri The relation URI
  46. * @param string $rel The relation type (e.g. "preload", "prefetch", "prerender" or "dns-prefetch")
  47. * @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)")
  48. *
  49. * @return string The relation URI
  50. */
  51. public function link($uri, $rel, array $attributes = array())
  52. {
  53. if (!$request = $this->requestStack->getMasterRequest()) {
  54. return $uri;
  55. }
  56. $link = new Link($rel, $uri);
  57. foreach ($attributes as $key => $value) {
  58. $link = $link->withAttribute($key, $value);
  59. }
  60. $linkProvider = $request->attributes->get('_links', new GenericLinkProvider());
  61. $request->attributes->set('_links', $linkProvider->withLink($link));
  62. return $uri;
  63. }
  64. /**
  65. * Preloads a resource.
  66. *
  67. * @param string $uri A public path
  68. * @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('crossorigin' => 'use-credentials')")
  69. *
  70. * @return string The path of the asset
  71. */
  72. public function preload($uri, array $attributes = array())
  73. {
  74. return $this->link($uri, 'preload', $attributes);
  75. }
  76. /**
  77. * Resolves a resource origin as early as possible.
  78. *
  79. * @param string $uri A public path
  80. * @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)")
  81. *
  82. * @return string The path of the asset
  83. */
  84. public function dnsPrefetch($uri, array $attributes = array())
  85. {
  86. return $this->link($uri, 'dns-prefetch', $attributes);
  87. }
  88. /**
  89. * Initiates a early connection to a resource (DNS resolution, TCP handshake, TLS negotiation).
  90. *
  91. * @param string $uri A public path
  92. * @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)")
  93. *
  94. * @return string The path of the asset
  95. */
  96. public function preconnect($uri, array $attributes = array())
  97. {
  98. return $this->link($uri, 'preconnect', $attributes);
  99. }
  100. /**
  101. * Indicates to the client that it should prefetch this resource.
  102. *
  103. * @param string $uri A public path
  104. * @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)")
  105. *
  106. * @return string The path of the asset
  107. */
  108. public function prefetch($uri, array $attributes = array())
  109. {
  110. return $this->link($uri, 'prefetch', $attributes);
  111. }
  112. /**
  113. * Indicates to the client that it should prerender this resource .
  114. *
  115. * @param string $uri A public path
  116. * @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)")
  117. *
  118. * @return string The path of the asset
  119. */
  120. public function prerender($uri, array $attributes = array())
  121. {
  122. return $this->link($uri, 'prerender', $attributes);
  123. }
  124. }