[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/Extension/ -> WebLinkExtension.php (source)

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


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1