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
This commit is contained in:
Luke Towers 2017-04-27 00:17:05 -06:00 committed by GitHub
parent 3a2b9b1973
commit 68e874dc97
2 changed files with 27 additions and 2 deletions

View File

@ -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) {

View File

@ -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