[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/WebAsset/ -> WebAssetItem.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2018 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\WebAsset;
  11  
  12  use Joomla\CMS\HTML\HTMLHelper;
  13  
  14  // phpcs:disable PSR1.Files.SideEffects
  15  \defined('JPATH_PLATFORM') or die;
  16  // phpcs:enable PSR1.Files.SideEffects
  17  
  18  /**
  19   * Web Asset Item class
  20   *
  21   * Asset Item are "read only" object, all properties must be set through class constructor.
  22   * Only properties allowed to be edited is an attributes and an options.
  23   * Changing an uri or a dependencies are not allowed, prefer to create a new asset instance.
  24   *
  25   * @since  4.0.0
  26   */
  27  class WebAssetItem implements WebAssetItemInterface
  28  {
  29      /**
  30       * Asset name
  31       *
  32       * @var    string  $name
  33       * @since  4.0.0
  34       */
  35      protected $name = '';
  36  
  37      /**
  38       * The URI for the asset
  39       *
  40       * @var    string
  41       * @since  4.0.0
  42       */
  43      protected $uri = '';
  44  
  45      /**
  46       * Additional options for the asset
  47       *
  48       * @var    array
  49       * @since  4.0.0
  50       */
  51      protected $options = [];
  52  
  53      /**
  54       * Attributes for the asset, to be rendered in the asset's HTML tag
  55       *
  56       * @var    array
  57       * @since  4.0.0
  58       */
  59      protected $attributes = [];
  60  
  61      /**
  62       * Asset dependencies
  63       *
  64       * @var    string[]
  65       * @since  4.0.0
  66       */
  67      protected $dependencies = [];
  68  
  69      /**
  70       * Asset version
  71       *
  72       * @var    string
  73       * @since  4.0.0
  74       */
  75      protected $version = 'auto';
  76  
  77      /**
  78       * Class constructor
  79       *
  80       * @param   string  $name          The asset name
  81       * @param   string  $uri           The URI for the asset
  82       * @param   array   $options       Additional options for the asset
  83       * @param   array   $attributes    Attributes for the asset
  84       * @param   array   $dependencies  Asset dependencies
  85       *
  86       * @since   4.0.0
  87       */
  88      public function __construct(
  89          string $name,
  90          string $uri = null,
  91          array $options = [],
  92          array $attributes = [],
  93          array $dependencies = []
  94      ) {
  95          $this->name    = $name;
  96          $this->uri     = $uri;
  97  
  98          if (array_key_exists('version', $options)) {
  99              $this->version = $options['version'];
 100              unset($options['version']);
 101          }
 102  
 103          if (array_key_exists('attributes', $options)) {
 104              $this->attributes = (array) $options['attributes'];
 105              unset($options['attributes']);
 106          } else {
 107              $this->attributes = $attributes;
 108          }
 109  
 110          if (array_key_exists('dependencies', $options)) {
 111              $this->dependencies = (array) $options['dependencies'];
 112              unset($options['dependencies']);
 113          } else {
 114              $this->dependencies = $dependencies;
 115          }
 116  
 117          $this->options = $options;
 118      }
 119  
 120      /**
 121       * Return Asset name
 122       *
 123       * @return  string
 124       *
 125       * @since   4.0.0
 126       */
 127      public function getName(): string
 128      {
 129          return $this->name;
 130      }
 131  
 132      /**
 133       * Return Asset version
 134       *
 135       * @return  string
 136       *
 137       * @since   4.0.0
 138       */
 139      public function getVersion(): string
 140      {
 141          return (string) $this->version;
 142      }
 143  
 144      /**
 145       * Return dependencies list
 146       *
 147       * @return  array
 148       *
 149       * @since   4.0.0
 150       */
 151      public function getDependencies(): array
 152      {
 153          return $this->dependencies;
 154      }
 155  
 156      /**
 157       * Get the file path
 158       *
 159       * @param   boolean  $resolvePath  Whether need to search for a real paths
 160       *
 161       * @return  string  The resolved path if resolved, else an empty string.
 162       *
 163       * @since   4.0.0
 164       */
 165      public function getUri($resolvePath = true): string
 166      {
 167          $path = $this->uri;
 168  
 169          if ($resolvePath && $path) {
 170              switch (pathinfo($path, PATHINFO_EXTENSION)) {
 171                  case 'js':
 172                      $path = $this->resolvePath($path, 'script');
 173                      break;
 174                  case 'css':
 175                      $path = $this->resolvePath($path, 'stylesheet');
 176                      break;
 177                  default:
 178                      break;
 179              }
 180          }
 181  
 182          return $path ?? '';
 183      }
 184  
 185      /**
 186       * Get the option
 187       *
 188       * @param   string  $key      An option key
 189       * @param   string  $default  A default value
 190       *
 191       * @return mixed
 192       *
 193       * @since   4.0.0
 194       */
 195      public function getOption(string $key, $default = null)
 196      {
 197          if (array_key_exists($key, $this->options)) {
 198              return $this->options[$key];
 199          }
 200  
 201          return $default;
 202      }
 203  
 204      /**
 205       * Set the option
 206       *
 207       * @param   string  $key    An option key
 208       * @param   string  $value  An option value
 209       *
 210       * @return self
 211       *
 212       * @since   4.0.0
 213       */
 214      public function setOption(string $key, $value = null): WebAssetItemInterface
 215      {
 216          $this->options[$key] = $value;
 217  
 218          return $this;
 219      }
 220  
 221      /**
 222       * Get all options
 223       *
 224       * @return array
 225       *
 226       * @since   4.0.0
 227       */
 228      public function getOptions(): array
 229      {
 230          return $this->options;
 231      }
 232  
 233      /**
 234       * Get the attribute
 235       *
 236       * @param   string  $key      An attributes key
 237       * @param   string  $default  A default value
 238       *
 239       * @return mixed
 240       *
 241       * @since   4.0.0
 242       */
 243      public function getAttribute(string $key, $default = null)
 244      {
 245          if (array_key_exists($key, $this->attributes)) {
 246              return $this->attributes[$key];
 247          }
 248  
 249          return $default;
 250      }
 251  
 252      /**
 253       * Set the attribute
 254       *
 255       * @param   string  $key    An attribute key
 256       * @param   string  $value  An attribute value
 257       *
 258       * @return self
 259       *
 260       * @since   4.0.0
 261       */
 262      public function setAttribute(string $key, $value = null): WebAssetItemInterface
 263      {
 264          $this->attributes[$key] = $value;
 265  
 266          return $this;
 267      }
 268  
 269      /**
 270       * Get all attributes
 271       *
 272       * @return array
 273       *
 274       * @since   4.0.0
 275       */
 276      public function getAttributes(): array
 277      {
 278          return $this->attributes;
 279      }
 280  
 281      /**
 282       * Resolve path
 283       *
 284       * @param   string  $path  The path to resolve
 285       * @param   string  $type  The resolver method
 286       *
 287       * @return string
 288       *
 289       * @since  4.0.0
 290       */
 291      protected function resolvePath(string $path, string $type): string
 292      {
 293          if ($type !== 'script' && $type !== 'stylesheet') {
 294              throw new \UnexpectedValueException('Unexpected [type], expected "script" or "stylesheet"');
 295          }
 296  
 297          $file     = $path;
 298          $external = $this->isPathExternal($path);
 299  
 300          if (!$external) {
 301              // Get the file path
 302              $file = HTMLHelper::_(
 303                  $type,
 304                  $path,
 305                  [
 306                      'pathOnly' => true,
 307                      'relative' => !$this->isPathAbsolute($path),
 308                  ]
 309              );
 310          }
 311  
 312          return $file ?? '';
 313      }
 314  
 315      /**
 316       * Check if the Path is External
 317       *
 318       * @param   string  $path  Path to test
 319       *
 320       * @return  boolean
 321       *
 322       * @since   4.0.0
 323       */
 324      protected function isPathExternal(string $path): bool
 325      {
 326          return strpos($path, 'http://') === 0 || strpos($path, 'https://') === 0 || strpos($path, '//') === 0;
 327      }
 328  
 329      /**
 330       * Check if the Path is relative to /media folder or absolute
 331       *
 332       * @param   string  $path  Path to test
 333       *
 334       * @return  boolean
 335       *
 336       * @since   4.0.0
 337       */
 338      protected function isPathAbsolute(string $path): bool
 339      {
 340          // We have a full path or not
 341          return is_file(JPATH_ROOT . '/' . $path);
 342      }
 343  }


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