Skip to content
Snippets Groups Projects
Commit bfda712e authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

implement PluginAssetsTrait::addAsset() and PluginAssetsTrait::addAssets(), fixes #2164

Closes #2164

Merge request studip/studip!1398
parent af51e825
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,55 @@
*/
trait PluginAssetsTrait
{
/**
* Adds an asset while detecting the type automatically.
*
* @param string $asset Asset to add
* @param array $variables Variables for the LESS/SCSS compiler, unused for JS
* @since Stud.IP 5.4
*/
public function addAsset(string $asset, array $variables = []): void
{
$type = $this->detectAssetType($asset);
if ($type === 'js') {
$this->addScript($asset);
} elseif ($type === 'css') {
$this->addStylesheet($asset, $variables);
}
}
/**
* Adds many assets while detecting the type automatically.
*
* @param string[] $assets Assets to add
* @param array $variables Variables for the LESS/SCSS compiler, unused
* for JS
* @param bool $combine If true, the assets will be combined into one
* single file for each type
* @since Stud.IP 5.4
*/
public function addAssets(array $assets, array $variables = [], bool $combine = false): void
{
if (!$combine) {
foreach ($assets as $asset) {
$this->addAsset($asset, $variables);
}
} else {
$temp = ['css' => [], 'js' => []];
foreach ($assets as $asset) {
$temp[$this->detectAssetType($asset)] = $asset;
}
if (count($temp['css']) > 0) {
$this->addStylesheets($temp['css'], $variables);
}
if (count($temp['js']) > 0) {
$this->addScripts($temp['js']);
}
}
}
/**
* Adds many stylesheeets at once.
* @param array $filenames List of relative filenames
......@@ -212,4 +261,26 @@ trait PluginAssetsTrait
}
return $contents;
}
/**
* Detects the asset type based on the extension of the asset.
*
* @param string $asset Asset to test
* @return string Either 'css' or 'js'
* @throws InvalidArgumentException if no valid type can be detected
*/
private function detectAssetType(string $asset): string
{
$extension = pathinfo($asset, PATHINFO_EXTENSION);
if ($extension === 'js') {
return 'js';
}
if (in_array($extension, ['css', 'less', 'scss'])) {
return 'css';
}
throw new InvalidArgumentException("Unknown asset type {$extension}");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment