[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Document/ -> PreloadManager.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Document;
  11  
  12  use Fig\Link\GenericLinkProvider;
  13  use Fig\Link\Link;
  14  use Psr\Link\EvolvableLinkProviderInterface;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('_JEXEC') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Joomla! Preload Manager
  22   *
  23   * @since  4.0.0
  24   */
  25  class PreloadManager implements PreloadManagerInterface
  26  {
  27      /**
  28       * The link provider
  29       *
  30       * @var    EvolvableLinkProviderInterface
  31       * @since  4.0.0
  32       */
  33      protected $linkProvider;
  34  
  35      /**
  36       * PreloadManager constructor
  37       *
  38       * @param   EvolvableLinkProviderInterface  $linkProvider  The link provider
  39       *
  40       * @since   4.0.0
  41       */
  42      public function __construct(EvolvableLinkProviderInterface $linkProvider = null)
  43      {
  44          $this->linkProvider = $linkProvider ?: new GenericLinkProvider();
  45      }
  46  
  47      /**
  48       * Get the link provider
  49       *
  50       * @return  EvolvableLinkProviderInterface
  51       *
  52       * @since   4.0.0
  53       */
  54      public function getLinkProvider(): EvolvableLinkProviderInterface
  55      {
  56          return $this->linkProvider;
  57      }
  58  
  59      /**
  60       * Set the link provider
  61       *
  62       * @param   EvolvableLinkProviderInterface  $linkProvider  The link provider
  63       *
  64       * @return  $this
  65       *
  66       * @since   4.0.0
  67       */
  68      public function setLinkProvider(EvolvableLinkProviderInterface $linkProvider)
  69      {
  70          $this->linkProvider = $linkProvider;
  71  
  72          return $this;
  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. "array('as' => true)", "array('crossorigin' => 'use-credentials')")
  80       *
  81       * @return  void
  82       *
  83       * @since   4.0.0
  84       */
  85      public function preload(string $uri, array $attributes = [])
  86      {
  87          $this->link($uri, 'preload', $attributes);
  88      }
  89  
  90      /**
  91       * Resolves a resource origin as early as possible.
  92       *
  93       * @param   string  $uri         A public path
  94       * @param   array   $attributes  The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)")
  95       *
  96       * @return  void
  97       *
  98       * @since   4.0.0
  99       */
 100      public function dnsPrefetch(string $uri, array $attributes = [])
 101      {
 102          $this->link($uri, 'dns-prefetch', $attributes);
 103      }
 104  
 105      /**
 106       * Initiates an early connection to a resource (DNS resolution, TCP handshake, TLS negotiation).
 107       *
 108       * @param   string  $uri         A public path
 109       * @param   array   $attributes  The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)")
 110       *
 111       * @return  void
 112       *
 113       * @since   4.0.0
 114       */
 115      public function preconnect(string $uri, array $attributes = [])
 116      {
 117          $this->link($uri, 'preconnect', $attributes);
 118      }
 119  
 120      /**
 121       * Indicates to the client that it should prefetch this resource.
 122       *
 123       * @param   string  $uri         A public path
 124       * @param   array   $attributes  The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)")
 125       *
 126       * @return  void
 127       *
 128       * @since   4.0.0
 129       */
 130      public function prefetch(string $uri, array $attributes = [])
 131      {
 132          $this->link($uri, 'prefetch', $attributes);
 133      }
 134  
 135      /**
 136       * Indicates to the client that it should prerender this resource.
 137       *
 138       * @param   string  $uri         A public path
 139       * @param   array   $attributes  The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)")
 140       *
 141       * @return  void
 142       *
 143       * @since   4.0.0
 144       */
 145      public function prerender(string $uri, array $attributes = [])
 146      {
 147          $this->link($uri, 'prerender', $attributes);
 148      }
 149  
 150      /**
 151       * Adds a "Link" HTTP header.
 152       *
 153       * @param   string  $uri         The relation URI
 154       * @param   string  $rel         The relation type (e.g. "preload", "prefetch", "prerender" or "dns-prefetch")
 155       * @param   array   $attributes  The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)")
 156       *
 157       * @return  void
 158       *
 159       * @since   4.0.0
 160       */
 161      private function link(string $uri, string $rel, array $attributes = [])
 162      {
 163          $link = new Link($rel, $uri);
 164  
 165          foreach ($attributes as $key => $value) {
 166              $link = $link->withAttribute($key, $value);
 167          }
 168  
 169          $this->setLinkProvider($this->getLinkProvider()->withLink($link));
 170      }
 171  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer