- md5
- 2a7e6be5af3fd0c016ebc87fb0ac430f
- sha1
- a97a82a6f39774b1efd6a4c7a15ab9536408f772
- sha256
- d6c61bca6562074a8a24d9d91f8eeb1938b03d15369aafae3a72791249b29c61
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace Joomla\CMS\WebAsset;
\defined('JPATH_PLATFORM') or die;
use Joomla\CMS\HTML\HTMLHelper;
/**
* Web Asset Item class
*
* @since 4.0.0
*/
class WebAssetItem implements WebAssetItemInterface
{
/**
* Asset name
*
* @var string $name
* @since 4.0.0
*/
protected $name;
/**
* Asset version
*
* @var string
* @since 4.0.0
*/
protected $version;
/**
* The Asset source info, where the asset comes from.
*
* @var array
* @since 4.0.0
*/
protected $assetSource;
/**
* Item weight
*
* @var float
*
* @since 4.0.0
*/
protected $weight = 0;
/**
* List of JavaScript files, and its attributes.
* The key is file path, the value is array of attributes.
*
* @var array
* @since 4.0.0
*/
protected $js = [];
/**
* List of StyleSheet files, and its attributes
* The key is file path, the value is array of attributes.
*
* @var array
* @since 4.0.0
*/
protected $css = [];
/**
* Asset dependencies
*
* @var string[]
* @since 4.0.0
*/
protected $dependencies = [];
/**
* Internal use, to keep track of resolved paths
*
* @var array
*
* @since 4.0.0
*/
protected $resolvedPaths = [];
/**
* Class constructor
*
* @param string $name The asset name
* @param array $data The Asset information
*
* @since 4.0.0
*/
public function __construct(string $name, array $data = [])
{
$this->name = $name;
$this->version = !empty($data['version']) ? $data['version'] : null;
$this->assetSource = !empty($data['assetSource']) ? $data['assetSource'] : null;
$attributes = empty($data['attribute']) ? [] : $data['attribute'];
// Check for Scripts and StyleSheets, and their attributes
if (!empty($data['js']))
{
foreach ($data['js'] as $js)
{
$this->js[$js] = empty($attributes[$js]) ? [] : $attributes[$js];
}
}
if (!empty($data['css']))
{
foreach ($data['css'] as $css)
{
$this->css[$css] = empty($attributes[$css]) ? [] : $attributes[$css];
}
}
if (!empty($data['dependencies']))
{
$this->dependencies = (array) $data['dependencies'];
}
if (!empty($data['weight']))
{
$this->weight = (float) $data['weight'];
}
}
/**
* Return Asset name
*
* @return string
*
* @since 4.0.0
*/
public function getName(): string
{
return $this->name;
}
/**
* Return Asset version
*
* @return string
*
* @since 4.0.0
*/
public function getVersion()
{
return $this->version;
}
/**
* Return dependencies list
*
* @return array
*
* @since 4.0.0
*/
public function getDependencies(): array
{
return $this->dependencies;
}
/**
* Set the desired weight for the Asset in Graph.
* Final weight will be calculated by AssetManager according to dependency Graph.
*
* @param float $weight The asset weight
*
* @return self
*
* @since 4.0.0
*/
public function setWeight(float $weight): WebAssetItemInterface
{
$this->weight = $weight;
return $this;
}
/**
* Return the weight of the Asset.
*
* @return float
*
* @since 4.0.0
*/
public function getWeight(): float
{
return $this->weight;
}
/**
* Get CSS files
*
* @param boolean $resolvePath Whether need to search for a real paths
*
* @return array
*
* @since 4.0.0
*/
public function getStylesheetFiles($resolvePath = true): array
{
if ($resolvePath)
{
$files = [];
foreach ($this->css as $path => $attr)
{
$resolved = $this->resolvePath($path, 'stylesheet');
$fullPath = $resolved['fullPath'];
if (!$fullPath)
{
// File not found, But we keep going ???
continue;
}
$files[$fullPath] = $attr;
$files[$fullPath]['__isExternal'] = $resolved['external'];
$files[$fullPath]['__pathOrigin'] = $path;
}
return $files;
}
return $this->css;
}
/**
* Get JS files
*
* @param boolean $resolvePath Whether we need to search for a real paths
*
* @return array
*
* @since 4.0.0
*/
public function getScriptFiles($resolvePath = true): array
{
if ($resolvePath)
{
$files = [];
foreach ($this->js as $path => $attr)
{
$resolved = $this->resolvePath($path, 'script');
$fullPath = $resolved['fullPath'];
if (!$fullPath)
{
// File not found, But we keep going ???
continue;
}
$files[$fullPath] = $attr;
$files[$fullPath]['__isExternal'] = $resolved['external'];
$files[$fullPath]['__pathOrigin'] = $path;
}
return $files;
}
return $this->js;
}
/**
* Resolve path
*
* @param string $path The path to resolve
* @param string $type The resolver method
*
* @return array
*
* @since 4.0.0
*/
protected function resolvePath(string $path, string $type): array
{
if (!empty($this->resolvedPaths[$path]))
{
return $this->resolvedPaths[$path];
}
if ($type !== 'script' && $type !== 'stylesheet')
{
throw new \UnexpectedValueException('Unexpected [type], expected "script" or "stylesheet"');
}
$file = $path;
$external = $this->isPathExternal($path);
if (!$external)
{
// Get the file path
$file = HTMLHelper::_(
$type,
$path,
[
'pathOnly' => true,
'relative' => !$this->isPathAbsolute($path)
]
);
}
$this->resolvedPaths[$path] = [
'external' => $external,
'fullPath' => $file ? $file : false,
];
return $this->resolvedPaths[$path];
}
/**
* Check if the Path is External
*
* @param string $path Path to test
*
* @return boolean
*
* @since 4.0.0
*/
protected function isPathExternal(string $path): bool
{
return strpos($path, 'http://') === 0 || strpos($path, 'https://') === 0 || strpos($path, '//') === 0;
}
/**
* Check if the Path is relative to /media folder or absolute
*
* @param string $path Path to test
*
* @return boolean
*
* @since 4.0.0
*/
protected function isPathAbsolute(string $path): bool
{
// We have a full path or not
return is_file(JPATH_ROOT . '/' . $path);
}
}
No other release has this exact content. See every version of this file.