diff --git a/lib/Util/AssetUtil.php b/lib/Util/AssetUtil.php new file mode 100644 index 0000000..9232d10 --- /dev/null +++ b/lib/Util/AssetUtil.php @@ -0,0 +1,64 @@ + + */ +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, + ]); + } +}