aboutsummaryrefslogtreecommitdiff
path: root/lib/plugins
diff options
context:
space:
mode:
authorJan-Hendrik Willms <tleilax+studip@gmail.com>2023-03-13 21:37:16 +0000
committerJan-Hendrik Willms <tleilax+studip@gmail.com>2023-03-13 21:37:16 +0000
commitbfda712ee474f4b5753456f862362bf13b9002d6 (patch)
tree70e85e59da0b5f92d02cdd0d0f72f43f77c3c861 /lib/plugins
parentaf51e82596020e533bfd6df00e172d71f0d31e51 (diff)
implement PluginAssetsTrait::addAsset() and PluginAssetsTrait::addAssets(), fixes #2164
Closes #2164 Merge request studip/studip!1398
Diffstat (limited to 'lib/plugins')
-rw-r--r--lib/plugins/core/PluginAssetsTrait.php71
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/plugins/core/PluginAssetsTrait.php b/lib/plugins/core/PluginAssetsTrait.php
index 326809c..7313661 100644
--- a/lib/plugins/core/PluginAssetsTrait.php
+++ b/lib/plugins/core/PluginAssetsTrait.php
@@ -9,6 +9,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
* @param array $variables Optional array of variables to pass to the
@@ -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}");
+ }
}