diff --git a/lib/plugins/core/PluginAssetsTrait.php b/lib/plugins/core/PluginAssetsTrait.php index 326809c442081650eaeef52426937b73c2d9b9ea..7313661a2603994d871e52a5c84d9b2142f5dcc9 100644 --- a/lib/plugins/core/PluginAssetsTrait.php +++ b/lib/plugins/core/PluginAssetsTrait.php @@ -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}"); + } }