add asset utils (php side)

This commit is contained in:
Simon Vieille 2022-10-30 14:19:28 +01:00
parent f11dff5d91
commit 3e9a9fadf2
Signed by: deblan
GPG key ID: 579388D585F70417

64
lib/Util/AssetUtil.php Normal file
View file

@ -0,0 +1,64 @@
<?php
namespace OCA\SideMenu\Util;
use OC_Util;
use OCA\SideMenu\AppInfo\Application;
/**
* class AssetUtil.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class AssetUtil
{
protected array $entrypoints;
protected static ?AssetUtil $instance = null;
public function __construct()
{
$this->entrypoints = json_decode(
file_get_contents(__DIR__.'/../../assets/entrypoints.json'),
true
)['entrypoints'];
}
public static function getInstance(): self
{
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function addEntrypointAsset($entry, $type, $key): self
{
$file = basename($this->entrypoints[$entry][$type][$key]);
$path = $this->generatePath('assets', $file);
return $this->addAsset($type, $path);
}
public function addAsset($type, $path): self
{
$path = preg_replace('/\.(css|js)$/', '', $path);
if ('css' === $type) {
OC_Util::$styles[] = $path;
} elseif ('js' === $type) {
OC_Util::$scripts[] = $path;
}
return $this;
}
public function generatePath($directory, $file): string
{
return implode('/', [
Application::APP_ID,
$directory,
$file,
]);
}
}