<?php namespace Lightscale\LaralightAssets; use Illuminate\Support\Str; class Manifest { private ?array $data = null; private string $basePath; public function __construct( private string $path, private string $baseUrl, ?string $basePath = null, private string $parserClass = JsonManifestParser::class, ) { $fileName = Str::afterLast($path, '/'); $this->basePath = Str::replaceLast("/{$fileName}", '', $path); } private function readFile(): string { return file_get_contents($this->path); } private function loadData(): void { $data = $this->readFile(); $parser = new $this->parserClass; $this->data = $parser->parse($data); } private function getData(): array { if ($this->data === null) $this->loadData(); return $this->data; } public function getDir(): string { return $this->basePath; } public function getFile(string $file): ?string { return $this->getData()[$file] ?? null; } public function getUrl(string $file): ?string { $file = $this->getFile($file); return $file === null ? null : asset("{$this->baseUrl}/{$file}"); } public function getPath(string $file): ?string { $file = $this->getFile($file); $dir = $this->getDir(); return $file === null ? null : "{$dir}/{$file}"; } }