From 68e874dc9789479b14f9cede0342f13dfdce79b3 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Thu, 27 Apr 2017 00:17:05 -0600 Subject: [PATCH] AssetMaker Improvements (#2847) * Check if asset file exists before symbolizing path Adds a check to see if the asset file exists before attempting to symbolize it, this will allow the asset combiner to be passed assets with complete paths instead of only relative paths. * Implement combiner business logic * Improve handling of bad inputs --- modules/system/classes/CombineAssets.php | 4 ++-- modules/system/traits/AssetMaker.php | 25 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/modules/system/classes/CombineAssets.php b/modules/system/classes/CombineAssets.php index 7d6ef36be..23b41f61e 100644 --- a/modules/system/classes/CombineAssets.php +++ b/modules/system/classes/CombineAssets.php @@ -404,7 +404,7 @@ class CombineAssets $filesSalt = null; foreach ($assets as $asset) { $filters = $this->getFilters(File::extension($asset)) ?: []; - $path = File::symbolizePath($asset, null) ?: $this->localPath . $asset; + $path = file_exists($asset) ? $asset : File::symbolizePath($asset, null) ?: $this->localPath . $asset; $files[] = new FileAsset($path, $filters, public_path()); $filesSalt .= $this->localPath . $asset; } @@ -458,7 +458,7 @@ class CombineAssets $key = ''; $assetFiles = array_map(function ($file) { - return File::symbolizePath($file, null) ?: $this->localPath . $file; + return file_exists($file) ? $file : File::symbolizePath($file, null) ?: $this->localPath . $file; }, $assets); foreach ($assetFiles as $file) { diff --git a/modules/system/traits/AssetMaker.php b/modules/system/traits/AssetMaker.php index 4637af7a0..e81283082 100644 --- a/modules/system/traits/AssetMaker.php +++ b/modules/system/traits/AssetMaker.php @@ -4,6 +4,7 @@ use Url; use Html; use System\Models\Parameter; use System\Models\PluginVersion; +use System\Classes\CombineAssets; /** * Asset Maker Trait @@ -110,6 +111,10 @@ trait AssetMaker */ public function addJs($name, $attributes = []) { + if (is_array($name)) { + $name = $this->combineAssets($name); + } + $jsPath = $this->getAssetPath($name); if (isset($this->controller)) { @@ -136,6 +141,10 @@ trait AssetMaker */ public function addCss($name, $attributes = []) { + if (is_array($name)) { + $name = $this->combineAssets($name); + } + $cssPath = $this->getAssetPath($name); if (isset($this->controller)) { @@ -179,6 +188,22 @@ trait AssetMaker } } + /** + * Run the provided assets through the Asset Combiner + * @param array $assets Collection of assets + * @param string $localPath Prefix all assets with this path (optional) + * @return string + */ + public function combineAssets(array $assets, $localPath = '') + { + // Short circuit if no assets actually provided + if (empty($assets)) { + return ''; + } + $assetPath = !empty($localPath) ? $localPath : $this->assetPath; + return Url::to(CombineAssets::combine($assets, $assetPath)); + } + /** * Returns an array of all registered asset paths. * @return array