diff --git a/libraries/src/WebAsset/WebAssetItem.php b/libraries/src/WebAsset/WebAssetItem.php index d563ed8201675..69a8a85b44906 100644 --- a/libraries/src/WebAsset/WebAssetItem.php +++ b/libraries/src/WebAsset/WebAssetItem.php @@ -105,7 +105,7 @@ class WebAssetItem * @var array * @since __DEPLOY_VERSION__ */ - protected $js = array(); + protected $js = []; /** * List of StyleSheet files, ant it's attributes @@ -114,7 +114,7 @@ class WebAssetItem * @var array * @since __DEPLOY_VERSION__ */ - protected $css = array(); + protected $css = []; /** * Asset dependencies @@ -122,7 +122,16 @@ class WebAssetItem * @var string[] * @since __DEPLOY_VERSION__ */ - protected $dependencies = array(); + protected $dependencies = []; + + /** + * Internal use, to keep track of resolved paths + * + * @var array + * + * @since __DEPLOY_VERSION__ + */ + protected $resolvePaths = []; /** * Class constructor @@ -132,7 +141,7 @@ class WebAssetItem * * @since __DEPLOY_VERSION__ */ - public function __construct($name, array $data = array()) + public function __construct($name, array $data = []) { $this->name = strtolower($name); // No fancy Camels or Elephants $this->version = !empty($data['version']) ? $data['version'] : null; @@ -253,6 +262,60 @@ public function getWeight() return $this->weight; } + /** + * Get CSS files + * + * @param boolean $resolvePath Whether need to search for real path + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public function getCSSFiles($resolvePath = true) + { + $files = $this->css; + + if ($resolvePath) + { + foreach ($files as $path => $attr) + { + $resolved = $this->resolvePath($path, 'stylesheet'); + + $files[$path]['__isExternal'] = $resolved['external']; + $files[$path]['__fullPath'] = $resolved['fullPath']; + } + } + + return $files; + } + + /** + * Get JS files + * + * @param boolean $resolvePath Whether need to search for real path + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + public function getJSFiles($resolvePath = true) + { + $files = $this->js; + + if ($resolvePath) + { + foreach ($files as $path => $attr) + { + $resolved = $this->resolvePath($path, 'script'); + + $files[$path]['__isExternal'] = $resolved['external']; + $files[$path]['__fullPath'] = $resolved['fullPath']; + } + } + + return $files; + } + /** * Attach active asset to the Document * @@ -285,24 +348,15 @@ public function attach(Document $doc) */ protected function attachCSS(Document $doc) { - foreach ($this->css as $path => $attr) + foreach ($this->getCSSFiles(true) as $path => $attr) { - $file = $path; - $version = false; - - if (!$this->isPathExternal($path)) + if ($attr['__fullPath']) { - // Get the file path - $file = HTMLHelper::_('stylesheet', $path, [ - 'pathOnly' => true, - 'relative' => !$this->isPathAbsolute($path) - ] - ); - $version = 'auto'; - } + $file = $attr['__fullPath']; + $version = $attr['__isExternal'] ? false : 'auto'; + + unset($attr['__fullPath'], $attr['__isExternal']); - if ($file) - { $doc->addStyleSheet($file, ['version' => $version], $attr); } } @@ -321,24 +375,15 @@ protected function attachCSS(Document $doc) */ protected function attachJS(Document $doc) { - foreach ($this->js as $path => $attr) + foreach ($this->getJSFiles() as $path => $attr) { - $file = $path; - $version = false; - - if (!$this->isPathExternal($path)) + if ($attr['__fullPath']) { - // Get the file path - $file = HTMLHelper::_('script', $path, [ - 'pathOnly' => true, - 'relative' => !$this->isPathAbsolute($path) - ] - ); - $version = 'auto'; - } + $file = $attr['__fullPath']; + $version = $attr['__isExternal'] ? false : 'auto'; + + unset($attr['__fullPath'], $attr['__isExternal']); - if ($file) - { $doc->addScript($file, ['version' => $version], $attr); } } @@ -346,6 +391,49 @@ protected function attachJS(Document $doc) return $this; } + /** + * Resolve path + * + * @param string $path The path to resolve + * @param string $type The resolver method + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + protected function resolvePath($path, $type) + { + if (!empty($this->resolvePaths[$path])) + { + return $this->resolvePaths[$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->resolvePaths[$path] = [ + 'external' => $external, + 'fullPath' => $file ? $file : false, + ]; + + return $this->resolvePaths[$path]; + } + /** * Check if the Path is External * diff --git a/libraries/src/WebAsset/WebAssetRegistry.php b/libraries/src/WebAsset/WebAssetRegistry.php index 33ddbda141aa6..8d92793558aa5 100644 --- a/libraries/src/WebAsset/WebAssetRegistry.php +++ b/libraries/src/WebAsset/WebAssetRegistry.php @@ -85,7 +85,7 @@ class WebAssetRegistry * * @since __DEPLOY_VERSION__ */ - protected $dataFiles = array(); + protected $dataFiles = []; /** * Registry of available Assets @@ -94,7 +94,7 @@ class WebAssetRegistry * * @since __DEPLOY_VERSION__ */ - protected $assets = array(); + protected $assets = []; /** * Weight of the most heavier and active asset @@ -308,7 +308,7 @@ public function attach(Document $doc) // Pre-save existing Scripts, and attach them after requested assets. $jsBackup = $doc->_scripts; - $doc->_scripts = array(); + $doc->_scripts = []; // Attach an active assets do the document foreach ($assets as $asset) @@ -400,7 +400,7 @@ protected function resolveItemDependency(WebAssetItem $asset) */ protected function getDependenciesForAsset(WebAssetItem $asset) { - $assets = array(); + $assets = []; foreach ($asset->getDependencies() as $depName) { @@ -462,7 +462,7 @@ function($a, $b) use ($ask) * * @since __DEPLOY_VERSION__ */ - public function createAsset($name, array $data = array()) + public function createAsset($name, array $data = []) { return new WebAssetItem($name, $data); } @@ -570,9 +570,9 @@ protected function parseDataFile($path) } // Keep source info - $assetSource = $data; - $assetSource['dataFile'] = $path; - unset($assetSource['assets']); + $assetSource = [ + 'dataFile' => $path, + ]; // Prepare WebAssetItem instances foreach ($data['assets'] as $item)